1

我已经检查了几个相关问题,例如如何判断 `NSManagedObject` 是否已被删除?,避免获取已删除的 NSManagedObjects

但是我确实使用了deleteCacheWithNameand 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 ]];

有什么办法可以检查我是否错过了什么?

4

1 回答 1

1

删除某些内容后,应添加:

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

这将提交该操作。

于 2012-11-12T01:15:07.763 回答