RestKit を使用して、次の JSON をコア データにマップしています。
コア データに多対多の関係が設定されています。
ホッパーは多くのユーザーを持つことができ、ユーザーは多くのホッパーを持つことができます。
次のエラー メッセージが表示されます。
(エンティティ: ホッパー; id: 0x767d240
hopperToUsers = <relationship fault: 0x8352960 'hopperToUsers' ;
したがって、私のユーザーは更新されていません。
json:
{
"hoppers": [{
"bearing": 0,
"created_at": "2013-07-26T07:57:00Z",
"distance": 0.0,
"id": 4,
"lat": "30.422032",
"lng": "-86.617069",
"name": "Fort Walton Beach",
"updated_at": "2013-07-26T07:57:00Z",
"users": [{
"avatar_url": null,
"created_at": "2013-07-26T21:37:21Z",
"id": 1,
"link": "http:/example.com/savetheday",
"name": "Clark Kent",
"post": " I once reported a heinous crime here. Superman came to the rescue!",
"thumbnail_url": null,
"updated_at": "2013-07-26T21:37:21Z"
}, {
"avatar_url": null,
"created_at": "2013-07-26T21:37:57Z",
"id": 2,
"link": "http:/example.com/villaincaught",
"name": "Lex Luther",
"post": " I got busted here. Almost took over the world!",
"thumbnail_url": null,
"updated_at": "2013-07-26T21:37:57Z"
}]
}]
}
私のコードは、RestKit README https://github.com/RestKit/RestKitの Managed Object Request の例に従っています。
パラメータの NSURL を変更する必要があることは認識していますが、ホッパーは問題なくマップされているようです。
`[managedObjectStore createManagedObjectContexts];
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
[userMapping addAttributeMappingsFromDictionary:@{ @"avatar_url": @"avatarURL" ,
@"created_at": @"createdAt",
@"link": @"jsonURL",
@"post": @"post",
@"thumbnail_url": @"thumbnailURL",
@"updated_at": @"updatedAt",
@"id": @"userID"}];
RKEntityMapping *hopperMapping = [RKEntityMapping mappingForEntityForName:@"Hopper" inManagedObjectStore:managedObjectStore];
[hopperMapping addAttributeMappingsFromDictionary:@{ @"id": @"hopperID",
@"lat": @"lat",
@"lng": @"lng",
@"name": @"name",
@"created_at": @"createdAt",
@"distance": @"distance"}];
[hopperMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"hopperToUsers" toKeyPath:@"hopperToUsers" withMapping:userMapping]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:hopperMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"hoppers" statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/hoppers.json?lat=30.422032&lng=-86.617069"]];
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = managedObjectStore.managedObjectCache;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
Hopper *hopper= [result firstObject];
NSLog(@"Mapped the hopper: %@", hopper);
NSLog(@"Mapped the user: %@", [hopper.hopperToUsers anyObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
NSOperationQueue *operationQueue = [NSOperationQueue new];
[operationQueue addOperation:operation];}`