NSFetchedResultsController仍然会返回已删除的NSManagedObject

时间:2012-10-11 04:11:50

标签: ios core-data nsmanagedobject nsmanagedobjectcontext

我检查过几个相关问题,例如How can I tell whether an `NSManagedObject` has been deleted?Avoid fetching deleted NSManagedObjects

但我确实使用了deleteCacheWithNamedeleteObject:以及save,并检查了对象是否已被删除。每次我从后台删除应用程序。并重新开始,已删除的数据NSManagedObject再次返回。

这是我删除NSManagedObject的方式。

[NSFetchedResultsController deleteCacheWithName:@"MyLists"];
[_fetchedResultsController.managedObjectContext deleteObject:list];
NSError *error;
if ([_fetchedResultsController.managedObjectContext save:&error]) {
  NIDPRINT(@"managedObjectContext Save Error: %@", error.localizedDescription);
}
NIDPRINT(@"Check hasManagedObjectBeenDeleted: %@", [self hasManagedObjectBeenDeleted:list] ? @"YES" : @"NO");

这就是我初始化NSFetchedResultsController

的方式
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyList"];
fetchRequest.sortDescriptors = @[ [[NSSortDescriptor alloc] initWithKey:@"listID" ascending:YES] ];
NSString *userName = [[ICAccountManager sharedManager] getUsername];
NSString *format = [NSString stringWithFormat:@"username==\"%@\"", userName];
fetchRequest.predicate = [NSPredicate predicateWithFormat:format];

fetchRequest.propertiesToFetch = [NSArray arrayWithObject:@"count"];
fetchRequest.shouldRefreshRefetchedObjects = YES;
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[(id)[[UIApplication sharedApplication] delegate] managedObjectContext] sectionNameKeyPath:nil cacheName:@"lists"];
_fetchedResultsController.delegate = self;
[_fetchedResultsController performSelectorOnMainThread:@selector(performFetch:) withObject:nil waitUntilDone:YES modes:@[ NSRunLoopCommonModes ]];

有什么方法可以检查我是否遗漏了什么?

1 个答案:

答案 0 :(得分:1)

删除内容后,您应添加:

NSError *error;
[self.managedObjectContext save:&error];

那将提交行动。