简体   繁体   中英

iOS app: When should I clear my cache data? (using Realm)

In my iOS app, I'm saving specific data (Realm DB containing cached models) on disk, in the cache directory of the device. The cache data increases when the user uses the app, so I would like to clear old cache data regularly.

The problem is that I don't know how to check if the models I'm deleting are used by the current view. (By trying to delete all models, I got an error telling that some model objects used by the current view were invalidated.)

To ensure that the cached models I'm deleting are not currently used, I'm currently cleaning the cache when the app terminates:

- (void)applicationWillTerminate:(UIApplication *)application {
    [[ModelManager sharedInstance] deleteUnusedCacheModels];
}

Sample code in deleteUnusedCacheModels:

        NSDate* limitDate = [NSDate dateWithTimeIntervalSinceNow:-(60.0 * 60.0 * 24.0 * 7.0)];
        RLMResults* modelProductListArray = [ModelProductList objectsInRealm:_cacheDb
                                                                       where:@"modelUpdate < %@ || modelDelete = %@", limitDate, @YES];
        [_cacheDb deleteObjects:modelProductListArray];

But this code won't be executed if the user doesn't often terminate the app. (Most user leaves apps in background.)

Is there a better way for cleaning cached model data?

Thanks.

I ended up with the following solution.

Implementations in the models:

  • Set a property NSDate* modelUpdate that indicates the last update of the model. For models that should be deleted when they are old.
  • Set a property BOOL modelDelete in every model that needs to be deleted. But don't delete anything while the app is running. (so the existing models on memory don't become invalidated)

Strategy for recycling cached model on a server request:

  1. Check if a list of cached models exists (lets say cache A) for a given server request. (where modelDelete = NO)
  2. In parallel, even if cache exists, run the request to the server in background.
  3. Use cache A to show content to user as soon as possible. (Don't make the user wait)
  4. When the request to the server is done, save the new results to the cache. (cache B)
  5. Update what the user is seeing with the new data, gently with fades etc.
  6. Mark all the models of cache A to be deleted. (modelDelete = YES)

Delete cache at the following times:

  • When the app starts. (didFinishLaunchingWithOptions)
  • When the app terminates. (applicationWillTerminate)

With code like this:

NSDate* limitDate = [NSDate dateWithTimeIntervalSinceNow:-(60.0 * 60.0 * 24.0 * 7.0)];
RLMResults* modelProductListArray = [ModelProductList objectsInRealm:_cacheDb
                                                               where:@"modelUpdate < %@ || modelDelete = YES", limitDate];
[_cacheDb deleteObjects:modelProductListArray];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM