如何以一对多关系清除/重置所有核心数据

时间:2013-02-01 12:44:30

标签: ios ios5 core-data ios6 persistent-storage

我正在使用coreData,具有一对多的实现,我有一个文件夹实体和一个文件实体。文件夹可以包含许多文件,依此类推。

所以,我有两个ViewControllers,FolderViewController和FileViewController,它们分别包含文件夹和文件。现在我有一个modalView,可以从文件夹和文件viewcontroller访问。在这个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");
}

因此,当我关闭View时单击resetButton后,我收到此错误

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

那么如何解决这个问题。

此致 兰吉特

2 个答案:

答案 0 :(得分:6)

我已经解决了这个问题,下面是代码,

此功能已在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上调用它,点击这样。

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

此致 兰吉特。

答案 1 :(得分:1)

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