我想通過刪除應用程序的sqlite文件重置數據存儲清理。我在數據輔助類寫了這個功能:核心數據:爲什麼刪除sqlite文件並沒有立即清除以前的所有數據?
-(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;
}
我把這個的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]];
而是一組由功能的populateTestData創建的數據,我可以清楚地看到三組數據(因爲我稱爲函數三次)
顯然,resetPersistenStore()的作品,因爲沒有它,數據將繼續積累。
所以我的問題是:
爲什麼重置不會立即生效?
我已經設置managedObjectContext爲零在重置功能,但它沒有幫助。
這裏是我的populateTestData功能
+(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");
}
}
我在managedObjectModel_ = nil之前添加了[managedObjectContext reset]但它沒有任何影響 – 2011-02-19 07:08:39