ios - Magical Record import (next step) -
i've put next step in title not same problem previous question exact same title.
i have person
entity.
person -------- name - mappedkeyname: fullname email - mappedkeyname: emailaddress personid - mappedkeyname: id -------- photos
and photo
entity.
photo -------- image createdate - mappedkeyname: date photoid - mappedkeyname: id -------- owner (type person) - mappedkeyname: userid - relatedbyattribute: personid
there other objects relate person
, json these comes so...
{ objectid : blah, owner : { id : 12345asdfg, fullname : oliver, emailaddress : oliver@oliver.com } }
with json setup works import. person records don't exist (with id) created. , exist updated.
however, photos json object comes this...
{ id : thisisthephotoid, date : today, userid : 12345asdfg }
when objects come down magical record import stops when gets person import.
the code crashes at...
- (id) mr_relatedvalueforrelationship:(nsrelationshipdescription *)relationshipinfo { nsstring *lookupkey = [self mr_lookupkeyforrelationship:relationshipinfo]; return lookupkey ? [self valueforkeypath:lookupkey] : nil; // stops here. }
the value of lookupkey
@"personid".
printing out relationshipinfo @ breakpoint gives...
$6 = 0x1fd695e0 (<nsrelationshipdescription: 0x1fd695e0>), name owner, isoptional 0, istransient 0, entity photo, renamingidentifier owner, validation predicates (), warnings (), versionhashmodifier (null) userinfo { mappedkeyname = userid; relatedbyattribute = personid; }, destination entity person, inverserelationship photos, mincount 1, maxcount 1, isordered 0, deleterule 1
i have no idea why isn't working. don't sensible errors report.
magicalrecord cannot map relationship automatically json format:
{ id : thisisthephotoid, date : today, userid : 12345asdfg }
in order magicalrecord map relationship person
object, have object in json well, example:
{ id : thisisthephotoid, date : today, user : { userid : 12345asdfg } }
this way magicalrecord knows it's object , appropriate lookup in existing database person
record above id , map relationship.
so there 2 issues this, though. if cannot change json output have create category class on photo
manually map relationship yourself. i'll after second issue.
the second issue above json format assumes have parsed users , stored records in database. if have not magicalrecord create new person
record above id, since no other attributes exist on object (notice userid key attribute in dictionary) empty , not include name , email address. can extend json (if have possibility) include attributes in person dictionary inside photo dictionary:
{ id : thisisthephotoid, date : today, user : { userid : 12345asdfg, fullname : oliver, emailaddress : oliver@oliver.com } }
the json payload quite small doesn't hurt if can. plus create new person
record if 1 doesn't exist in database already.
and manual mapping. if cannot change json above format have manually override relationship mapping json not prepared way magicalrecord mapping.
create category class photo
called photo+mapping.h/.m
. stick +mapping
these. class should photo (mapping)
in header , implementation file , you're go.
magicalrecord has number of instance methods available override (see latter part of this article on magicalrecord importing written author of magicalrecord), among them import<;attributename>;:
, import<;relationshipname>;:
. there willimport:
, didimport:
, shouldimport:
methods on class allows override mapping.
for case can use import<;relationshipname>;:
or shouldimport:
. took these 2 because 1 has bit of benefit depending on whether have mapped person
objects , they're available relationship mapping on photo
object.
here examples of can (you can choose combine few of them if wish, doesn't hurt so). note here: always use current nsmanagedobjectcontext
when overriding mapping (easily accessible magicalrecord through self.managedobjectcontext
) otherwise end context issues.
be sure import person:
#import "photo+mapping.h" #import "person.h" // assuming want import photo object if have person stored great method tell magicalrecord whether continue importing or not -(bool)shouldimport:(id)data { person *person = [person findfirstbyattribute:data[@"userid"] value:@"personid" incontext:self.managedobjectcontext]; if (!person) { // no person object exists don't import photo object - again since might want create record if not return no; } // can set relationship here (you might well) or use importperson: method below (doing second lookup, unnecessary @ point) [self setperson:person]; return yes; } // if use method you're doing lookup check whether record exist when magicalrecord trying map person relationship -(void)importperson:(id)data { person *person = [person findfirstbyattribute:data[@"userid"] value:@"personid" incontext:self.managedobjectcontext]; if (!person) { // if no person record exists associated userid, should create 1 (or not - if choose not to, it's wise throw away photo object) person = [person createincontext:self.managedobjectcontext]; [person setpersonid:data[@"userid"]]; } // set relationship [self setperson:person]; } // can use following method when magicalrecord done mapping , rid of photo object if person relationship nil: -(void)didimport:(id)data { if (!self.person) { [self deleteincontext:self.managedobjectcontext]; } }
hope helps! let me know if have questions.
Comments
Post a Comment