我正在获取一个本地 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'
希望你能帮忙。