NSFetchedResultController忽略来自另一个NSManagedObjectContext的INSERTED对象

时间:2013-12-16 12:46:04

标签: ios core-data restkit nsfetchedresultscontroller

使用RestKit,我映射然后在具有专用上下文的后台线程中保存JSON中的对象。

我相信我正面临着这里强调的问题: NSFetchedResultsController with predicate ignores changes merged from different NSManagedObjectContext 这里也有很好的解释: http://www.mlsite.net/blog/?p=518

但我的问题不是UPDATED对象,而是INSERTED对象。在我的主要MOC中,我可以看到在mergeChangesFromContextDidSaveNotification之后插入了对象,但是他们的数据是错误的。我想这就是我NSPredicate NSFetchedResultController不起作用的原因(它查看特定字段)。 我尝试了其他SO问题的解决方法,但我无法让它工作。

[[NSNotificationCenter defaultCenter]
    addObserverForName:NSManagedObjectContextObjectsDidChangeNotification
    object:nil
    queue:nil
    usingBlock:^(NSNotification* note)
    {
        NSManagedObjectContext *moc = managedObjectStore.mainQueueManagedObjectContext;
        if (note.object != moc){
            // When I look at note.userInfo.description, I can see that the objects seem properly created and inserted, with all their data
            [moc performBlock:^(){
                [moc mergeChangesFromContextDidSaveNotification:note];
                }];
        }
        else {
            // This does get called, i.e. the main moc is indeed saved properly. BUT when I look at note.userInfo.description the objects' data is at fault. And my NSFetchedResultController does not pick up the insertion.
        }
    }
];

更新:添加我的FRC配置:

- (NSFetchedResultsController *)fetchedResultsController {

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

    NSFetchedResultsController* theFetchedResultsController;
    NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"userID" ascending:YES]];
    theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

    // Note: self.managedObjectContext is set to [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext

    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;


    [_fetchedResultsController performFetch:NULL];

    return _fetchedResultsController;

}

更新2 :这是我收到JSON数据时的操作:

RKObjectManager *manager = [RKObjectManager sharedManager];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"keyPath IN %@", @"users"];
RKEntityMapping *mapping = (RKEntityMapping *)[[[manager responseDescriptors] filteredArrayUsingPredicate:pre].firstObject mapping];
NSDictionary *mappingsDictionary = @{ [NSNull null]: mapping };

if (!_backgroundMOC){ // Create a MOC for background mapping
    NSManagedObjectContext* context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    context.persistentStoreCoordinator = [RKManagedObjectStore defaultStore].persistentStoreCoordinator;
    _backgroundMOC = context;
}


RKManagedObjectStore *store = [RKManagedObjectStore defaultStore];
RKManagedObjectMappingOperationDataSource *mappingDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:_backgroundMOC cache:store.managedObjectCache];

RKMapperOperation *mapperOperation = [[RKMapperOperation alloc] initWithRepresentation:(id)jSONData  mappingsDictionary:mappingsDictionary];
mapperOperation.mappingOperationDataSource = mappingDataSource;
mapperOperation.delegate = self;
[manager.operationQueue addOperation:mapperOperation];

- (void)mapperDidFinishMapping:(RKMapperOperation *)mapper {

    NSError *error;
    [((RKManagedObjectMappingOperationDataSource *)[mapper mappingOperationDataSource]).managedObjectContext save:&error];

}

这是否有一个已知的解决方法?或者我错过了什么?

0 个答案:

没有答案