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
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
Post a Comment