<tfoot id='tm2Ae'></tfoot>

        <small id='tm2Ae'></small><noframes id='tm2Ae'>

          <bdo id='tm2Ae'></bdo><ul id='tm2Ae'></ul>
      1. <legend id='tm2Ae'><style id='tm2Ae'><dir id='tm2Ae'><q id='tm2Ae'></q></dir></style></legend>
        <i id='tm2Ae'><tr id='tm2Ae'><dt id='tm2Ae'><q id='tm2Ae'><span id='tm2Ae'><b id='tm2Ae'><form id='tm2Ae'><ins id='tm2Ae'></ins><ul id='tm2Ae'></ul><sub id='tm2Ae'></sub></form><legend id='tm2Ae'></legend><bdo id='tm2Ae'><pre id='tm2Ae'><center id='tm2Ae'></center></pre></bdo></b><th id='tm2Ae'></th></span></q></dt></tr></i><div id='tm2Ae'><tfoot id='tm2Ae'></tfoot><dl id='tm2Ae'><fieldset id='tm2Ae'></fieldset></dl></div>

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

        时间:2023-06-01
        • <bdo id='EJ4Qu'></bdo><ul id='EJ4Qu'></ul>

          • <i id='EJ4Qu'><tr id='EJ4Qu'><dt id='EJ4Qu'><q id='EJ4Qu'><span id='EJ4Qu'><b id='EJ4Qu'><form id='EJ4Qu'><ins id='EJ4Qu'></ins><ul id='EJ4Qu'></ul><sub id='EJ4Qu'></sub></form><legend id='EJ4Qu'></legend><bdo id='EJ4Qu'><pre id='EJ4Qu'><center id='EJ4Qu'></center></pre></bdo></b><th id='EJ4Qu'></th></span></q></dt></tr></i><div id='EJ4Qu'><tfoot id='EJ4Qu'></tfoot><dl id='EJ4Qu'><fieldset id='EJ4Qu'></fieldset></dl></div>

            <small id='EJ4Qu'></small><noframes id='EJ4Qu'>

              • <legend id='EJ4Qu'><style id='EJ4Qu'><dir id='EJ4Qu'><q id='EJ4Qu'></q></dir></style></legend>
                  <tbody id='EJ4Qu'></tbody>
                <tfoot id='EJ4Qu'></tfoot>

                  本文介绍了如何以一对多关系清除/重置所有CoreData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 coreData,具有一对多的关系,我有一个文件夹实体和一个文件实体.一个文件夹可以有很多文件等等.

                  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.

                  所以,我有两个 ViewController,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 后,我收到此错误

                  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.

                  问候兰吉特

                  推荐答案

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

                  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 中,我在以这种方式单击的重置按钮上调用它.

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

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

                  问候兰吉特.

                  这篇关于如何以一对多关系清除/重置所有CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 iOS 5.1 中以 MM-dd-yy 格式显示选取器视图 下一篇:我应该担心苹果将在 iOS6 中停止使用谷歌地图的

                  相关文章

                  最新文章

                  <small id='VJyjZ'></small><noframes id='VJyjZ'>

                  1. <tfoot id='VJyjZ'></tfoot>
                    <legend id='VJyjZ'><style id='VJyjZ'><dir id='VJyjZ'><q id='VJyjZ'></q></dir></style></legend>
                    <i id='VJyjZ'><tr id='VJyjZ'><dt id='VJyjZ'><q id='VJyjZ'><span id='VJyjZ'><b id='VJyjZ'><form id='VJyjZ'><ins id='VJyjZ'></ins><ul id='VJyjZ'></ul><sub id='VJyjZ'></sub></form><legend id='VJyjZ'></legend><bdo id='VJyjZ'><pre id='VJyjZ'><center id='VJyjZ'></center></pre></bdo></b><th id='VJyjZ'></th></span></q></dt></tr></i><div id='VJyjZ'><tfoot id='VJyjZ'></tfoot><dl id='VJyjZ'><fieldset id='VJyjZ'></fieldset></dl></div>
                      <bdo id='VJyjZ'></bdo><ul id='VJyjZ'></ul>