0

I have two entities with a to-many relationship, there can be multiple items to each list.

  • List
  • Item

Basically all I want to achieve is to to save an Item to a specific List on click.

I've figured out how to save a new list:

- (IBAction)handleBtnAddList:(id)sender

{ MyListAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  NSManagedObjectContext *context = [appDelegate managedObjectContext];
  NSManagedObject *newList;

newList =  [NSEntityDescription
           insertNewObjectForEntityForName:@"List"
           inManagedObjectContext:context];
[newContact setValue:@"Shopping" forKey:@"name"];

NSError *error;
[context save:&error]; }

But how do I save an item to the newly created list "Shopping" ?

Any help appreciated. Thanks in advance!


Hi there, finally had a chance to try this out. It's working now but running into another issue. I can add a new list, but can't seem to add an item.

I looked in the sqlite database and under the 'Item' entity the column named 'zLists' is empty. How do I get a value in there that will correspond with the 'List' that the item should be under?

This is what I've got

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *list = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"List"
                                   inManagedObjectContext:context];
[list setValue:@"Test List" forKey:@"name"];
NSManagedObject *item = [NSEntityDescription
                                      insertNewObjectForEntityForName:@"Item"
                                      inManagedObjectContext:context];
[item setValue:@"Test Item" forKey:@"name"];

I also tried adding this at the end but it crashes the app

[item setValue:@"Test List" forKey:@"lists"];

1 Answer 1

0

For my example to work with your code, you will need these prerequisites...

  1. You have a Core Data model NSManagedObjectModel,
  2. Within that model, you have an entity List,
  3. Within that model and for that entity, you have two attributes listDate, and listText,
  4. You have used Xcode to prepare (or have manually prepared) an NSManagedObject subclass for your entity List.

Instead of your line of code: [newContact setValue:@"Shopping" forKey:@"name"];...

you might choose to use dot notation to set attribute values in this manner...

newList.listDate = [NSDate date];  //sets the attribute to current date and time
newList.listText = <<some user input from (for example) a view controller>>;

or to match your syntax, you might choose to use key-value coding in this manner.

[newList setValue:[NSDate date] forKey:@"listDate"];
[newList setValue:<<some user input>> forKey:@"listText"];
2
  • Thanks heaps for your reply. I'll try this now. Jun 4, 2014 at 3:03
  • Saving a List works now. However can't seem to save an Item. I've edited my question with details... Jun 7, 2014 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.