4

我正在获取一个本地 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'

希望你能帮忙。

4

1 回答 1

5

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

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

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

代替:

RKManagedObjectMappingOperationDataSource *mappingDS = [RKManagedObjectMappingOperationDataSource new];
mapper.dataSource = mappingDS;

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

于 2013-01-30T18:22:49.913 回答