我检查过几个相关问题,例如How can I tell whether an `NSManagedObject` has been deleted?,Avoid fetching deleted NSManagedObjects。
但我确实使用了deleteCacheWithName
和deleteObject:
以及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 ]];
有什么方法可以检查我是否遗漏了什么?
答案 0 :(得分:1)
删除内容后,您应添加:
NSError *error;
[self.managedObjectContext save:&error];
那将提交行动。