NSIncrementalStore:无法从此NSManagedObjectContext的协调器访问Object的持久性存储

时间:2014-01-14 19:50:18

标签: objective-c core-data ios7 nsincrementalstore

我正在尝试实现一个NSIncrementalStore,它将通过RPC API访问服务器。它需要在本地缓存所有数据,因此我首先创建了一个NSIncrementalStore来使用另一个用作缓存的核心数据堆栈。

首先,我设置元数据并设置缓存将使用的核心数据堆栈。缓存SQL文件是初始化存储时传入的URL:

    - (BOOL)loadMetadata:(NSError *__autoreleasing *)error {

NSMutableDictionary *mutableMetadata = [NSMutableDictionary dictionary];
[mutableMetadata setValue:[[NSProcessInfo processInfo] globallyUniqueString] forKey:NSStoreUUIDKey];
[mutableMetadata setValue:RTkCachedAPIStoreType forKey:NSStoreTypeKey];
//[mutableMetadata setValue:NSStringFromClass([self class]) forKey:NSStoreTypeKey];
[self setMetadata:mutableMetadata];

// Set up the Cache Stack
NSManagedObjectModel *model = [self.persistentStoreCoordinator.managedObjectModel copy];
self.cacheModel = model;

self.cachePersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.cacheModel];

self.cacheContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];

NSURL *storeURL;
if ([self.URL isFileURL]) {
    storeURL = self.URL;
}

NSError *storeAddError = nil;
if (![self.cachePersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                        configuration:nil
                                                                  URL:storeURL
                                                              options:nil
                                                                error:&storeAddError]) {
    NSLog(@"Fatal error while creating persistent store: %@", storeAddError);
    abort();
}

[self.cacheContext setPersistentStoreCoordinator:self.cachePersistentStoreCoordinator];

return YES;
}

对实体的简单请求的执行请求类似于以下代码。本质上我只是接受请求,将其包装为FetchRequest并在Cache Core数据堆栈上执行请求。然后,我使用调用上下文中返回的objectID创建对象:

- (id)executeRequest:(NSPersistentStoreRequest *)request withContext:(NSManagedObjectContext *)context error:(NSError *__autoreleasing *)error {

if ([request requestType] == NSFetchRequestType) {
    NSFetchRequest *fetchRequest = (NSFetchRequest *)request;
    NSEntityDescription *entity = [fetchRequest entity];

    NSFetchRequest *cacheFetchRequest = [[NSFetchRequest alloc] init];
    cacheFetchRequest.entity = entity;
    __block NSArray *fetchedObjectFromCache;
    [self.cacheContext performBlockAndWait:^(){
        fetchedObjectFromCache = [self.cacheContext executeFetchRequest:cacheFetchRequest error:error];
    }];
    NSMutableArray *fetchedObjects = [NSMutableArray arrayWithCapacity:[fetchedObjectFromCache count]];
    for (NSManagedObject *anObject in fetchedObjectFromCache) {
        NSManagedObjectID *objectID = anObject.objectID;
        NSManagedObject *managedObject = [context objectWithID:objectID];
        [fetchedObjects addObject:managedObject];
    }
    return fetchedObjects;
}
}

当我尝试这个时,会抛出错误

的异常

<unknown>:0: error: -[] : Object's persistent store is not reachable from this NSManagedObjectContext's coordinator

在第NSManagedObject *managedObject = [context objectWithID:objectID];行我无法解释为什么会这样 - 这是苹果建议在文档中完成的确切方式!

这个错误似乎与跨线程访问上下文有关 - 但我不这样做。 关于这里有什么问题的任何建议?

1 个答案:

答案 0 :(得分:3)

您使用的是错误的objectID。

缓存中的objectID与持久存储的objectID不匹配,因为增量存储没有相同的标识符。