我的应用程序中有一个奇怪的问题。
我实例化 aNSFetchedResultsController
来填充我的数组。
它是这样声明的(作为@propoerty (nonatomic, retain)
)
- (NSFetchedResultsController *)fetchedContactsController {
if (self->fetchedContactsController != nil) return self->fetchedContactsController;
// Create the fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// I want every contact...
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self->managedObjectContext];
[fetchRequest setEntity:entity];
// ... whose are not associated with my order item
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%@ IN items)", self.orderItem];
[fetchRequest setPredicate:predicate];
// Edit the section name key path and cache name if appropriate
self.fetchedContactsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self->managedObjectContext sectionNameKeyPath:nil cacheName:nil];
[aFetchedResultsController release];
[firstnameSortDescriptor release];
[lastnameSortDescriptor release];
[sortDescriptors release];
[fetchRequest release];
// Execute the fetch request
[self->fetchedContactsController performFetch:nil];
return self->fetchedContactsController;
}
当然,contacts.items 是存在的。
当我的应用程序进入后台然后再次进入前台时,如果我再做一次
NSLog(@"before >>> %d", [[fetchedContactsController fetchedObjects]count]); // >>> 3
[self->fetchedContactsController performFetch:nil];
[tableView reloadData];
NSLog(@"after >>> %d", [[fetchedContactsController fetchedObjects]count]); // >>> 0
我数组中的每一行都消失了!
编辑 :
如果我评论这些行:
// ... whose are not associated with my order item
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%@ IN items)", self.orderItem];
//[fetchRequest setPredicate:predicate];
问题不再出现!但这不是我想要的……