如何清除/重置所有CoreData在一个一对多的关系(How to clear/reset all

2019-07-19 03:18发布

我使用coreData,与一对多realtionship,我有一个文件夹实体和文件实体。 文件夹可以有很多的文件等。

所以,我有两个ViewControllers,FolderViewController和FileViewController它包含文件夹和文件respectively.Now我有一个modalView,这从文件夹和文件视图 - 控制入店。 在这个VC我有一个按钮,可以将所有数据。 所以,当我点击这个我想所有的数据应该复位。

我用这个代码,这个功能是写在appdelegate.m从我的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当我关闭浏览后,我得到这个错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Object's persistent store is not reachable from this NSManagedObjectContext's coordinator'

那么如何解决这个问题。

问候兰吉特

Answer 1:

I have solved this problem, below is the code,

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
    }  
}

And in my SettingsViewController, I am calling this on resetbutton clicked in this way.

- (void)resetButtonclicked
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate resetApplicationModel];  
}  

Regards Ranjit.



Answer 2:

    NSPersistentStore *store = [self.persistentStoreCoordinator.persistentStores lastObject];
    NSError *error;
    NSURL *storeURL = store.URL;
    NSPersistentStoreCoordinator *storeCoordinator = self.persistentStoreCoordinator;
    [storeCoordinator removePersistentStore:store error:&error];
    [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
//    Then, just add the persistent store back to ensure it is recreated properly.    
    if (![self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }  


文章来源: How to clear/reset all CoreData in one-to-many relationship