ios - Core Data initialisation -



ios - Core Data initialisation -

i'm new coredata have basic question have providers entity, in view controller @ viewdidload implement method:

-(nsdictionary *) allproviders { providers *orangefr = [nsentitydescription insertnewobjectforentityforname:@"providers" inmanagedobjectcontext:self.managedobjectcontext]; orangefr.name = [nsstring stringwithformat:@"orange"]; orangefr.adress = [nsstring stringwithformat:@"champ de mars"]; providers *sfrfr = [nsentitydescription insertnewobjectforentityforname:@"providers" inmanagedobjectcontext:self.managedobjectcontext]; sfrfr.name = [nsstring stringwithformat:@"sfr"]; sfrfr.adress = [nsstring stringwithformat:@"sfr adress"]; nsarray *frproviders = @[orangefr, sfrfr]; providers *att = [nsentitydescription insertnewobjectforentityforname:@"providers" inmanagedobjectcontext:self.managedobjectcontext]; beelineru.name = [nsstring stringwithformat:@"att"]; beelineru.adress = [nsstring stringwithformat:@"att adress"]; providers *verizon = [nsentitydescription insertnewobjectforentityforname:@"providers" inmanagedobjectcontext:self.managedobjectcontext]; mtsru.name = [nsstring stringwithformat:@"verizon"]; mtsru.adress = [nsstring stringwithformat:@"verizon adress"]; nsarray *usproviders = @[att, verizon]; _allproviders = @{@"france": frproviders, @"usa":usproviders}; homecoming _allproviders; }

so works fine, @ sec launch of application need check if there's filled provider entity. how that, _allproviders in both case?

create nsmutabledictionary:

nsmutabledictionary *providersbycountry = [nsmutabledictionary dictionary]

fetch entities:

nsfetchrequest *request = [[nsfetchrequest alloc] init]; request.entity = [nsentitydescription entityforname:@"providers" inmanagedobjectcontext:self.managedobjectcontext]; nsarray *providers = [self.managedobjectcontext executefetchrequest:request error:null];

insert providers dictionary (i'm assuming have country property on providers entity... have one?):

for (providers *provider in providers) { nsarray *providersforthiscountry = [providersbycountry[provider.country] if (!providersforthiscountry) { providersforthiscountry = [nsarray array]; } providersforthiscountry = [providersforthiscountry arraybyaddingobject:provider]; providersbycountry[provider.country] = providersforthiscountry; }

finally, create immutable re-create of nsmutabledictionary, , homecoming it:

return providersbycountry.copy;

since potentially slow operation, might want cache result in private @property — but you'll need clear cache somehow whenever modify provider list.

ios objective-c xcode core-data

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -