ios - Core data save entity issue -


this might primitive question. have list of states belongs different countries.

data 1: state - xyz         country -  data 2 : state - abc          country - b  data 3: state - ght         country -  data 2 : state - ase          country - b 

in core data have differnt entities state , country. while saving how put states country object?

edit : country state relation

enter image description here

assuming data array of dictionary,

for (nsdictionary *data in datas) {      nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init];     nsentitydescription *entity = [nsentitydescription entityforname:@"country" inmanagedobjectcontext:[dbmanager getcurrentcontext]];     [fetchrequest setentity:entity];      nspredicate *predicate = [nspredicate predicatewithformat:@"name = %@", [data valueforkey:@"country"]];     [fetchrequest setpredicate:predicate];      nserror *error;     nsarray * fetchedobjects = [managedobjectcontext executefetchrequest:fetchrequest error:&error];     if ([fetchedobjects count] > 0) {          country *country = [fetchedobjects objectatindex:0];         //using coredatageneratedaccessors         [country addstateobject:[data valueforkey:@"state"]];     } else {          country *country = [nsentitydescription insertnewobjectforentityforname:@"country" inmanagedobjectcontext:managedobjectcontext];         country.name = [data valueforkey:@"country"];         ...          if ([managedobjectcontext save:&error])         {              //using coredatageneratedaccessors              [country addstateobject:[data valueforkey:@"state"]];         }     } } 

hope may


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -