【问题标题】:Object Mapping with RestKit to CoreData使用 RestKit 到 CoreData 的对象映射
【发布时间】:2013-11-28 21:31:59
【问题描述】:

这段代码:

NSString* MIMEType = @"application/xml";
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:MIMEType];

NSError* error = nil;
NSString* XMLString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:myLocallyStoredFileName ofType:@"xml"]
                                                encoding:NSUTF8StringEncoding
                                                   error:nil];
NSData* XMLData = [XMLString dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:XMLData MIMEType:MIMEType error:&error];
if (parsedData == nil && error) {
    // Parser error...
}

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[myCoreDataObject class]];
[mapping addAttributeMappingsFromDictionary:@{
                                             ... my mappings ...
                                                     }];

NSDictionary *mappingsDictionary = @{ [NSNull null]: mapping };
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
NSError *mappingError = nil;


NSPersistentStoreCoordinator *psc = ((MPTAppDelegate *)[UIApplication sharedApplication].delegate).persistentStoreCoordinator;
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];

RKFetchRequestManagedObjectCache *cache = [RKFetchRequestManagedObjectCache new];

RKManagedObjectMappingOperationDataSource *source =
[[RKManagedObjectMappingOperationDataSource alloc]
 initWithManagedObjectContext:context
 cache:cache];

mapper.mappingOperationDataSource = source;
BOOL isMapped = [mapper execute:&mappingError];
if (isMapped && !mappingError) {
    NSLog(@"Error");
}

给我以下错误:

CoreData:错误:无法调用 NSManagedObject 类“myCoreDataObject”上的指定初始化程序

就好像映射操作不知道我在使用 Core Data。我该如何解决这个问题?

【问题讨论】:

    标签: ios restkit restkit-0.20


    【解决方案1】:

    这是因为您的映射是RKObjectMapping 的实例,它应该是RKEntityMapping 的实例才能与Core Data 正确集成。当您创建映射时,您将提供核心数据实体而不是代表类。


    另外,你真的应该让 RestKit 为你管理核心数据堆栈。为此,请创建类似于以下内容的堆栈:

    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:model];
    
    NSError *error;
    NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"XXXX.sqlite"];
    
    NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
    
    [managedObjectStore createManagedObjectContexts];
    

    那么你有一个完全配置的managedObjectStore 可以使用。

    (目前您没有managedObjectStore,所以从您评论中的代码来看,您只是传递了nil)。

    【讨论】:

    • 我做了以下更改: RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"myCoreDataObject" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];但现在我得到:无效参数不满意:managedObjectStore
    猜你喜欢
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    相关资源
    最近更新 更多