objective c - RestKit nested relationships -
working restkit, i'm trying map following json.
{ "userid": 1, "collections": [ { "collectionid": 120, "friends": [ { "friendid": 6, "orders": [ { "orderid": 1, "name": "small" } ] } ] }, { "collectionid": 123, "friends": [ { "friendid": 6, "orders": [ { "orderid": 2, "name": "medium" } ] } ] } ] }
i'm utilising restkit , magicalrecord - setup code, mapping , relationships below
nsurl *modelurl = [nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:@"model" oftype:@"momd"]]; nsmanagedobjectmodel *managedobjectmodel = [[[nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl] mutablecopy]; rkmanagedobjectstore *managedobjectstore = [[rkmanagedobjectstore alloc] initwithmanagedobjectmodel:managedobjectmodel]; nsstring *storepath = [rkapplicationdatadirectory() stringbyappendingpathcomponent:@"db.sqlite"]; [managedobjectstore addsqlitepersistentstoreatpath:storepath fromseeddatabaseatpath:nil withconfiguration:nil options:nil error:nil]; [managedobjectstore createmanagedobjectcontexts]; // configure magicalrecord use restkit's core data stack [nspersistentstorecoordinator mr_setdefaultstorecoordinator:managedobjectstore.persistentstorecoordinator]; [nsmanagedobjectcontext mr_setrootsavingcontext:managedobjectstore.persistentstoremanagedobjectcontext]; [nsmanagedobjectcontext mr_setdefaultcontext:managedobjectstore.mainqueuemanagedobjectcontext]; rkobjectmanager *objectmanager = [rkobjectmanager managerwithbaseurl:[nsurl urlwithstring:@"http://127.0.0.1:3500"]]; objectmanager.managedobjectstore = managedobjectstore; rkentitymapping *appmapping = [rkentitymapping mappingforentityforname:@"app" inmanagedobjectstore:managedobjectstore]; appmapping.identificationattributes = @[ @"userid" ]; rkentitymapping *collectionmapping = [rkentitymapping mappingforentityforname:@"collection" inmanagedobjectstore:managedobjectstore]; collectionmapping.identificationattributes = @[ @"collectionid" ]; [collectionmapping addattributemappingsfromarray:@[@"collectionid"]]; rkentitymapping *friendmapping = [rkentitymapping mappingforentityforname:@"friend" inmanagedobjectstore:managedobjectstore]; friendmapping.identificationattributes = @[ @"friendid" ]; [friendmapping addattributemappingsfromarray:@[@"friendid"]]; rkentitymapping *ordermapping = [rkentitymapping mappingforentityforname:@"order" inmanagedobjectstore:managedobjectstore]; ordermapping.identificationattributes = @[ @"orderid" ]; [ordermapping addattributemappingsfromarray:@[@"orderid", @"name"]]; rkrelationshipmapping *appcollectionrelationship = [rkrelationshipmapping relationshipmappingfromkeypath:@"collections" tokeypath:@"collections" withmapping:collectionmapping]; [appmapping addpropertymapping:appcollectionrelationship]; rkrelationshipmapping *collectionfriendrelationship = [rkrelationshipmapping relationshipmappingfromkeypath:@"friends" tokeypath:@"friends" withmapping:friendmapping]; [collectionmapping addpropertymapping:collectionfriendrelationship]; rkrelationshipmapping *friendsorderrelationship = [rkrelationshipmapping relationshipmappingfromkeypath:@"orders" tokeypath:@"orders" withmapping:ordermapping]; [friendsorderrelationship setassignmentpolicy:rkunionassignmentpolicy]; [friendmapping addpropertymapping:friendsorderrelationship];
then, querying /test route on api (which outputs json block above) below console out...
rkresponsedescriptor *responsedescriptor = [rkresponsedescriptor responsedescriptorwithmapping:appmapping method:rkrequestmethodany pathpattern:nil keypath:nil statuscodes:rkstatuscodeindexsetforclass(rkstatuscodeclasssuccessful)]; [objectmanager addresponsedescriptor:responsedescriptor]; [objectmanager getobjectsatpath:@"/test" parameters:nil success:^(rkobjectrequestoperation *operation, rkmappingresult *mappingresult) { app *app = [mappingresult firstobject]; (collection *collection in app.collections) { nslog(@"collectionid = %@", collection.collectionid); (friend *friend in collection.friends) { nslog(@"friendid = %@", friend.friendid); (order *order in friend.orders) { nslog(@"name = %@", order.name); } } } } failure:^(rkobjectrequestoperation *operation, nserror *error) {}];
provides following output - notice both name's medium, not 1 small , other medium.
collectionid = 120 friendid = 6 name = medium name = small collectionid = 123 friendid = 6 name = medium name = small
why happening? i'm going guess it's because both friend id's same, though they're under different collections...
this because have identificationattributes = @[ @"friendid" ];
, default relationship connection rule relationships rather replacement. so, second mapping finds friend created first mapping , replaces order relationship contents.
you can't realistically change unique identifier based on json show, might want examine (in relation object graph requirements).
also / alternatively, see this answer details of how change relationship assignment policy original relationship contents aren't lost.
Comments
Post a Comment