objective c - Clearing CoreData and all that inside -
i'm pretty new coredata , app uses it. , i'm working on feature clears entire core info when log out in app.
i have 2 sqllite files (for reason thought handy)
how can clear both files of info , reset them dataless state?
i've tried lot of ways, next guides, on so.
how clear/reset coredata in one-to-many realationship
how remove objects core data
they seem fail me. i'm wondering do wrong? , perhaps can explain me how reset 2 coredata files proper way.
edit:
//should clear whole coredata database. used logout mechanism -(void)resetcoredata { (nspersistentstore *store in self.persistentstorecoordinator.persistentstores) { // nspersistentstore *store = self.persistentstorecoordinator.persistentstores[0]; nserror *error; nsurl *storeurl = store.url; dlog(@"storeurl: %@", storeurl); nspersistentstorecoordinator *storecoordinator = self.persistentstorecoordinator; [storecoordinator removepersistentstore:store error:&error]; [[nsfilemanager defaultmanager] removeitematpath:storeurl.path error:&error]; dlog(@"there erreurs: %@", error); // [self adddefaultdata]; } _persistentstorecoordinator = nil; _managedobjectcontext = nil; _managedobjectmodel = nil; }
this doesn't seem clear coredata me.
edit2:
- (nsmanagedobjectcontext *)managedobjectcontext { if (_managedobjectcontext != nil) { homecoming _managedobjectcontext; } nspersistentstorecoordinator *coordinator = [self persistentstorecoordinator]; if (coordinator != nil) { _managedobjectcontext = [[nsmanagedobjectcontext alloc] init]; [_managedobjectcontext setpersistentstorecoordinator:coordinator]; } homecoming _managedobjectcontext; } - (nsmanagedobjectmodel *)managedobjectmodel { if (__managedobjectmodel != nil) { homecoming __managedobjectmodel; } nsurl *modelurl = [[nsbundle mainbundle] urlforresource:@"myname" withextension:@"momd"]; __managedobjectmodel = [[nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl]; homecoming __managedobjectmodel; } - (nspersistentstorecoordinator *)persistentstorecoordinator { if (__persistentstorecoordinator != nil) { homecoming __persistentstorecoordinator; } nsstring *storepath = [[self applicationdocumentsdirectory] stringbyappendingpathcomponent:@"myname.sqlite"]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; // if expected store doesn't exist, re-create default store. if (![filemanager fileexistsatpath:storepath]) { nsstring *defaultstorepath = [[nsbundle mainbundle] pathforresource:@"myname" oftype:@"momd"]; if (defaultstorepath) { [filemanager copyitematpath:defaultstorepath topath:storepath error:null]; } } __persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel: [self managedobjectmodel]]; //check see version of current model we're in. if it's >= 2.0, //then , check if migration has been performed... nsset *versionidentifiers = [[self managedobjectmodel] versionidentifiers]; dlog(@"which current version our .xcdatamodeld file set to? %@", versionidentifiers); if ([versionidentifiers containsobject:@"2.0"]) { bool hasmigrated = yes; if (hasmigrated==yes) { storepath = nil; storepath = [[self applicationdocumentsdirectory] stringbyappendingpathcomponent:@"myname2.sqlite"]; } } nsurl *storeurl = [nsurl fileurlwithpath:storepath]; nserror *error; nsdictionary *pscoptions = [nsdictionary dictionarywithobjectsandkeys:[nsnumber numberwithbool:yes], nsmigratepersistentstoresautomaticallyoption, [nsnumber numberwithbool:no], nsinfermappingmodelautomaticallyoption, nil]; if (![__persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:pscoptions error:&error]) { dlog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } homecoming __persistentstorecoordinator; }
edit 3: i'm still looking way reset coredata if deleted entire app , started again. usual ways of doing not working case. , there 2 sqllite files. there hints of migration took place @ point in application i'm not sure when , how. error logs show nil useful.
i'm not looking efficient way. way.
help me out , bounty yours.edit 4: final result: seemed legacy code had sec managedobjectcontext instantiated. moment retrieved , did flush-function it. both sqlite-files disappeared needed.
thanks set effort in problem.
try next method flush database, works perfect me.
-(void) flushdatabase{ [__managedobjectcontext lock]; nsarray *stores = [__persistentstorecoordinator persistentstores]; for(nspersistentstore *store in stores) { [__persistentstorecoordinator removepersistentstore:store error:nil]; [[nsfilemanager defaultmanager] removeitematpath:store.url.path error:nil]; } [__managedobjectcontext unlock]; __managedobjectmodel = nil; __managedobjectcontext = nil; __persistentstorecoordinator = nil; }
objective-c core-data reset
No comments:
Post a Comment