iphone - Core data: Why removing the sqlite file did not clear all the previous data insert immediately? -
i want reset data store clean removing app's sqlite file. wrote function in data helper class:
-(void) resetpersistenstore { nserror *error = nil; [persistentstorecoordinator_ release]; nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:@"myapp.sqlite"]; [[nsfilemanager defaultmanager] removeitematurl:storeurl error:&error]; if (error) { nslog(@"unresolved error %@, %@", error, [error userinfo]); } persistentstorecoordinator_ = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self managedobjectmodel]]; if (![persistentstorecoordinator_ addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl options:nil error:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } managedobjectmodel_ = nil; }
i put following test in uiapplication::didfinishlaunchingwithoptions
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { /* test */ [testdatahelper populatetestdata:[self managedobjectcontext]]; [testdatahelper populatetestdata:[self managedobjectcontext]]; [self resetpersistenstore]; [testdatahelper populatetestdata:[self managedobjectcontext]]; [testdatahelper testpopulatetestdata:[self managedobjectcontext]];
instead of 1 set of data created function populatetestdata, can see 3 set of data (because called function 3 times)
it clear resetpersistenstore() works, because without it, data keep accumulating.
so question is:
why reset not take effect immediately?
i have set managedobjectcontext nil in reset function, did not help.
here populatetestdata function
+(void)populatetestdata:(nsmanagedobjectcontext*)managedobjectcontext { stock* s1 = (stock*)[nsentitydescription insertnewobjectforentityforname:@"stock" inmanagedobjectcontext:managedobjectcontext]; s1.code = @"abc2"; s1.name = @"abc"; s1.enteredby = @"akong"; [managedobjectcontext save:&error]; if (error) { nslog(@"data error %@", [error description]); } else { nslog(@"init completed"); } }
even though context saved data in populatetestdata
, still has data loaded in it.
suppose when application starts, file has 3 objects. first populatetestdata
message adds object context, , save it. 4 objects in file, 1 in context. after second message, 5 in file, 2 in context. file gone, still there 2 in context. final message adds object context , file, there 3 objects.
you said setting managedobjectcontext nil in reset function didn't help. doubt if context has been reset there. try [managedobjectcontext reset] instead.
Comments
Post a Comment