ios - Multiple ObjectContext for multiple dataModels? -
i have 3 datamodels defined in application. question is, if need 3 different contexts? 3 different sqlite files?
at moment every time save record database following:
managedobjectcontext = [[appdelegate sharedappdelegate] getmanagedobjectcontextwithdb:@"testdb.sqlite"]; or if save record other datamodel do:
managedobjectcontext = [[appdelegate sharedappdelegate] getmanagedobjectcontextwithdb:@"testdb2.sqlite"]; so right way? or have have 1 context 3 models?
here other methods:
- (nsmanagedobjectcontext *)getmanagedobjectcontextwithdb:(nsstring *)db { if (_managedobjectcontext != nil) { homecoming _managedobjectcontext; } nspersistentstorecoordinator *coordinator = [self persistentstorecoordinatorwithdb:db]; if (coordinator != nil) { _managedobjectcontext = [[nsmanagedobjectcontext alloc] init]; [_managedobjectcontext setpersistentstorecoordinator: coordinator]; } homecoming _managedobjectcontext; } - (nsmanagedobjectmodel *)managedobjectmodel { if (_managedobjectmodel != nil) { homecoming _managedobjectmodel; } _managedobjectmodel = [nsmanagedobjectmodel mergedmodelfrombundles:nil]; homecoming _managedobjectmodel; } - (nspersistentstorecoordinator *)persistentstorecoordinatorwithdb:(nsstring *)db { if (_persistentstorecoordinator != nil) { homecoming _persistentstorecoordinator; } nsurl *storeurl = [nsurl fileurlwithpath: [[self applicationdocumentsdirectory] stringbyappendingpathcomponent: db]]; nsdictionary *options = @{ nsmigratepersistentstoresautomaticallyoption : @yes, nsinfermappingmodelautomaticallyoption : @yes }; nserror *error = nil; _persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self managedobjectmodel]]; if(![_persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:options error:&error]) { /*error store creation should handled in here*/ } homecoming _persistentstorecoordinator; }
if info model mean model file containing entities , relationships(file extension .xcdatamodel) believe reply yes. interact info model set persistent store coordinator , instantiate model (as have done). interact managed objects create managed object context (which have done). since not possible going assume have 1 model file , instead have 3 seperate stores derived model.
since interested in establishing several stores corresponding same model, illustration if have default info store comes pre-populated , user populated store, totally possible. need phone call addpersistentstorewithtype:configuration: 1 time again on coordinator. there plenty of so posts on won't go in depth.
the 1 thing need consider setting several stores mean need watch performance (you on mobile device after all). beingness said don't need manage 3 different contexts, need ensure stores added same persistent store coordinator context communicates with. coordinator work figure out store read , write to.
i hope helps!
edit
following comments came lite original problem indeed revolve around maintaining separate managed object model files , separate persistent store coordinators. op having issues regarding updates , inserts of these entities across contexts. realized problem might fact mo's beingness accessed outside of contexts , solution problem involves wrapping updates mo in [nsmanagedobjectcontext performblock:] method. gives thread safe means of accessing mo's.
a word of caution, setting 3 separate models complex solution , might not necessary in scenarios. having separate models means there no way have relationships entities in other models. suggestion beginners utilize 1 model file , if of import have separate stores can utilize [nsmanagedobjectmodel mergedmodelfrombundles:] method create separate stores whilst still talking same coordinator. @ end of day, 1 of powerful features of core info relationship management , setting separate models limits in many ways.
ios iphone objective-c core-data ios7
No comments:
Post a Comment