iphone - iOS RestKit what is the purpose of [mappingProvider setMapping:forKeyPath:] method? -
i'm working through restkit relationship mapping example , cannot understand purpose of these method calls, or if there's typo in calls. refer to? when object loader encounter content @ these key paths?
[objectmanager.mappingprovider setmapping:usermapping forkeypath:@"user"]; [objectmanager.mappingprovider setmapping:taskmapping forkeypath:@"task"]; [objectmanager.mappingprovider setmapping:projectmapping forkeypath:@"project"];
the json file being loaded data has 3 objects: project, tasks , user. note tasks plural.
there 3 entities defined in core data model: user, task , project. these start capital letters.
finally, nsmanagedobject classes derived data model have relationships: task>assigneduser , user>tasks , project being regular nsobject
should @"task" @"tasks"?
@implementation rkrelationshipmappingexample - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { rkobjectmanager* objectmanager = [rkobjectmanager objectmanagerwithbaseurl:grkcatalogbaseurl]; objectmanager.objectstore = [rkmanagedobjectstore objectstorewithstorefilename:@"rkrelationshipmappingexample.sqlite"]; rkmanagedobjectmapping* taskmapping = [rkmanagedobjectmapping mappingforclass:[task class]]; [taskmapping mapkeypath:@"id" toattribute:@"taskid"]; [taskmapping mapkeypath:@"name" toattribute:@"name"]; [taskmapping mapkeypath:@"assigned_user_id" toattribute:@"assigneduserid"]; taskmapping.primarykeyattribute = @"taskid"; //uniquely identifies record update purposes rkmanagedobjectmapping* usermapping = [rkmanagedobjectmapping mappingforclass:[user class]]; [usermapping mapattributes:@"name", @"email", nil]; [usermapping mapkeypath:@"id" toattribute:@"userid"]; usermapping.primarykeyattribute = @"userid"; [objectmanager.mappingprovider setmapping:usermapping forkeypath:@"user"]; [objectmanager.mappingprovider setmapping:taskmapping forkeypath:@"task"]; [usermapping maprelationship:@"tasks" withmapping:taskmapping]; [taskmapping maprelationship:@"assigneduser" withmapping:usermapping]; [taskmapping connectrelationship:@"assigneduser" withobjectforprimarykeyattribute:@"assigneduserid"]; // note - project not backed core data rkobjectmapping* projectmapping = [rkobjectmapping mappingforclass:[project class]]; [projectmapping mapkeypath:@"id" toattribute:@"projectid"]; [projectmapping mapattributes:@"name", @"description", nil]; [projectmapping maprelationship:@"user" withmapping:usermapping]; [projectmapping maprelationship:@"tasks" withmapping:taskmapping]; [objectmanager.mappingprovider setmapping:projectmapping forkeypath:@"project"]; } return self; } //more code @end
thank clarification!
the rkobjectmappingprovider setmapping:forkeypath:
method instructs mapping provider object mapping use when encounters data @ given key path. in case of example , associated json data, there no actual instances of task
keypaths -- in particular example, [objectmanager.mappingprovider setmapping:taskmapping forkeypath:@"task"]
statement not required; in case, might have like
"task":{"id":10,"name":"some task", "assigned_user_id":5}
in case would required. actual tasks in json stream mapped because of [projectmapping maprelationship:@"tasks" withmapping:taskmapping]
.
you can use rklogconfigurebyname()
inspect restkit doing -- useful. rklogconfigurebyname("restkit/objectmapping", rklogleveltrace)
in case give "blow-by-blow" of mapping logic.
Comments
Post a Comment