我正在获取一个本地 JSON 文件并尝试将其映射到关系映射中。它在没有关系的情况下工作正常,但是一旦我添加了关系,就会出错。

JSON:

https://gist.github.com/4675414

代码:

    // Get json from destination
    NSString *myJSON = [[NSString alloc] initWithContentsOfFile:contentPath encoding:NSUTF8StringEncoding error:NULL];

    NSString* MIMEType = @"application/json";
    NSError* parseError;

    NSData *data = [myJSON dataUsingEncoding:NSUTF8StringEncoding];
    id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&parseError];
    if (parsedData == nil && parseError) {
        NSLog(@"Cannot parse data: %@", parseError);
    }

    // Setting up objectmapping for issue
    RKObjectMapping *issueMapping = [RKObjectMapping mappingForClass:[Issue class]];
    [issueMapping addAttributeMappingsFromDictionary:@{
     @"title":                      @"title",
     @"description":                @"description",
     @"cover_url":                  @"cover_url",
     @"published_at":               @"published_at",
     @"issue_number":               @"issue_number"
     }];

    //Setting up objectmapping for article
    RKObjectMapping *articleMapping = [RKObjectMapping mappingForClass:[Article class]];
    [articleMapping addAttributeMappingsFromDictionary:@{
     @"title":                      @"title",
     @"main_text":                  @"main_text",
     @"article_image_url":          @"article_image_url"
     }];
    [issueMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"articles" toKeyPath:@"articles" withMapping:articleMapping]];

    Issue *issue = [[Issue alloc] init];
    RKMappingOperation* mapper = [[RKMappingOperation alloc] initWithSourceObject:[parsedData objectForKey:@"issue"] destinationObject:issue mapping:issueMapping];
    RKManagedObjectMappingOperationDataSource *mappingDS = [RKManagedObjectMappingOperationDataSource new];
    mapper.dataSource = mappingDS;
    [mapper performMapping:&parseError];
    NSLog(@"Parse error: %@", parseError);
    NSLog(@"Issue title: %@", issue.title);

错误:
    2013-01-30 13:07:42.486 uninkd[13722:907] *** Assertion failure in -[RKManagedObjectMappingOperationDataSource mappingOperation:targetObjectForRepresentation:withMapping:inRelationship:], /Users/holgersindbaek/Projects/Uninkd/Uninkd_IOS/Pods/RestKit/Code/CoreData/RKManagedObjectMappingOperationDataSource.m:232

    2013-01-30 13:07:42.487 uninkd[13722:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'RKManagedObjectMappingOperationDataSource must be initialized with a managed object context.'

如果我拿走数据源会出错:
    2013-01-30 13:27:32.601 uninkd[13754:907] *** Assertion failure in -[RKMappingOperation applyRelationshipMappings], /Users/holgersindbaek/Projects/Uninkd/Uninkd_IOS/Pods/RestKit/Code/ObjectMapping/RKMappingOperation.m:699
    2013-01-30 13:27:32.603 uninkd[13754:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot perform relationship mapping without a data source'

希望你能帮忙。

最佳答案

这是因为 RKManagedObjectMappingOperationDataSource 需要一个托管对象上下文,这是专门针对 Core Data 的。你在使用核心数据吗?

忘记我之前说过的一切并使用:

#import "RKObjectMappingOperationDataSource.h"
RKObjectMappingOperationDataSource *mappingDS = [RKObjectMappingOperationDataSource new];
mapper.dataSource = mappingDS;

代替:
RKManagedObjectMappingOperationDataSource *mappingDS = [RKManagedObjectMappingOperationDataSource new];
mapper.dataSource = mappingDS;

提示,如果您不使用 CoreData,请不要使用任何显示 ManagedObject 的类。这是指 ManagedObject 中的 ManagedObjectContext,它是 CoreData 独有的。话虽如此,核心数据 + restkit 很棒,您应该检查一下。

关于iphone - RestKit 0.20 : Map relational data from local file,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14610867/

10-14 22:35