【发布时间】:2013-10-22 03:37:13
【问题描述】:
两个实体:
NotificationUser
User 有一个名为username 的属性
User 之间存在一对多的relationship Notification 称为“用户”
保存了一个Notification 对象(ObjectA),它在“用户”关系中保存了(2) 个UserObjects。我想通过删除“用户”关系中的 User 对象之一来更新 ObjectA。
User 实体有一个名为“用户名”的属性。
有(2)User's 用户名“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”对象的最佳方法是什么?
【问题讨论】:
-
这在什么方面不起作用?
-
不只是从用户关系中删除 UserA 对象,而是删除整个 Notification 对象。
标签: ios core-data one-to-many