我有一个项目设置,其中来自服务器的所有数据都使用托管模型写入Core Data托管商店。我使用mogenerator从Core Data模型生成所有实体。我将所有RestKit映射集成到我的实体中。
NSError *error = nil;
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"dataModel" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
// Initialize the Core Data stack
[managedObjectStore createPersistentStoreCoordinator];
NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
NSAssert(persistentStore, @"Failed to add persistent store: %@", error);
[managedObjectStore createManagedObjectContexts];
// Set the default store shared instance
[RKManagedObjectStore setDefaultStore:managedObjectStore];
由于时间限制,现在计划已经发生变化。根本不应存储数据。应从服务器读取数据并直接显示。没有储蓄,没有坚持。所以我想删除RKManagedObjectStore,保留实体和映射,并在请求成功或RKPaginator重新启动时从'RKMappingResult * mappingResult'读取数据。适用于RKManagedObjectStore和RKPaginator的示例:
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:[Friend entityMapping:objectManager.managedObjectStore]
method:RKRequestMethodAny
pathPattern:nil
keyPath:@"items"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
[objectManager setPaginationMapping:[self paginationMapping]];
self.paginator = [objectManager paginatorWithPathPattern:@"data"];
self.paginator.perPage = 20;
//Set completion block for this paginator
[self.paginator setCompletionBlockWithSuccess:^(RKPaginator *paginator, NSArray *objects, NSUInteger page) {
[weakSelf.dataArray addObjectsFromArray:objects];
} failure:^(RKPaginator *paginator, NSError *error) {
}];
然而,当我开始重新考虑RKManagedObjectStore时,我开始在映射时遇到问题。
'You must provide a managedObjectStore. Invoke mappingForClass:inManagedObjectStore: instead.'
Q.1我可以在没有RKManagedObjectStore的情况下使用Enitiy Mapping吗?我是朝着正确的方向前进的吗?
Q.2我可以删除商店并保留模型吗?
任何提示,帮助或示例都会很棒,然后我才能参与进来,走向错误的方向。 谢谢Al
答案 0 :(得分:2)
您应该反对需求变更并使用Core Data作为临时信息缓存来帮助进行内存管理(这样您就可以向上和向下滚动列表而无需一直加载所有内容)。这不应该再实施......
RKEntityMapping
,则无法使用RKManagedObjectStore
。