我正在尝试使用 RestKit 将本地 JSON 映射到我的核心数据实体,因为我正在处理对 API 的身份验证并且已经在使用另一个库来执行此操作,所以我只是将 RestKit 传递给 JSON 数组数据(我已经验证我接收正确)。我已经设置了所有基本的必要组件(数据模型/实体、NSManagedObject 类、托管对象上下文等),因为我正在调整我编写的现有应用程序,该应用程序使用 RestKit 访问 RSS 提要并将其映射到核心数据。
从研究来看,我似乎应该使用它RKManagedObjectMappingOperationDataSource
来完成此操作,并为此调整了我在单元测试中找到的示例:
-(void)performMapping
{
FoodTruck *foodTruck = FoodTruck.new;
RKEntityMapping *mapping = [ObjectMappings FoodTruckArticleMapping];
RKManagedObjectStore *store = [[FoodTruckDataModel sharedDataModel] objectStore];
RKManagedObjectMappingOperationDataSource *mappingDS = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.mainQueueManagedObjectContext cache:store.managedObjectCache];
mappingDS.operationQueue = [NSOperationQueue new];
RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:self.moreStatuses destinationObject:foodTruck mapping:mapping];
operation.dataSource = mappingDS;
NSError *error = nil;
[operation performMapping:&error];
[mappingDS.operationQueue waitUntilAllOperationsAreFinished];
}
我的对象映射是在一个单独的映射类中完成的,如下所示:
+(RKEntityMapping *)FoodTruckArticleMapping
{
RKEntityMapping *jsonMapping = [RKEntityMapping mappingForEntityForName:@"FoodTruck" inManagedObjectStore:[[FoodTruckDataModel sharedDataModel] objectStore]];
jsonMapping.identificationAttributes = @[@"tweetID"];
[jsonMapping addAttributeMappingsFromDictionary:@{
@"text": @"tweet", @"user.screen_name": @"foodTruckName", @"id": @"tweetID", @"created_at": @"timeStamp"}];
return jsonMapping;
}
我的 FoodTruckDataModel 类如下所示:
+(id)sharedDataModel {
static FoodTruckDataModel *__sharedDataModel = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__sharedDataModel = [[FoodTruckDataModel alloc] init];
});
return __sharedDataModel;
}
- (NSManagedObjectModel *)managedObjectModel
{
return [NSManagedObjectModel mergedModelFromBundles:nil];
}
-(id)optionsForSQLiteStore
{
return @{
NSInferMappingModelAutomaticallyOption: @YES,
NSMigratePersistentStoresAutomaticallyOption: @YES
};
}
-(void)setup
{
self.objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[self managedObjectModel]];
[self.objectStore createPersistentStoreCoordinator];
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"FoodTruckxxx.sqlite"];
NSLog(@"Setting up store at %@", path);
[self.objectStore addSQLitePersistentStoreAtPath:path
fromSeedDatabaseAtPath:nil
withConfiguration:nil
options:self.optionsForSQLiteStore
error:nil];
[self.objectStore createManagedObjectContexts];
self.objectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:self.objectStore.persistentStoreManagedObjectContext];
}
但是,有些事情没有正确完成,因为我收到了这个核心数据错误:
2013-05-25 19:38:17.867 FoodTrucks[37340:c07] CoreData: error: Failed to call designated initializer on NSManagedObject class 'FoodTruck'
我也对此感到崩溃:
2013-05-25 19:38:17.887 FoodTrucks[37340:4903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
看起来好像它正在对我的数据执行映射操作,但它没有被传递给我的实体和映射信息,从我的日志中可以看出(JSON 数据已删除)。 我不确定我做错了什么,因为在我使用RKManagedObjectRequestOperation
.
如果有人有任何建议或帮助,将不胜感激!