iOS - RestKit and clearing all data?

这一生的挚爱 提交于 2020-01-12 05:07:46

问题


I am using RestKit for webservice calls, caching, and etags. I implemented my own coredata model and managedObjects

As soon as the user signs out I need to clear all data in the database. I was able to successfully delete the sqlite file and recreate it, but I can't find out a way to clear all RestKit catching and etag data. How can I completely wipe all data stored by RestKit?


回答1:


You want to call [[RKClient sharedClient].requestCache invalidateAll]; to wipe the cache clean. You can view the API docs.




回答2:


Use the following method from the RKManagedObjectStore class.

- (void)deletePersistantStoreUsingSeedDatabaseName:(NSString *)seedFile

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




回答3:


In Restkit 0.20 try this:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

worked for me =)




回答4:


In RestKit 0.20.2 the following example does the trick. Its based off code found in the RestKit/Testing component in the file RKTestFactory.m and has worked great in my project.

Also, If RestKit is managing your CoreData stack, which is how mine is set up, remember to delete any NSFetchedResultsController that are using the NSManagedObjectContext in your RestKit setup.

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


来源:https://stackoverflow.com/questions/7208078/ios-restkit-and-clearing-all-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!