Copy the Realm file to the Caches directory

My application was rejected by Apple because of this: "When you download and download content, your application stores 13.01 MB for iCloud user, which does not comply with the iOS Storage Guide."

I know the problem. How can I save my Realm database in the Caches directory instead of the Documents directory?

+5
source share
1 answer

You can use Realm.Configuration.fileURL to change the path to the Realm file. As below:

 let cachesDirectoryPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] let cachesDirectoryURL = NSURL(fileURLWithPath: cachesDirectoryPath) let fileURL = cachesDirectoryURL.URLByAppendingPathComponent("Default.realm") let config = Realm.Configuration(fileURL: fileURL) let realm = try! Realm(configuration: config) 

If you do not want to specify fileURL every instance of Realm, you can use Realm.Configuration.defaultConfiguration . If you set the configuration object in defaultConfiguration , Realm() uses the default configuration.

 Realm.Configuration.defaultConfiguration = config let realm = Realm() 

See also ... https://realm.io/docs/swift/latest/#realm-configuration

+4
source

All Articles