0

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

我实例化 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];

问题不再出现!但这不是我想要的……

4

1 回答 1

0

好的,我找到了。感谢这个问题,我替换了这一行:

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

经过

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

有趣的是,您提出问题的事实如何使您走上解决问题的道路。

于 2012-09-27T09:31:10.750 回答