2011-08-26 54 views
10

我正在使用RestKit进行web服务调用,缓存和etags。 我实现了我自己的coredata模型和managedObjectsiOS - RestKit并清除所有数据?

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

回答

14

要拨打[[RKClient sharedClient].requestCache invalidateAll];擦拭缓存清理。您可以查看API docs

+8

作为RestKit 0.20您将需要使用'[[RKManagedObjectStore defaultStore] resetPersistentStores:nil];' –

+0

使用RestKit 0.20为我工作http://stackoverflow.com/a/18425303/1318202 –

+0

嗨@ blake-watters如何在0.20版本中做到这一点 –

4

使用RKManagedObjectStore类中的以下方法。

- (void)deletePersistantStoreUsingSeedDatabaseName:(NSString *)seedFile

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

+0

什么是我的种子文件?我有我自己的核心数据模型和上下文,我不使用restkit – aryaxt

+1

在这种情况下,你的问题在这里得到了解答:http://stackoverflow.com/questions/1077810/delete-reset-all-entries-in-核心数据 –

+0

我不认为他们在同一个sqlite文件中存储缓存和etags。目前我删除我的sqlite并重新创建它,当我调用web服务时,RestKit返回以前的缓存数据,所以我最终重新获得旧数据。 – aryaxt

2

在Restkit 0.20 试试这个:

[[NSURLCache sharedURLCache] removeAllCachedResponses]; 

为我工作=)

0

在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]; 
}