2012-10-11 71 views
1

我已經檢查了像How can I tell whether an `NSManagedObject` has been deleted?,Avoid fetching deleted NSManagedObjects這樣的幾個相關問題。NSFetchedResultsController仍然帶回NSManagedObject,它已被刪除

但我確實使用了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

你刪除的東西后,你應該增加:

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

這將提交動作。

相關問題