RestKit 0.20.1将本地JSON映射到Core Data崩溃

时间:2013-05-26 01:28:30

标签: ios objective-c json core-data restkit

我正在尝试使用RestKit将本地JSON映射到我的Core Data实体,因为我正在处理对API的身份验证并且已经在使用另一个库来执行此操作,因此我只是将RestKit传递给JSON数据数组(我已经验证我正确接收)。我已经设置了所有基本必需组件(数据模型/实体,NSManagedObject类,托管对象上下文等),因为我正在调整我编写的现有应用程序,该应用程序使用RestKit访问RSS提要并将其映射到Core数据

从研究开始,似乎我应该使用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'

好像它正在对我的数据执行映射操作,但它没有传递我的实体和映射信息,从我的log (JSON data erased).可以看出我不确定我做错了什么,因为它在我使用RKManagedObjectRequestOperation之前工作正常。

如果有人有任何建议或帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

第一个问题(也可能是第二个问题)的原因是由:

引起的
  

FoodTruck * foodTruck = FoodTruck.new;

因为您不允许实例化此类托管对象。相反,您必须使用NSEntityDescription。您目前使用的代码不会创建有效的实例,因此当您尝试使用它时会发生不好的事情。