2011-08-26 55 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]; 
}