views:

131

answers:

2

Hi All,

I have User and Friend entities in my data model, being a one User to many Friends relationship.

My ViewController, is instantiated with an instance variable for User (*user), and therefore I can access all friends by loading user.friends as friends is defined as an NSSet in my User object.

In my code I load all friends in an NSMutableArray, do some stuff, and potentially before leaving want to add additional friends, and edit the attributes of existing friends. I'm at a loss on how I can add/edit friends.

Should I edit the NSMutableArray of friend objects, and save that back to the User.context? If so, how?

If editing a friend, should I copy existing friend object, change values, delete old object from the array and add the new (copied and updated) one?

I hope this makes sense...

A: 

If you have a pointer/reference to a NSManagedObject you can edit all attributes. Say you get one friend like this:

Friend *aFriend = [user.friends anyObject];
[aFriend setLastName:@"new Lastname"];
NSError *error = nil;
[[self managedObjectContext] save:&error];

To add a friend do this:

Friend *aNewFriend = [NSEntityDescription insertNewObjectForEntityForName:@"friend" inManagedObjectContext:[self managedObjectContext]];
[user addFriendsObject:aNewFriend];
NSError *error = nil;
[[self managedObjectContext] save:&error];

However this will not add the new friend to the NSMutableArray that you created from the user.friends set earlier.

Felix
Hi,I don't have a NSManagedObject in my controller (I don't think?). I setup my view controller from a UITableView like so:User *user = (User *)[fetchedResultsController objectAtIndexPath:indexPath];viewController.user = user;And in that viewController, I get friends like so:NSMutableArray *friends = [[NSMutableArray alloc] initWithArray:[user.friends allObjects]];Is this the wrong approach - I have copied the Apple sample code for iPhone Recipes in Core Data.
mootymoots
what I should add is that with my approach I can get the user.context, so can I save against that?
mootymoots
you can do full tracking of changes when using NSFetchedResultsController. Any changes will be reported back to your controller where you can save it in the context. Check out the Overview in docs for more info - http://developer.apple.com/iphone/library/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html
Anurag
User is a subclass of NSManagedObject, so you can get the managed object context from a User object: [user managedObjectContext].
gerry3
+1  A: 

You can modify your Friend objects (no need to make new copies and delete old ones).

Try this:

// create a mutable copy of an array from the set of the user's friends
NSMutableArray *friends = [[user.friends allObjects] mutableCopy];

// modify friends array or any Friend objects in the array as desired

// create a new set from the array and store it as the user's new friends
user.friends = [NSSet setWithArray:friends];
[friends release];

// save any changes
NSManagedObjectContext *moc = [user managedObjectContext];
if ([moc hasChanges] && ![moc save:&error]) {
    // handle error
}

You could also use a mutable set instead of an array:

// create a mutable copy of the set of the user's friends
NSMutableSet *friends = [user.friends mutableCopy];

// modify friends set or any Friend objects in the set as desired

// create a new set from the set and store it as the user's new friends
user.friends = [NSSet setWithSet:friends];
[friends release];

// save any changes
NSManagedObjectContext *moc = [user managedObjectContext];
if ([moc hasChanges] && ![moc save:&error]) {
    // handle error
}
gerry3
Thank you - this approach appears to be working.
mootymoots