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

CloudKit and CoreData

Forums > Swift

I have a weird situation here that I cant quite figure out the solution to. My app uses CoreData (CD) and I load the data base with a bunch of data the first time the app launches. Thats all been working fine with no issues.

Heres the problem: I have decided to implement CloudKit so data is synced across devices. This works fine with one exception, heres the scenario where things are failing:

The user has 2 devices an iPhone and iPad they want to run my app on:

So they install the app on their iPhone and when the app runs its checks if the local CD database has records. If not, it loads the Hymns.json file (which is a bunch of static hymns) and this data is then put in the cloud.

Now the user loads the app on a new device( their iPad ). When the app runs the local (on device) CD database will be empty, so it will then load the Hymns.json file to the local (on device) database. At this point the data is duplicated.

Does anyone know if there is a way to check the cloud first to see if there is data stored their before loading data from the JSON file to their local (on Device) CD database?

I would appreciate any advice y'all have. Or if you encountered this type of situation before how did you solve it?

Thanks

2      

Hi,

I think one solution could be to use the NSUbiquitousKeyValueStore (https://developer.apple.com/documentation/foundation/nsubiquitouskeyvaluestore) Which works like UserDefaults but is synced via iCloud across devices. With this the check will be quicker than waiting for the CloudKit to load the records.

Another route you could try is to give these records same IDs and then maybe the database would realize that these are the same data and would not import them again?

2      

Thanks, @nemecek-filip,

I'll went and read about this, it seems quite involved, not saying this isnt the way to go. Basically I have a local JSON file of 341 records thats part of the app and are essestial to the app. So I only want them loaded once but if the user has two devices I just wanted a way to do a simple check on the cloud data, if some was there then I wouldn't load the local file. Can that be done?

Now an off topic question, I've seen several of you guys do this. How do you bold the text in red, I can't seem to figure that one out. When I hit cmd B its puts ** around the text.

Thanks, Mark

2      

Now an off topic question, I've seen several of you guys do this. How do you bold the text in red, I can't seem to figure that one out. When I hit cmd B its puts ** around the text.

Use backticks around the text to give it that look. Or hit the </> button in the formatting toolbar.

2      

If you want to check based on the data, you need to find out how to listen to CloudKit to tell you that it finished syncing the Core Data objects. Once that is done, you can query the objects and if none are there, do the import from JSON.

Also since these data are static (from the json file), why bother syncing them? You can always load them from JSON and be done with it?

2      

Thank you again for the reply and suggestions. I'll look into each of them.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.