ios - Save to more than one entity -


for detail view let user leave notes each item. app data-driven website. in web version, web app stores notes in separate table field itemid.

in core data have entity of items , entity of notes. notes entity has attribute called itemid. when user creates note first time, stores itemid in note record.

my question when pull item editing how can simultaneously pull right note based on note having itemid?

in database situation join, or web page make 2 separate requests 2 tables flummoxed how core data.

do have put relationship note , therefore have noteid in item row?

if able access note attribute using item object?

thanks in advance suggestions.

this using save information. don't know how make sure i'm saving right note.

self.managedobjectcontext = [idmodel sharedinstance].managedobjectcontext; nsstring *notetext = _notesview.text; nsnumber *itemid = self.item.itemid; // populate record [self.note setvalue:notetext forkey:@"note"]; [self.note setvalue:itemid forkey:@"itemid"];  model (simplified):  item: name nsstring itemid: integer 64  note: note nsstring noteid: integer 64 itemid: integer 64    edit: 

code try link note , item while creating both...

//in save method         // create entity         nsentitydescription *entity = [nsentitydescription entityforname:@"notes" inmanagedobjectcontext:self.managedobjectcontext];          // initialize new record ie newnote         nsmanagedobject *record = [[nsmanagedobject alloc] initwithentity:entity insertintomanagedobjectcontext:self.managedobjectcontext];          // populate record         [record setvalue:note forkey:@"note"];         [record setvalue:localid forkey:@"localnid"];          // save record         nserror *error = nil;          if ([self.managedobjectcontext save:&error]) { //   if note saved, save new item…              if (itemlength>1) {             items *newitem = [nsentitydescription insertnewobjectforentityforname:@“item” inmanagedobjectcontext:self.managedobjectcontext];             newitem.item = item;               newitem.note = self.note //this null note not seem pick newly created note.             if (![self.managedobjectcontext save:&error]) {                 nslog(@"error: %@", [error localizeddescription]);              }              } 

yes, should use relationship between item , note entities. create relationship ctrl-drag 1 entity other in data model editor. note xcode automatically adds inverse relationship:

screenshot of erd

i've renamed relationships clarity - can tailor details of relationship (name, one-one v one-many, delete rule, etc) in panel on right. in example above, item entity has 3 properties: 2 attributes , 1 relationship. given item object, myitem, values these properties can accessed using key value coding methods: valueforkey: , setvalue:forkey:. example, if attribute defined string:

 nsstring *mystringvalue = [myitem valueforkey:@"attribute"];  [myitem setvalue:@"new value attribute" forkey:@"attribute"]; 

that's long-winded. make life easier, use "create nsmanagedobject subclass..." option. xcode configure each entity subclass of nsmanagedobject , create new class files (.h/.m or .swift) details of properties. example item:

@property (nullable, nonatomic, retain) nsstring *attribute; @property (nullable, nonatomic, retain) nsstring *attribute1; @property (nullable, nonatomic, retain) note *note; 

the thing realise note relationship of class note. it's not foreign key, or noteid, have use lookup corresponding note object. is note object. under hood, coredata adding primary keys , foreign keys underlying tables, aggravation abstracted away.

having created subclasses, can use dot-accessors object properties:

nsstring *mystringvalue = myitem.attribute; myitem.attribute = @"new value attribute";  

for relationship, if have item object called myitem , note object called mynote, can set relationship value with:

myitem.note = mynote; 

or equivalently:

mynote.item = myitem; 

(note: use 1 or other, not both; coredata automatically sets inverse relationships you).

now, have added complication of web server item objects , note objects downloaded. , on web server, notes table has field itemid, used link items , notes. @ point, want link note objects , item objects using itemid. usual approach once (as coredata objects synchronised server), set relationship accordingly, , thenceforth use relationship rather itemid note given item. example, if creating new note object, , itemid server "1234", might this:

nsfetchrequest *fetchrequest = [nsfetchrequest fetchrequestwithentityname:@"item"]; fetchrequest.predicate = [nspredicate predicatewithformat:@"itemid == %@", @"1234"]; nserror *error; nsarray *results = [context executefetchrequest:fetchrequest error:&error]; // should check nil results/error if (results.count > 0) {     // found (at least) 1 item itemid == @"1234"     // use first set relationship     newnote.item = results[0]; } 

then whenever have particular item object, can access corresponding note using

note *mynote = myitem.note; 

furthermore, can cascade dot-notation, value of attribute note myitem, use:

nsstring *notetext = myitem.note.attribute; 

edit

your save method close: either set self.note = record before save, or use newitem.note = record:

    //in save method     // create entity     nsentitydescription *entity = [nsentitydescription entityforname:@"notes" inmanagedobjectcontext:self.managedobjectcontext];      // initialize new record ie newnote     nsmanagedobject *record = [[nsmanagedobject alloc] initwithentity:entity insertintomanagedobjectcontext:self.managedobjectcontext];      // populate record     [record setvalue:note forkey:@"note"];     [record setvalue:localid forkey:@"localnid"];      // save record     nserror *error = nil;      if ([self.managedobjectcontext save:&error]) {     //   if note saved, save new item…         if (itemlength>1) {             items *newitem = [nsentitydescription insertnewobjectforentityforname:@“item” inmanagedobjectcontext:self.managedobjectcontext];             newitem.item = item;             newitem.note = record;             if (![self.managedobjectcontext save:&error]) {                 nslog(@"error: %@", [error localizeddescription]);             }         }     } 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -