NSFetchedResultsController在输入背景后丢失了他的行

时间:2012-09-27 08:55:07

标签: ios nsfetchrequest nsfetchedresultscontroller

我的应用程序中存在一个奇怪的问题。

我实例化一个NSFetchedResultsController来填充我的数组。

它以这种方式声明(作为@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];

问题不再发生了!但那不是我想要的......

1 个答案:

答案 0 :(得分:0)

好的,我找到了。 Thanks to this question,我替换了这一行:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%@ IN items)", self.orderItem];

通过

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT SELF IN %@", self.orderItem.recipients];

有趣的是,你问一个问题的事实会让你走上解决的道路。