UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

What is the right way of using CloudKit as a Backup Service for Core Data?

Forums > Swift

Hey Guys,

I'm building a simple iPad diary app in which the data is saved with Core Data. The data consists of a lot of different things, like the user's drawings or the images added to the pages. My question is how to implement a backup service into my app the correct way. I don't think I need the functionality of syncing my app with other devices, I only need a possiblity for the user to restore the Core Data state, when he loses his device or accidentally deletes the app.

There are two approaches I have found:

  1. I build something similar to WhatsApp, where there is a section in the settings of my app called "Backup". There the user can set a time fequency (e.g. every day) at what point in time the app should create a backup. Then the app compresses all Core Data entities into one file and push it into the user's private cloud, via converting the file into a CKRecord. When the user loses his device and reinstalls the app, the app will search for a backup file in the user's cloud and if there is one availble download it and restore the Core Data state.

  2. I can use and NSPersistentCloudKitContainer instead of a normal one so all Core Data is synced with the user's cloud. There are two Problems I see here: a) There would be a lot of deleting and adding, I would need to define the right merge policy. I always want the cloud to be a copy of the app. I never want the situation, that the user deletes pages and then the app downloads them back from the cloud. I guess I would need to use the NSMergePolicy of type overwrite, or would I need to use the type mergeByPropertyObjectTrump? b) There is no restautation phase when the user redownloads the app. One approach for the restoration phase I came up with: The app has to automatically check if the user has existing iCloud data. If this case is true, the user has the option to reset the app or get his old backup back. If the user chooses to restore his old backup, the app will compare the number of local entities in Core Data to the number of entities in iCloud, till this number is equal. This should work because CloudKit automatically syncs the local database to the already existing one in iCloud, and downloads all entities in the background.

Right now I started implementing the second approach. I get a problem lot of errors I do not understand, which look like these:

PFCloudKitExporter exportOperationFinished:withSavedRecords:deletedRecordIDs:operationError:]_block_invoke(492): Failed to delete asset file: .....fxd
Error Domain=NSCocoaErrorDomain Code=4 "“.....fxd” couldn’t be removed." UserInfo={NSUserStringVariant=(
    Remove
), NSFilePath=/var/mobile/.....fxd, NSUnderlyingError=0x281995e90 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

Therefore I am rethinking of switching to the first approach.

3      

I have the same dilema. I was thinking of using option 1. Did you end up trying this? Any recommednations based on your experince?

3      

Just curious if either of you solved this problem. I am in a similar situation and looking for a good solution.

Thank you

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.