【问题标题】:iOS - RestKit and clearing all data?iOS - RestKit 并清除所有数据?
【发布时间】:2011-11-04 16:53:26
【问题描述】:

我正在使用 RestKit 进行 Web 服务调用、缓存和 etags。 我实现了自己的 coredata 模型和 managedObjects

用户退出后,我需要清除数据库中的所有数据。 我能够成功删除 sqlite 文件并重新创建它,但我找不到清除所有 RestKit 捕获和 etag 数据的方法。 如何彻底擦除 RestKit 存储的所有数据?

【问题讨论】:

    标签: iphone objective-c ios restkit


    【解决方案1】:

    您想调用[[RKClient sharedClient].requestCache invalidateAll]; 来清除缓存。您可以查看API docs

    【讨论】:

    • 从 RestKit 0.20 开始,您需要使用 [[RKManagedObjectStore defaultStore] resetPersistentStores:nil];
    • 嗨 @blake-watters 如何在 0.20 版本中做到这一点
    【解决方案2】:

    使用 RKManagedObjectStore 类中的以下方法。

    - (void)deletePersistantStoreUsingSeedDatabaseName:(NSString *)seedFile

    http://restkit.org/api/0.9/Classes/RKManagedObjectStore.html#//api/name/deletePersistantStoreUsingSeedDatabaseName:

    【讨论】:

    • 我的种子文件是什么?我有自己的核心数据模型和上下文,我不会为此使用 restkit
    • 在这种情况下,您的问题在这里得到了回答:stackoverflow.com/questions/1077810/…
    • 我不认为他们将缓存和 etag 存储在同一个 sqlite 文件中。目前我删除我的 sqlite 并重新创建它,当我调用 web 服务时,RestKit 会返回以前缓存的数据,所以我最终会再次使用我的旧数据。
    【解决方案3】:

    在 Restkit 0.20 中 试试这个:

    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    

    为我工作 =)

    【讨论】:

      【解决方案4】:

      在 RestKit 0.20.2 中,以下示例可以解决问题。它基于文件 RKTestFactory.m 中的 RestKit/Testing 组件中的代码,并且在我的项目中运行良好。

      另外,如果 RestKit 正在管理您的 CoreData 堆栈,这是我的设置方式,请记住删除在您的 RestKit 设置中使用 NSManagedObjectContext 的任何 NSFetchedResultsController。

      - (void)tearDownRestKit
      {
          // Cancel any network operations and clear the cache
          [[RKObjectManager sharedManager].operationQueue cancelAllOperations];
          [[NSURLCache sharedURLCache] removeAllCachedResponses];
      
          // Cancel any object mapping in the response mapping queue
          [[RKObjectRequestOperation responseMappingQueue] cancelAllOperations];
      
          // Ensure the existing defaultStore is shut down
          [[NSNotificationCenter defaultCenter] removeObserver:[RKManagedObjectStore defaultStore]];
      
          // Not be needed if not using indexer
          if ([[RKManagedObjectStore defaultStore] respondsToSelector:@selector(stopIndexingPersistentStoreManagedObjectContext)]) {
              // Search component is optional
              [[RKManagedObjectStore defaultStore] performSelector:@selector(stopIndexingPersistentStoreManagedObjectContext)];
      
              if ([[RKManagedObjectStore defaultStore] respondsToSelector:@selector(searchIndexer)]) {
                  id searchIndexer = [[RKManagedObjectStore defaultStore] valueForKey:@"searchIndexer"];
                  [searchIndexer performSelector:@selector(cancelAllIndexingOperations)];
              }
          }
      
          [RKObjectManager setSharedManager:nil];
          [RKManagedObjectStore setDefaultStore:nil];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-24
        相关资源
        最近更新 更多