copying Realm file in Caches Directory
Asked Answered
B

2

8

My app got rejected by Apple because of this : "On launch and content download, your app stores 13.01 MB on the user's iCloud, which does not comply with the iOS Data Storage Guidelines."

I know what's the problem.how can i save my Realm database in Caches Directory instead of Documents directory?

Bleb answered 16/7, 2016 at 14:37 Comment(2)
#8489842Parturifacient
this is not what i want.Bleb
R
7

You can use Realm.Configuration.fileURL to change a Realm file path. Like the following:

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 would not like to specify fileURL every instantiating Realm, you can use Realm.Configuration.defaultConfiguration. If you set a configuration object to defaultConfiguration, Realm() uses the configuration as default.

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

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

Rosettarosette answered 17/7, 2016 at 17:10 Comment(0)
E
0

I updated kishikawa's answer for Swift 5 and Realm 10. I call this func in AppDelegate's "didFinishLaunchingWithOptions":

private func setRealmDefaultConfig() {
    let cachesDirectoryPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
    let cachesDirectoryURL = NSURL(fileURLWithPath: cachesDirectoryPath)
    let fileURL = cachesDirectoryURL.appendingPathComponent("Default.realm")
    let config = Realm.Configuration(fileURL: fileURL)
    Realm.Configuration.defaultConfiguration = config
}

keep in mind that obviously "DefaultConfiguration" will be used only when instantiating with no new config: Realm()

Eous answered 9/11, 2022 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.