How to clear/reset all CoreData in one-to-many relationship

家住魔仙堡 提交于 2019-12-17 16:41:39

问题


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.

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.

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");
}

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.

Regards Ranjit


回答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.




回答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();
    }  


来源:https://stackoverflow.com/questions/14646595/how-to-clear-reset-all-coredata-in-one-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!