我也困惑了大约4或5小时:) 所以。 您继承的NSPersistentStore类是远程数据存储的“表示”。
所以,对于访问远程数据和保存/缓存在本地你需要做以下
1)创建NSPersistentStore和设置它的子类。
就像是:
YOURIncrementalStore *incrementalStore = [coordinator addPersistentStoreWithType:[YOURIncrementalStore type] configuration:nil URL:nil options:nil error:&error];
地方协调你的主要NSPersistentStoreCoordinator
2)然后,你需要其他NSPersistentStoreCoordinator,将 “协调地方代表(incrementalStore)和上下文外部存储的”并提供本地存储代表(如SQLite DB URL):
[incrementalStore.backingPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]
但不要忘记,你的新持久性商店必须知道你所有的以前的当地的状态。所以选项字典将是:
NSDictionary *options = @{ NSInferMappingModelAutomaticallyOption : @YES, NSMigratePersistentStoresAutomaticallyOption:@YES }
那么,恕我直言,我明白所有的内部工作是这样的:
您向外部API的一些数据。解析它,然后保存到backingPersistentStoreCoordinator的上下文中,然后合并到主要的一个。所以所有情境的状态将是平等的。
以前的所有文本均基于使用AFIncrementalStore解决方法。
我的代码与MagicalRecord实施AFIncrementalStore:正确
- (void)addMRAndAFIS {
[MagicalRecord setupCoreDataStack];
NSURL *storeURL = [NSPersistentStore urlForStoreName:[MagicalRecord defaultStoreName]];
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator defaultStoreCoordinator];
NSError *error = nil;
NSArray *arr = coordinator.persistentStores;
AFIncrementalStore *incrementalStore = (AFIncrementalStore*)[coordinator addPersistentStoreWithType:[PTIncrementalStore type] configuration:nil URL:nil options:nil error:&error];
NSDictionary *options = @{ NSInferMappingModelAutomaticallyOption : @YES,
NSMigratePersistentStoresAutomaticallyOption:@YES };
arr = coordinator.persistentStores;
if (![incrementalStore.backingPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
如果我们需要讨论的最简单的方法,你只需要继承NSIncrementalStore,设置它(像我写的),解析数据,然后创建一些上下文,保存日期,然后保存并合并到父上下文。
因此,您将拥有2个商店和2个上下文以及1个StoreCoordinator。
如果我在某处犯了错误,请参考它。
也试试:https://gist.github.com/stevederico/5316737
我已经开始实现我自己的子类。我将尽快公开发布,敬请关注。 :D – wczekalski
那又如何?你得到你想要的东西吗? –
我仍然在做,但我很快就会完成。它会在我的github上可用 - github.com/wczekalski – wczekalski