Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.realm file automatically backed up by iCloud

Tags:

ios

swift

realm

my app was rejected because of the size of the content that it uploads to iCloud. The only file in my app's Documents folder is the default.realm database file. I think that this is the file that iCloud is uploading. How can I prevent iCloud to upload the database to iCloud?

Thanks.

like image 822
bitsoverflow Avatar asked Jul 22 '15 18:07

bitsoverflow


People also ask

Are files automatically backed up to iCloud?

To automatically back up your device each day, turn on iCloud Backup via Settings > [your name] > iCloud > iCloud Backup and toggle iCloud Backup to on. If you're using iOS 10.2 or earlier, go to Settings > iCloud > Backup. The device will then back up when your phone is connected to power, locked, and on Wi-Fi.


1 Answers

According to the App Backup Best Practices section of the iOS App Programming Guide, <Application_Data>/Library/Caches or <Application_Data>/tmp will not backup to iCloud. Generally, you can use <Application_Data>/Library/Caches directory to save your data that you won't backup to iCloud.

To change the file path of Realm, you can pass the path parameter when creating Realm instance, like below:

let realmPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] as! String
let realm = Realm(path: realmPath.stringByAppendingPathComponent("data.realm"))

Otherwise, you can use NSURLIsExcludedFromBackupKey file system property to exclude files and directories from backups (See Technical Q&A QA1719). If you want to use the default path, there is the only way to exclude Realm file from backups.

let realm = Realm()
if let realmPathURL = NSURL(fileURLWithPath: realm.path) {
    realmPathURL.setResourceValue(NSNumber(bool: true), forKey: NSURLIsExcludedFromBackupKey, error: nil)
}
like image 164
kishikawa katsumi Avatar answered Oct 30 '22 08:10

kishikawa katsumi