gpt4 book ai didi

iphone - RestKit 0.20 : Map relational data from local file

转载 作者:可可西里 更新时间:2023-11-01 05:37:10 26 4
gpt4 key购买 nike

我正在获取本地 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 的类。这是指 ManagedObjectContext 中的 ManagedObject,这是 CoreData 独有的。话虽这么说,核心数据 + restkit 很棒,你应该检查一下。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com