Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSManagedObject中未保存的NSSet成员是否会在保存时保留已删除的成员?_Ios_Cocoa Touch_Core Data_Nsmanagedobject - Fatal编程技术网

Ios NSManagedObject中未保存的NSSet成员是否会在保存时保留已删除的成员?

Ios NSManagedObject中未保存的NSSet成员是否会在保存时保留已删除的成员?,ios,cocoa-touch,core-data,nsmanagedobject,Ios,Cocoa Touch,Core Data,Nsmanagedobject,今天我遇到一位同事提出的一个有趣的问题。起初答案似乎很简单,但后来我意识到我从未见过任何支持我假设的文档 给定一个NSManagedObject,例如Person,它与另一个对象(例如Friends)具有一对多关系,下面的代码--friends1和friends2是否会被持久化到数据存储中 NSManagedObjectContext *context = [a private context]; Person *aPerson = [NSEntityDescription insertNewO

今天我遇到一位同事提出的一个有趣的问题。起初答案似乎很简单,但后来我意识到我从未见过任何支持我假设的文档

给定一个
NSManagedObject
,例如
Person
,它与另一个对象(例如
Friends
)具有一对多关系,下面的代码--
friends1
friends2
是否会被持久化到数据存储中

NSManagedObjectContext *context = [a private context];
Person *aPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"
                                                inManagedObjectContext:context];

NSMutableSet *friendsSet = [NSMutableSet new]; 
// Create some friends in the private context
Person *aFriend1 = [NSEntityDescription insertNewObjectForEntityForName:@"Friend"
                                                 inManagedObjectContext:context];
[friendsSet addObject:friend1];
Person *aFriend2 = [NSEntityDescription insertNewObjectForEntityForName:@"Friend"
                                                 inManagedObjectContext:context];
[friendsSet addObject:friend2];

// Add these friends to aPerson
[person addFriends:friendsSet];

// Alas! aPerson decides they want better friends.
NSMutableSet *betterFriendsSet = [NSMutableSet new]; 

Person *aFriend3 = [NSEntityDescription insertNewObjectForEntityForName:@"Friend"
                                                 inManagedObjectContext:context];
[betterFriendsSet addObject:friend3];
Person *aFriend4 = [NSEntityDescription insertNewObjectForEntityForName:@"Friend"
                                                 inManagedObjectContext:context];
[betterFriendsSet addObject:friend4];

// Dump those old friends
aPerson.friends = nil;
// Add the new friends
[person addFriends:betterFriendsSet];

// Save 
[context save:nil];

一个人在数据存储中有2个朋友还是4个朋友?我的假设是2,因为不再有任何对friend1和friend2的强烈引用,它们应该被发布。然而,核心数据生成的访问器可能会在幕后做更多的工作,并且对它们有一个强引用,在这种情况下,它们将被持久化到数据存储中

首先,为核心数据属性添加和删除集合成员提供的核心数据方法是(示例属性是“Ratings”): 添加单个成员

- (void)addRatingsObject:(RatingsDB *)value;
- (void)removeRatingsObject:(RatingsDB *)value;
添加一组成员

- (void)addRatings:(NSSet *)value;
- (void)removeRatings:(NSSet *)value;
接下来,insertNewObjectForEntityForName方法将为“Friend”创建核心数据对象,并且无论您如何处理NSSet内容,这些对象都将以保存方式保存。它们将与使用nil创建的“Person”实体解除链接


如前所述完成后,您将有四个“朋友”记录。

您实际上在问两个不同的问题:

friend1和friend2是否会持久化到数据存储中

NSManagedObjectContext *context = [a private context];
Person *aPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"
                                                inManagedObjectContext:context];

NSMutableSet *friendsSet = [NSMutableSet new]; 
// Create some friends in the private context
Person *aFriend1 = [NSEntityDescription insertNewObjectForEntityForName:@"Friend"
                                                 inManagedObjectContext:context];
[friendsSet addObject:friend1];
Person *aFriend2 = [NSEntityDescription insertNewObjectForEntityForName:@"Friend"
                                                 inManagedObjectContext:context];
[friendsSet addObject:friend2];

// Add these friends to aPerson
[person addFriends:friendsSet];

// Alas! aPerson decides they want better friends.
NSMutableSet *betterFriendsSet = [NSMutableSet new]; 

Person *aFriend3 = [NSEntityDescription insertNewObjectForEntityForName:@"Friend"
                                                 inManagedObjectContext:context];
[betterFriendsSet addObject:friend3];
Person *aFriend4 = [NSEntityDescription insertNewObjectForEntityForName:@"Friend"
                                                 inManagedObjectContext:context];
[betterFriendsSet addObject:friend4];

// Dump those old friends
aPerson.friends = nil;
// Add the new friends
[person addFriends:betterFriendsSet];

// Save 
[context save:nil];
对。将它们添加到托管对象上下文中,然后保存更改。它们是否保存不受它们是否与
aPerson
相关的影响。这与生成的访问器无关,只是将它们添加到上下文中,而从未删除它们。如果你不想保留它们,你需要删除它们

一个人在数据存储中有2个朋友还是4个朋友

它有两个。由于您将
friends
设置为
nil
,因此删除了前两个friends实例的关系。当上述代码完成后,
aPerson
有两个朋友。以前的朋友仍在永久存储中,但他们不再与任何
人相关