如何在一对多关系中清除/重置所有CoreData [英] How to clear/reset all CoreData in one-to-many relationship
问题描述
我使用coreData,一对多的realtionship,我有一个文件夹实体和一个文件实体。一个文件夹可以有很多文件等等。
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.
所以,我有两个ViewControllers,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, / p>
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.
注意
Ranjit
Regards Ranjit
推荐答案
我已经解决了这个问题,下面是代码,
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中,我在resetbutton上以这种方式点击。
And in my SettingsViewController, I am calling this on resetbutton clicked in this way.
- (void)resetButtonclicked
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate resetApplicationModel];
}
尊敬
Ranjit。
Regards Ranjit.
这篇关于如何在一对多关系中清除/重置所有CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!