Fetching Firebase Firestore data with Codable in Swift | Firebase Bootcamp #10

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ส.ค. 2024
  • In this tutorial, you'll learn how to use the Codable protocol in Swift to fetch data from Firebase Firestore. With Codable, you can easily map your Firestore data to custom model objects, making it easier to work with your data in your iOS application. Follow along as we walk through the steps to set up your Firestore data model and decode your Firestore documents using Codable. By the end of this video, you'll have a solid understanding of how to use Codable to fetch and work with data from Firestore in your Swift application.
    🤙 WELCOME BACK 🤙
    WEBSITE: www.swiftful-thinking.com
    DISCORD: / discord
    GITHUB: github.com/SwiftfulThinking/
    SAY THANKS: www.buymeacoffee.com/nicksarno
    00:00 Firestore Codable
    01:57 Intro
    03:40 Managing database references
    05:56 Codable
    10:52 Encoder for snake_case
    12:40 Decoder for snake_case
    15:56 Customizing DBUser init
    17:40 Adding isPremium
    20:06 Updating a struct
    27:18 Mutating a struct
    30:20 setData vs updateData
    34:41 Custom coding strategies
    38:45 Custom Coding Keys
    42:03 Conclusion

ความคิดเห็น • 52

  • @kdtechniquesofficial6153
    @kdtechniquesofficial6153 ปีที่แล้ว +4

    I love the way you took the time to teach us how to scale our code in an efficient way by doing it the hard way first and then changing the code in an efficient and scalable manner. Thank you, Nick.

    • @iLoveAppl3947
      @iLoveAppl3947 หลายเดือนก่อน

      for someone coding along this can be a pain in the azz. He can do it correctly from the beginning that would save so much time and confusion

  • @TaylorOgburn
    @TaylorOgburn 9 หลายเดือนก่อน

    I have never watched a TH-cam video and actually learned something from them. The content is always over my head or so basic that I'm yawning. Nick, you are CRUSHING these, and I can honestly say my time in college is nowhere near as valuable as the knowledge I'm getting from your videos. RESPECT!

  • @MaksimLA
    @MaksimLA 9 หลายเดือนก่อน

    I love the way you show easy way in one video and then advanced in next! Gives me much better context and understanding!
    Thank you so much for your work

  • @sattorjabborov2728
    @sattorjabborov2728 ปีที่แล้ว +2

    You could do it using just one way to keep the video short, BUT I REALLY APPRECIATE THE WAY YOU SHOW US ALL POSSIBLE WAYS TO DO ONE THING. YOUR VIDEOS ARE 🔥

  • @dereknoppinger9129
    @dereknoppinger9129 ปีที่แล้ว +1

    This series has been a godsend. Thanks a lot man.

  • @sraldous
    @sraldous ปีที่แล้ว +2

    Amazing, I'm loving this Firebase Firestore series, thank you so much for sharing your knowledge. I'm learning a lot!

  • @danielwatson6529
    @danielwatson6529 7 หลายเดือนก่อน

    ive been working with swiftui/firebase for a while and doing incredibly well (I had thought) this is awesome stuff. I literally feel like you have made me a professional the layers of polish you have given me is insane. im so so grateful, reckon ill be leaving my day job soon because of all the hard work I have put in, combined with the absolute mirical of the modern world where I can latch on to an incredible mentor as someone like your self. you are a golden egg THANK YOU SARNO!!!

  • @loudibird
    @loudibird 10 หลายเดือนก่อน

    bro youre the best. your gonna live forever through our code

  • @user-fk7jl4ht1c
    @user-fk7jl4ht1c 4 หลายเดือนก่อน

    Wow...... Thank you, Nick. 🙏 This was a great job ! )

  • @andresraigoza2082
    @andresraigoza2082 ปีที่แล้ว +1

    Thank you so much Nick! Just a note at 21:58, if you extract the init(auth: AuthDataResultModel) { … } to an extension of the same struct you will get your memberwise initializer back, meaning that you won’t need to write this init() at all.

  • @pakaponwiwat2405
    @pakaponwiwat2405 ปีที่แล้ว

    Thank you very much, mr. Nick!

  • @user-mw2sq3cw1s
    @user-mw2sq3cw1s 8 หลายเดือนก่อน

    Every time you make it so easy thanks!!

  • @melmandaragrows
    @melmandaragrows ปีที่แล้ว +1

    Wow, this was great. I’ll have rewatch so many times

  • @juliansha2590
    @juliansha2590 ปีที่แล้ว

    Amazing stuff thank you :)

  • @aliabraham1725
    @aliabraham1725 ปีที่แล้ว

    Fantastic Work! This is so helpful. I'm building and app with firebase & I will go back make changes to make my app more efficient. Thank you!

  • @kdtechniquesofficial6153
    @kdtechniquesofficial6153 ปีที่แล้ว

    We are super excited too. Buckle up, and let's go. Boom Boom Boooooooom. 🚘

  • @omerfarukozturk9720
    @omerfarukozturk9720 7 หลายเดือนก่อน

    Content 🔥

  • @nikodemeroutis
    @nikodemeroutis ปีที่แล้ว +8

    Big big thanks !! But I don't know why when I try to make an encoder (11:20) I have an error saying that: Firestore.Encoder has no member keyEncodingStrategy ...

    • @iLoveAppl3947
      @iLoveAppl3947 หลายเดือนก่อน

      lots of boilerplate code in this video... complicated as f and this makes me use CloudKit

  • @vojtechkalivoda8455
    @vojtechkalivoda8455 ปีที่แล้ว +7

    ERROR FIX: encoder (11:20) I have an error saying that: Firestore.Encoder has no member keyEncodingStrategy ... 🙁 SOLUTION: It is just that u have different firebase version!!! Go to your project in Xcode(not target, but project!!) -> Package Dependencies -> double tap on Firebase -> select Exact version -> set it to his version which is 10.4.0 :) and you are ready to continue 😇

    • @killian8703
      @killian8703 ปีที่แล้ว

      this works. thank you

    • @ZL-Asica
      @ZL-Asica ปีที่แล้ว

      works!

    • @user-xd2fg4wp3l
      @user-xd2fg4wp3l ปีที่แล้ว

      Legend cheers

    • @lucyferyx
      @lucyferyx 7 หลายเดือนก่อน

      I hope your pillow is always cold on both sides

  • @zacmasters7744
    @zacmasters7744 ปีที่แล้ว

    Has Firebase deprecated setData(from: ) ? I can't seem to get it this version of setData working.

  • @jonothen2212
    @jonothen2212 9 หลายเดือนก่อน

    anyone tried to write .setData(from: ) and get this error: Cannot convert value of type 'myDatatype' to expected argument type '[String : Any]'
    when i typed .setData(from:) this func is grayed and get this: this method is defined in DocumentReference but my not be available in this context?
    thanks guys.

  • @lincolndickerson1293
    @lincolndickerson1293 ปีที่แล้ว

    Very thorough! Thank you.
    One thing that struck a sour note. You mentioned your preference to be ready to adapt to a bad key. Which at first glance seems great. Consider this, if you have to do that often then certainly be prepared.
    On the other hand creating the custom keys only if needed and its a very rare occurance which has been my experience saves many, many lines of code over many projects. Code that doesn’t exist doesn’t need to be tested. No typo’s on your end with keys if they are generated.
    One thing that I do find appealing is having one source of truth for the key itself. Do you know of a way to get the autogenerated key?

    • @SwiftfulThinking
      @SwiftfulThinking  ปีที่แล้ว

      I don’t think there’s a way to get the auto-generated keys, probably because if you don’t declare the CodingKeys, they are open to change based on the decoder preference.

  • @SlidE933
    @SlidE933 ปีที่แล้ว

    12:40 I try to decode for snake case, but this the method doesn't work, even if I copy this code, if I use old method with dictionary, it works...

  • @mahoozi.97
    @mahoozi.97 5 หลายเดือนก่อน

    great tutorial , I add displayName property in DBUser struct, when I signed with google I got displayName, but I didn't got displayName when I signed in with apple, also I facing same issue with photoUrl !

    • @SwiftfulThinking
      @SwiftfulThinking  5 หลายเดือนก่อน

      Different SSO options provide different bits of data unfortunately. You can customize what data you’re requesting in the SSO sign in request, buts it’s still limited.

  • @DaikiKaneda
    @DaikiKaneda ปีที่แล้ว

    😍

  • @crawford6337
    @crawford6337 10 หลายเดือนก่อน

    trying to follow along with this video and i'm running into an issue where firestore doesn't recognize encoder/decoder and is not allowing the profile info to show in the simulator. Chatgpt says swift doesn't use custom encoder/decoder and it has it's on default decoders. I'm stuck and lost and frustrated. Please help if you can.

    • @SwiftfulThinking
      @SwiftfulThinking  10 หลายเดือนก่อน

      Join the discord and someone will help you out: discord.gg/vhKKyYTGDb

    • @iLoveAppl3947
      @iLoveAppl3947 หลายเดือนก่อน

      don't follow along this is terrible to do... this guy is wasting your time

  • @lincolndickerson1293
    @lincolndickerson1293 ปีที่แล้ว

    I know that traditional REST and JSON use snake case for keys. But it is not really necessary to be functional. Why not just leave it with camel case and let the swift decoder handle the decoding without customizing the coding and decoding? I will work. I tried it to be sure.

    • @SwiftfulThinking
      @SwiftfulThinking  ปีที่แล้ว

      As I showed in the video, it definitely works… but if you ever don’t follow the decoding rule exactly, you’ll have major problems. I think this is a pretty common situation in more prod apps I’ve worked with. Just offering my two cents.. do what’s best for your project!

  • @ThinThedev
    @ThinThedev ปีที่แล้ว

    I tweaked the code a little and instead of using the encoder, I just used CodingKeys within the DBUser Struct. This essentially let's you specify a mapping between the your struct values and the Firestore values and reduces the need for using the firestore specific encoder/decoder.

  • @melmandaragrows
    @melmandaragrows ปีที่แล้ว +2

    When I try the to use
    private let encoder: Firestore.Encoder = {
    Let encoder = Firestore.Encoder()
    Encoder.keyencodingstrategy = .convert to snake case
    Return encoder
    }()
    I get two errors
    1. Value of type ‘Firestore.encoder’ has no member ‘keyencodingstrategy’
    2. Cannot infer contextual base in reference to member converttosnakecase

    • @rasheed1andrew
      @rasheed1andrew ปีที่แล้ว

      I'm facing the same problem and can't seem to figure it out

    • @melmandaragrows
      @melmandaragrows ปีที่แล้ว

      @@rasheed1andrew I reached out to google, I sent them a GitHub gist of the code for authmanager and usermanager, I think it might be a weird bug. They are working with me to try to figure it out. Will update whenever I get an answer.

    • @rasheed1andrew
      @rasheed1andrew ปีที่แล้ว +3

      @@melmandaragrows I figured it out. Check the version of Firebase you are currently using. Delete the one you currently have and when you download it again set the version to 10.0.0

    • @rasheed1andrew
      @rasheed1andrew ปีที่แล้ว

      @@melmandaragrows use the exact version Nick used in the video

  • @loudibird
    @loudibird 10 หลายเดือนก่อน

    can I give you equity in the app I'm trying to make

  • @user-ju9dy4yn2b
    @user-ju9dy4yn2b ปีที่แล้ว +1

    you write code difficult af , you only write as you want and I hate the way you write code

  • @vojtechkalivoda8455
    @vojtechkalivoda8455 ปีที่แล้ว +1

    Lets gooooo, love this video!!! 🔥🔥🧯