ios - Core Data: is there a way to properly change the persistent store at runtime? -
i have problem when trying change persistent store @ runtime. way i'm trying doing reinit of whole core data stack:
- (void)initializecoredataforstore:(nsurl *)storeurl withcompletionblock:(coredatarwcompletionblock)completionblock { //this code needed reset core data stack before re-creating new 1 [_managedobjectcontextbackgroundqueue reset]; _managedobjectcontextbackgroundqueue = nil; [_managedobjectcontextmainqueue reset]; _managedobjectcontextmainqueue = nil; _managedobjectmodel = nil; _persistentstorecoordinator = nil; _persitentstore = nil; //---------------end reset core data stack --------------------- //model self.managedobjectmodel = [nsmanagedobjectmodel mergedmodelfrombundles:nil]; self.persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:self.managedobjectmodel]; //main context self.managedobjectcontextmainqueue = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsmainqueueconcurrencytype]; [self.managedobjectcontextmainqueue setpersistentstorecoordinator:self.persistentstorecoordinator]; //background context self.managedobjectcontextbackgroundqueue = [[nsmanagedobjectcontext alloc] initwithconcurrencytype:nsprivatequeueconcurrencytype]; [self.managedobjectcontextbackgroundqueue performblockandwait:^{ [self.managedobjectcontextbackgroundqueue setpersistentstorecoordinator:self.persistentstorecoordinator]; }]; dispatch_queue_t queue = null; queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(queue, ^{ nserror *error = nil; self.persitentstore = [self.persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:nil error:&error]; dispatch_sync(dispatch_get_main_queue(), ^{ if (completionblock) { completionblock([error localizeddescription], nil); } }); }); }
all work on background context made performblock or performblockandwait. problem that, event if reset , reinit stack, if there pending "operations" on old background context, operations not canceled, , continue execute, more this, trying execute using newly instantiated background context, has persistent store. there way cancel operations private queue associated background context(the way nsoperations can canceled on operation queue)?
Comments
Post a Comment