1

我有以下代码:

- (NSFetchedResultsController *)fetchedResultsController {
    // Set up the fetched results controller if needed.
    if (fetchedResultsController == nil) {

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"DiskStory" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];

        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"isRemoved == %@", [NSNumber numberWithBool:NO]];

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];

        // Set limit
        [fetchRequest setFetchBatchSize:25];
        // Set batch size
        [fetchRequest setFetchLimit:50];


        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:kSavedStoryCache];
        aFetchedResultsController.delegate = self;
        fetchedResultsController = aFetchedResultsController;
 }
    return fetchedResultsController;
}    

在我看来DidLoad 我有:

 NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

我想知道为什么我的:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

}

代表,不是叫吗?我在里面放了一个断点。任何想法?

4

2 回答 2

3

我之前遇到过这个问题,第一次调用时不会调用它,performFetch而只有在fetchedResultsController实际更改数据时才会调用它。例如,当您从此获取结果中的上下文中删除对象时,将调用委托。

于 2012-10-23T00:09:53.900 回答
0

我想你已经使用了自我。在错误的地方。请看下面的代码;我已对更改添加评论

 - (NSFetchedResultsController *)fetchedResultsController {
    // Set up the fetched results controller if needed.
    if (fetchedResultsController == nil) {

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"DiskStory" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];

        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"isRemoved == %@", [NSNumber numberWithBool:NO]];

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];

        // Set limit
        [fetchRequest setFetchBatchSize:25];
        // Set batch size
        [fetchRequest setFetchLimit:50];


        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:kSavedStoryCache];
        aFetchedResultsController.delegate = self;
        fetchedResultsController = aFetchedResultsController;     // -> Shouldn't be self.fetchedResultsController 
 }
    return fetchedResultsController;        // -> Shouldn't be self.fetchedResultsController 
}   

此外,如果您已按如下方式合成fetchedResultsController :

fetchedResultsController = _fetchedResultsController 

然后在- (NSFetchedResultsController *)fetchedResultsController方法中,您应该将其称为 _fetchedResultsController

于 2012-10-22T23:20:30.863 回答