两个实体:
Notification
User
User
有一个属性叫做username
<<--->relationship
之间存在一对多的称为“用户”User
Notification
保存的Notification
对象 (ObjectA) 具有 (2)User
保存在“用户”关系中的对象。我想通过删除User
“用户”关系中的一个对象来更新 ObjectA。
User
实体有一个名为“用户名”的属性。
在“用户”关系中有(2)个User'
用户名“UserA”和“UserB”作为对象,我将如何删除“UserA”?
这是我想出的,但它不起作用:
NSFetchRequest *notificationRequest = [[NSFetchRequest alloc] initWithEntityName:@"Notification"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notification_id == %@", [selectedManagedObject valueForKey:@"notification_id"]];
[self.managedObjectContext executeFetchRequest:notificationRequest onSuccess:^(NSArray *results)
{
//Since I'm fetching based on objectID, there should always be one Object.
Notification *notificationObject = [results objectAtIndex:0];
NSArray *usersArray = [NSArray alloc]init];
//I don't think the code below is correct?
usersArray = [notificationObject valueForKey:@"users"];
for (User *user in userArray)
{
if (user.username == @"UserA")
{
[self.managedObjectContext deleteObject:user];
[self.managedObjectContext saveOnSuccess:^{
} onFailure:^(NSError *error) {
}];
} onFailure:^(NSError *error) {
}];
编辑
从关系中删除“UserA”对象的最佳方法是什么?