0

I have an application using Realm.io that on logout, we totally destroy the realm cache that has been built up by the user so that when a new user logs in, we can recreate the cache file for the next user.

Deleting all objects is not an option as the realm file is encrypted using a hash of the users passcode so it has to be a fresh Realm (AFAIK).

Currently I am deleting the Realm file like so:

NSString *realmFile = [[DCMCacheManager documentsDirectoryPath] stringByAppendingPathComponent:@"myApp.realm"];
NSFileManager *localFileManager = [NSFileManager new];

[localFileManager removeItemAtPath:realmFile error:&error];

And reinstating it using:

RLMRealm * realm = [RLMRealm realmWithConfiguration:config error:&error];

When the creation code runs for the first time the realm file is created with no issues. When I try to recreate it after deleting, the app seems to return an internally cached version of the realm file so does not create the myApp.realm file for me.

Any suggestions on how to make sure the realm is absolutely dead when I delete it?

darkbreed
  • 151
  • 9

1 Answers1

1

Before deleting the Realm file you will need to ensure that all Realm objects using that file have been deallocated. When possible, the easiest way to do this is to simply do the deletion before you ever open the Realm file at all, or if you only need the Realm to be open very briefly, to do so within an explicit autoreleasepool.

If users can log out and log into a different account while the app is running, then this isn't an option. Instead, what I would recommend is to use a different file path for each user (i.e. something like NSString *realmFile = [[DCMCacheManager documentsDirectoryPath] stringByAppendingPathComponent:userName]; rather than @"myapp.realm"). This would sidestep the issue entirely, and make switching back and forth between users faster. If your Realm files take up a nontrivial amount of space, you could clean up the files for all but the currently active user on app startup.

On a side note, if your Realm files are purely just a cache then Apple wants to you put the file in the Caches directory and not the Documents directory, and they do occasionally reject apps for not doing that.

Thomas Goyne
  • 8,010
  • 32
  • 30