NSFetchedResultsController未通知模型更改

时间:2013-01-08 11:39:58

标签: ios nsfetchedresultscontroller

不知何故失去了在其他项目中起作用的东西,现在停止了工作。

我有一个控制器(A)(NSObject的子类),它有一个NSCetchedResultsController用于类kClassCycleQuestionDetails的实体。 我的ManagedObjectContext是使用NSMainQueueConcurrencyType启动的。

在另一个控制器(B)中,我开始插入上述类的实体,并在每个第十个对象上开始保存。

[self.moc performBlock:^{
        [self prepareNewCycle];
    }];

上下文发布NSManagedObjectContextDidSaveNotification(在B中收到)但我从未看到控制器委托在A中调用的方法- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

两个控制器共享相同的托管对象上下文,所以我应该看到模型更改?!

有没有人有想法。

编辑: 为了澄清:prepareNewCycle将循环实体插入到上下文中 将kClassCycleQuestionDetails的实体添加到循环实体(to-many关系)。

这是控制器A的fetchedResultsController

的代码
- (NSFetchedResultsController *)fetchedResultsController {

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

/*
 Set up the fetched results controller.
 */
// Create the fetch request for the entity
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:kClassCycleQuestionDetails inManagedObjectContext:self.moc];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

[fetchRequest setPredicate:[self predicateForExamCycleMode:self.cycle.examQuestionModeValue cycleId:self.cycle.cycle_id]];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:kQCQDCycleQuestionDetails_id ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
                                                         initWithFetchRequest:fetchRequest
                                                         managedObjectContext:self.moc
                                                         sectionNameKeyPath:nil
                                                         cacheName:NSStringFromClass([self class])];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;


[NSFetchedResultsController deleteCacheWithName:NSStringFromClass([self class])];
NSError *error = nil;
if (![_fetchedResultsController performFetch:&error]) {
    ALog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _fetchedResultsController;

}

0 个答案:

没有答案