TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to upload default-data to the public CloudKit database

Forums > Swift

I need some help understanding how the public database works in CloudKit. First of all, let me say that I know how to connect and use the private database. In this question I'm not looking for an answer on how to connect to the database at self, only the concept. Here is my confusion. I have a set of images that all users in my app will be using, right now what I'm doing is adding the images directly to the app (an Image Set in Xcode) and then I am pulling them to Core Data and then syncing them to CloudKit. As you can see all images are technically stored in every device using my app and in Core Data/CloudKit, not very efficient. What I would like is to have the images stored in a single place where all uses can pull the images from, in this case CloudKit. I know I can have them somewhere in a private server, but I feel like I would be adding more complexity to my app, so I think using CloudKit is a better option for me. Here is my question.

How do I get the images to CloudKit, do I upload them directly to CloudKit and then read from all devices or do I need to first add them to a device and upload to CloudKit from there?

Thanks!

4      

Maybe this Apple Developer video will help you. Details will have moved on as this video is few years old, especially when looking at the CK database.

4      

Yeah, I already watched the video but I'm still a little lost. Based on what the video shows, I want to belive that the records can be added directly to the CloudKit console and then read them from all devices but I would like some one to confirm it. Thanks a lot for your reply!

I found this thread but it's now closed, I wanted to ask if he ever implemented what @roosterboy suggested since it's the approach I'm leaning to.

https://www.hackingwithswift.com/forums/swiftui/coredata-and-cloudkit-syncing-question/7770

3      

hi,

since i had put my multiple-cents worth into the referenced thread, i thought i'd tell you the current incarnation of our "pre-loaded records in memory, with user-edits & additions then backed by Core Data records synced to the cloud" strategy as a way to avoid the problems of data duplication from the cloud discussed in that thread.

in fact, we eventually implemented Apple's Linking Data Between Two Core Data Stores strategy, with a local configuration for pre-loaded data, and a cloud configuration for user-edits & additions approach. only the cloud configuration is mirrored to an iCloud container; pre-loaded data is always in a local store (in our case, this is read-only data).

as for how to put pre-loaded data into the cloud, you could have a hook in your app (for development and debugging purposes, but certainly not in production) to show a separate screen with a few minimal controls to connect to the public database and manage your uploaded images. that does not sound too difficult, and i dabbled a little bit with some code at one point that seemed to work. (if you sign up for HWS+, you'll see that the Ultimate Portfolio app writes data to the public database, and you can use a lot of that code as i did.)

finally, we thought of doing something similar, putting pre-loaded data in the public database. however we decided against this, because it was simply easier to put that data in the app bundle and load it directly into Core Data in a local store at startup if it were not already there. it also required less maintenance and avoided error handling considerations with the cloud.

hope that helps,

DMG

3      

It looks like using the .public database is not as straightforward as I thought plus I originanly thought that I was going to be able to upload the images direclty to the CloudKit console and then read them from all devices. My idea was to not have the images stored in any of the devices only in Core Data but I don't think CloudKit work that way.

I'll just add the images to the app and then to Core Data in every device.

Thank you all for your input!

3      

@delawaremathguy any chance you can expand on how you handled 'load it directly into Core Data in a local store at startup if it were not already there'?

On device #2, checking if the default data has already been syncronised with the SwiftData/CloudData private database seems to be quite the challenge.

2      

To efficiently store and share images among all users using CloudKit's public database:

Upload Directly to CloudKit: Upload images directly to CloudKit using CloudKit APIs. This avoids the need to store them locally on each device before uploading.

Public Database: Store the images in the public database, making them accessible to all users.

Fetch Images: From each device, fetch the images directly from CloudKit's public database when needed, eliminating the necessity of storing them locally on every device.

This approach ensures a centralized image repository on CloudKit accessible to all users, reducing redundancy and improving efficiency.

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!

Reply to this topic…

You need to create an account or log in to reply.

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.