如何清除/重置所有CoreData中的一对多关系

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

所以,我有两个ViewControllers,FolderViewController和FileViewController分别包含文件夹和文件。现在我有一个modalView,这是从文件夹和文件viewcontroller访问。 在这个VC中,我有一个button来重置所有数据。 所以当我点击这个我想所有的数据应该重置。

我用这个代码,这个函数是写在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"); } 

所以当我closures视图后点击resetButton,我得到这个错误

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

那么如何解决这个问题。

关心Ranjit

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

这个函数已经写在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,我打这个resetbuttonton按这种方式。

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

关心Ranjit。

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