我有一个 NSFetchedResultsController 以下列方式启动:
NSEntityDescription *myEntity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:myEntity];
[fetchRequest setFetchBatchSize:10];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"wasDeleted == %@", [NSNumber numberWithBool:NO]]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor, nil]];
myFetchedResults = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
myFetchedResults.delegate = self;
[myFetchedResults performFetch:nil];
一些事实:
wasDeleted 是 MyEntity 的 NSNumber 属性 (BOOL)
如果我将此属性更新
[NSNumber numberWithBool:YES]
为 NSFetchedResultsControllerDelegate 调用 didChangeObject 但触发NSFetchedResultsChangeUpdate而不是NSFetchedResultsChangeDelete。但是,如果我弹出视图并再次初始化获取的结果控制器,则该对象不再存在,表明它
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"wasDeleted == %@", [NSNumber numberWithBool:NO]]];
有效。在此 NSFetchedResultsChangeUpdate 之后,当调用 cellForRow 时,我打印 wasDeleted 并将其设置为 YES。
这里有什么问题?
(换句话说,如果对象仍然存在于上下文中但不适合谓词,则可以调用 NSFetchedResultsChangeDelete?)