問題描述
我正在使用 coreData,具有一對多的關系,我有一個文件夾實體和一個文件實體.一個文件夾可以有很多文件等等.
I am using coreData, with one -to-many realtionship, I have a folder entity and a file entity. A folder can have many files and so on.
所以,我有兩個 ViewController,FolderViewController 和 FileViewController,它們分別包含文件夾和文件.現在我有一個 modalView,它可以從文件夾和文件 viewcontroller 訪問.在這個 VC 中,我有一個按鈕來重置所有數據.因此,當我單擊此按鈕時,我希望所有數據都應重置.
So, I have two ViewControllers, FolderViewController and FileViewController which contains folders and files respectively.Now I have a modalView , which is accesible from both folder and file viewcontroller. In this VC I have a button to Reset all Data. So when I click this I want all the data should reset.
我使用了這段代碼,這個函數寫在 appdelegate.m 中并從我的 VC 中調用.
I used this code,this function is written in appdelegate.m and called from my VC.
- (void)resetToDefault
{
NSError * error;
// retrieve the store URL
NSURL * storeURL = [[__managedObjectContext persistentStoreCoordinator] URLForPersistentStore:[[[__managedObjectContext persistentStoreCoordinator] persistentStores] lastObject]];
// lock the current context
[__managedObjectContext lock];
[__managedObjectContext reset];//to drop pending changes
//delete the store from the current managedObjectContext
if ([[__managedObjectContext persistentStoreCoordinator] removePersistentStore:[[[__managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])
{
// remove the file containing the data
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
//recreate the store like in the appDelegate method
[[__managedObjectContext persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];//recreates the persistent store
}
[__managedObjectContext unlock];
//that's it !
NSLog(@"buttonReset Pressed");
}
所以在我關閉視圖時單擊 resetButton 后,我收到此錯誤
So after clicking on resetButton when I close the View, I get this error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Object's persistent store is not reachable from this NSManagedObjectContext's coordinator'
那么如何解決這個問題.
So how to solve this.
問候蘭吉特
推薦答案
這個問題我已經解決了,下面是代碼,
I have solved this problem, below is the code,
這個函數已經寫在appdelegate.m中
This function has been written in appdelegate.m
- (void) resetApplicationModel
{
NSError *error;
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AppName.sqlite"];
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
for (NSManagedObject *ct in [self.managedObjectContext registeredObjects]) {
[self.managedObjectContext deleteObject:ct];
}
//Make new persistent store for future saves
if (![self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// do something with the error
}
}
在我的 SettingsViewController 中,我在以這種方式單擊的重置按鈕上調用它.
And in my SettingsViewController, I am calling this on resetbutton clicked in this way.
- (void)resetButtonclicked
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate resetApplicationModel];
}
問候蘭吉特.
這篇關于如何以一對多關系清除/重置所有CoreData的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!