0

我有这个 fetchedResult 方法:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cards" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Cards" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;

    if (![self.fetchedResultsController performFetch:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
}

但是,当我要求它时,self.fetchedResultsController.fetchedObjects.count它总是会给我比应有的多一件。第一项总是这样:"<Cards: 0x83e9c50> (entity: Cards; id: 0x83e9c80 <x-coredata:///Cards/t8C371600-7875-468C-A529-C918F0008C384> ; data: {\n cardID = nil;\n favourite = 0;\n imageData = nil;\n})"但是通过终端检查 sqlite 我可以看到这个项目不存在。有什么想法吗?

4

1 回答 1

0

很难理解发生了什么。你的代码对我来说看起来不错。但是,如果您使用缓存机制,这可能是您遇到的奇怪行为。

要测试,请尝试使用nil而不是缓存,NSFetchedResultsController或者先删除缓存,然后再执行请求。

+ (void)deleteCacheWithName:(NSString *)name

如果您通过nil,所有的现金都将被删除。

如果你修复它,请告诉我。

PS我认为在幕后,现金被保存下来,它们可以存在于不同的应用程序初创公司中。有人知道吗?

于 2012-06-16T14:12:49.743 回答