objective c - RestKit 0.2 mapping Core Data <--> JSON with to-many relationships -
i've been battling restkit on week , can't seem head around relationship mapping @ all... i've tried reading documentation , examples, doesn't seem make sense when try , apply in practise.
i've temporarily abandoned app , reduced down simple standalone project, has following data model:
- a template owns collection of nodes.
- a subset of these nodes may referred 'links' within template.
the generated classes like:
@interface template : nsmanagedobject @property (nonatomic, retain) nsstring * guid; @property (nonatomic, retain) nsstring * name; @property (nonatomic, retain) nsset *links; @property (nonatomic, retain) nsset *nodes; @end @interface node : nsmanagedobject @property (nonatomic, retain) nsstring * desc; @property (nonatomic, retain) nsstring * guid; @property (nonatomic, retain) nsstring * name; @property (nonatomic, retain) template *template; @end
i trying use restkit mappings map core data model json, , json core data. there server component want use same mappings support import/export of json file.
here's example of how i'd json template:
{"nodes":[ {"name":"node 1","guid":"n-0001","desc":"this first node"}, {"name":"node 2","guid":"n-0002","desc":"this second node"}, {"name":"node 3","guid":"n-0003","desc":"this third node"} ], "guid":"t-0001", "name":"template 1", "links":["n-0001","n-0002"] }
here mappings i'm using:
self.nodemapping = [rkentitymapping mappingforentityforname:@"node" inmanagedobjectstore:self.rkmanagedobjectstore]; [self.nodemapping addattributemappingsfromdictionary:@{ @"guid": @"guid", @"name": @"name", @"desc": @"desc" }]; [self.nodemapping setidentificationattributes:@[@"guid"]]; self.templatemapping = [rkentitymapping mappingforentityforname:@"template" inmanagedobjectstore:self.rkmanagedobjectstore]; [self.templatemapping addattributemappingsfromdictionary:@{ @"guid": @"guid", @"name": @"name" }]; [self.templatemapping setidentificationattributes:@[@"guid"]]; [self.templatemapping addrelationshipmappingwithsourcekeypath:@"nodes" mapping:self.nodemapping];
this works 'nodes' relationship, can't life of me figure out how map links using ids.
i've tried using
[self.templatemapping addconnectionforrelationship:@"links" connectedby:@"guid"];
but leaves links property empty when import json above.
i've spent ages messing many different forms of relationship mapping , attribute mapping , results in either cryptic errors restkit, or nothing happening @ all.
can please tell me need add mapping links map correctly?
i plan use same mappings dump core data template out json. i'm hoping can use [self.templatemapping inversemapping] - i've given on until can import mapping working.
thanks
check section in restkit object mapping documentation: mapping values without key paths
i put quick sample following json structure:
rkentitymapping *linknodemapping = [rkentitymapping mappingforentityforname:@"node" inmanagedobjectstore:self.rkmanagedobjectstore]; [linknodemapping addpropertymapping:[rkattributemapping attributemappingfromkeypath:nil tokeypath:@"guid"]]; [linknodemapping setidentificationattributes:@[@"guid"]]; rkrelationshipmapping *linknodesrelationshipmapping = [rkrelationshipmapping relationshipmappingfromkeypath:@"links" tokeypath:@"links" withmapping:linknodemapping]; [self.templatemapping addpropertymapping:linknodesrelationshipmapping];
Comments
Post a Comment