CloudKit CRUD Functions in SwiftUI project | Advanced Learning #22

Поділитися
Вставка
  • Опубліковано 5 лют 2025
  • Now that our project is connected to CloudKit, we can start uploading and downloading data to and from the database. We will start with some simple functions and work out way through more difficult ones, covering all of the basic CRUD methods. That is: Creating, Reading, Updating, and Deleting CKRecords in our CloudKit container.
    Set up CloudKit: • Setup CloudKit in Swif...
    🤙 WELCOME BACK 🤙
    WEBSITE: www.swiftful-t...
    DISCORD: / discord
    GITHUB: github.com/Swi...
    SAY THANKS: www.buymeacoff...

КОМЕНТАРІ • 41

  • @OttoFaerber
    @OttoFaerber 2 роки тому +3

    Hi nick, thanks for all the great videos. They are really very helpful.
    It is totally crazy, I have programmed databases more than 30 years ago. But it has never been so totally crazy complicated to read a record from a database.

  • @orasbaeapps
    @orasbaeapps 3 роки тому +3

    As always, awesome! very exited for cursor videos. I have 4 Apps on Apple Store that works with iCloud and the cursor make me crazy. I never tire of saying it, very good job with your video series

  • @Connor-pj2tg
    @Connor-pj2tg 3 роки тому +1

    You mentioned the idea of combine with CloudKit, can't wait for that video lol :)
    Great video Nick!

    • @Connor-pj2tg
      @Connor-pj2tg 3 роки тому +1

      You also said in the video nick that the video is longer than expected. I'm not complaining lol. It's more to learn :)

    • @SwiftfulThinking
      @SwiftfulThinking  3 роки тому

      Haha yes, it's coming in video #25. It's a pretty challenging video to say the least 😳 You'll have to let me know what you think...

  • @thomasfrank8379
    @thomasfrank8379 3 роки тому +2

    Thanks for great videos, Nick!

  • @joshkinney5091
    @joshkinney5091 2 роки тому +2

    Awesome stuff!
    I really enjoy how you start coding, run into an isuues and solve it as you code vs just coding everything perfect.
    I currently have a collection view fetching data. It would be helpful to cover how to handle your data not disappearing when your app closes.

  • @MoAdel92
    @MoAdel92 3 роки тому

    i am really impressive with your efforts :)

  • @youngdomsey
    @youngdomsey 8 місяців тому

    In minute 33:40 you are talking about "combine code" which avoids getting the purple error when you are not updating the data in the main thread. You said you may cover this in a future video. Is this video already available?

  • @nicolarigoni1062
    @nicolarigoni1062 3 роки тому +2

    Hi Nick, that for your video they are exactly what I’m looking for. Do you plan to make a series about Firebase too? Thanks a lot

    • @SwiftfulThinking
      @SwiftfulThinking  3 роки тому +2

      Hi Nicola! Yes, Firebase has been requested a few times now 😅 It's coming soon..

    • @nicolarigoni1062
      @nicolarigoni1062 3 роки тому

      @@SwiftfulThinking Great, great, great!! and another time thank you so much for your course!!

  • @dr.craigcurphey4829
    @dr.craigcurphey4829 3 роки тому +2

    Hi Nick - When I add a fruit, it updates the database but not the UI. Is there a way to show the new item, since we are fetching all items in the database? Just wondering if it is a timing issue like perhaps the fetchItems() is happening before the database gets physically updated? If this is what is happening, is there a way to create a listener and have the database update fire some sort of "hey app, I have a new record and here it is!"? Thanks Nick! Good work as always.

    • @SwiftfulThinking
      @SwiftfulThinking  3 роки тому +1

      Dr. Craig always with the best questions haha two things....
      1. Yes, this is doable. In a later video in this series we add Push Notifications, which are actually just listeners on the database using Subscriptions. You can basically use the same code, except update the UI instead of sending a push notification. However...
      2. In this situation, we already know exactly what the new item is going to be. So even easier than listening to the database, we could just update the UI manually as we upload it to the database. Ie... user "adds item", you can immediately update the UI with the item, then upload to database and ensure it's successful. If there's an error updating the database, then revert your UI and remove the new item manually

    • @seccat
      @seccat 2 роки тому

      Does work if you delay the fetchItems() by 1 second. This solution is NOT FOR PRODUCTION. And it didn't work for 0.5sec.
      DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
      self.text = ""
      self.fetchItems()
      }

    • @seccat
      @seccat 2 роки тому +1

      Just saw your solution in "Advanced Learning #23"

  • @rvamerongen
    @rvamerongen Рік тому

    Hi, thank you for the tuts, love them. However, I want that the public data base is available to only family members, how can I accomplish that? Did you wrote something about this?

  • @DavidMartin12
    @DavidMartin12 2 роки тому

    How do you set it up so that when you save, if the "Apple" is already in there it just disregards or does nothing?

  • @Iphone-AndreAbels
    @Iphone-AndreAbels 2 роки тому

    I'm not experienced with CloudKit and CRUD functionality! How do I get additional information about a record? Like all metadata information: creationTime, modificationTime, who inserted the record etc. I would like to get the "name" of the record created AND the creationTime in the list as well.

  • @ncba
    @ncba 3 роки тому +2

    Is CloudKit like some Apple version of Firebase?

    • @SwiftfulThinking
      @SwiftfulThinking  3 роки тому +3

      Haha yes, basically. CloudKit is the backend "server" that Apple provides. It's great for iOS apps because we can connect in minutes and use it for free (up to certain limits). Every iOS user has an iCloud account and CloudKit connects to the same overarching cloud. The main downside is that CloudKit is specific for iOS. So if you need to share the backend with an Android app, then Firebase is probably a better option. But if you're only iOS (or at least only iOS for the time being), then CloudKit is a great choice.

  • @path2ds863
    @path2ds863 3 роки тому

    Great tutorial! Thank you so much! May I ask you if how can I authenticate users on Android devices? And is there a way to migrate from CloudKit to AWS?

  • @dugrut1325
    @dugrut1325 2 роки тому

    IMPORTANT QUESTION! How can you fetch more then the100 record limit? Or continue to make request until completion?

    • @SwiftfulThinking
      @SwiftfulThinking  2 роки тому

      Yea it would be multiple fetch requests

    • @dugrut1325
      @dugrut1325 2 роки тому

      @@SwiftfulThinking the question is HOW? I realize it’s necessary?

  • @DNeutron0
    @DNeutron0 2 роки тому +1

    How did you learn swift?

  • @calebyates253
    @calebyates253 Рік тому

    Discord notification at 37:40

  • @gabrielrml
    @gabrielrml 3 роки тому +3

    This is great BUT it would have been useful to include at least one other field, for example Cost and shown how to return more than 1 field. Apart from a to-do-list I can't think of one useful application that would return one field.

    • @dugrut1325
      @dugrut1325 2 роки тому +1

      I was able to have four fields: name, amount, color, date
      in the func addItem (passed the three values, to make it easy they are all string) in it I created the three fields (newFruit["name"] = name, newFruit["amount"] = amount, newFruit["color"] = color, newFruit["date"] = Date() )
      had three textfields in the View,
      had three binding: $nameText, $amountText, $colorText,
      I check to see if all three values were not empty. it all worked.
      BUT I do not know how to fetch the array of values, I would think you would need arrayModel like returnedItems: [FruitMode], but how to get the database into the array I do not know???

    • @gabrielrml
      @gabrielrml 2 роки тому

      @@dugrut1325 Thank you Douglas - great to hear back from someone, since the teacher just ignores us (me).

    • @dugrut1325
      @dugrut1325 2 роки тому

      @@gabrielrml the next video he gives more help with [FruitModel] array, to have as many values or keys as you want, even an image. Which I got to work, add, fetch, display in view

  • @gabrielrml
    @gabrielrml 2 роки тому +1

    Could someone - because Nick ignores his audience, tell me how you would fetch more than just "name" from fruits assuming it had more for example price, or variety - its the fetching bit I am having problems so far I can only return one item (firstName), because I cant load the others, from my cloudKit database.

    • @w0mblemania
      @w0mblemania 2 роки тому +2

      Nick doesn't owe you help with your homework.

    • @gabrielrml
      @gabrielrml 2 роки тому

      @@w0mblemania whats the point of a tutorial then? didn't you ask questions at school, obviously not.

    • @w0mblemania
      @w0mblemania 2 роки тому +1

      @@gabrielrml What happened at school when you accused your teacher of ignoring students?
      Did you get the answer you wanted, or did you get to sit in the corner for a while?

    • @gabrielrml
      @gabrielrml 2 роки тому

      @@w0mblemania If a teacher was accused of ignoring students they would be answerable to the powers that be - but while we are on school I am reminded of this "Tis better to keep silent and be thought a nasty piece of work, than to speak and remove all doubt." Stop digging my friend.

    • @w0mblemania
      @w0mblemania 2 роки тому +1

      @@gabrielrml Says the guy making snarky accusations against a teacher who is giving him and education for free...

  • @bobearl99
    @bobearl99 2 роки тому

    Continuing to enjoy the series tremendously. Apologies for a long and technical comment, but I think I may have stumbled upon a way to fetch items that to me, at least, more intuitive. I had a hard time wrapping my brain around the concept of adding an operation, as it wasn't entirely clear to me how to think about it so that I would continue to recall it in a week or a month, and I wasn't sure how I would remember to add the queryResultBlock modifier to the query operation. After a little fumbling around, it seems like the use of add(operation) here is analogous to the function CKContainer.default().publicCloudDatabase.perform to perform the query, which was deprecated in iOS 15. It was deprecated in favor of CKContainer.default().publicCloudDatabase.fetch(withQuery: query), which does have a completion handler that yields matched results that are mapped to the individual fruit records. So in code format:
    private func fetchItems(){
    let query = CKQuery(recordType: "Fruits", predicate: NSPredicate(value: true))
    CKContainer.default().publicCloudDatabase.fetch(withQuery: query) { [weak self] returnedResult in
    switch returnedResult{
    case .success((let matchResults, let queryCursor)):
    for result in matchResults{
    print("result: \(result)")
    }
    case .failure(let error):
    print("Error: \(error.localizedDescription)")
    }
    }
    }
    I know you are likely pretty busy getting feedback on hundreds of videos, but after learning literally hundreds of useful tips from you I am wondering if this might be an alternative intuitive way that you might find helpful. Buying you 2 more cups of coffee for your trouble, keep up the wonderful work!