Mapping Firestore documents using Swift Codable

  Рет қаралды 21,487

Firebase

Firebase

Күн бұрын

In the last episode, we learned how to fetch data from Firestore in real-time. On today’s episode of Firebase Semi-live, Peter will demonstrate how we can use Cloud Firestore and Swift Codable API.
Chapters:
0:00 Intro
0:24 A closer look at our existing code
1:18 The Codable protocol
1:55 Adding the FirebaseFirestoreSwift pod
2:25 Making the model codable
3:10 Fine-tuning the data mapping
3:55 Mapping the document ID
5:05 Refactoring the view model
6:24 Demo
7:11 Using Codable when writing data to Firestore
8:55 One more thing...
9:33 Using Google's Open Source search engine to search Firebase's code base
10:21 A look at the @ServerTimestamp property wrapper
12:50 What is @ExplicitNull, and when should you use it?
13:45 Wrap-up
Resources:
Encoding and Decoding Custom Types → goo.gle/2T1Quic
Follow Peter’s blog → goo.gle/2Y114cK
Firestore server timestamps explained → goo.gle/2T6mfqr
Setting up Firebase for iOS → goo.gle/39xIIBZ
Cloud Firestore Intro → goo.gle/2ycqV6L
SwiftUI and Firebase playlist → goo.gle/3f9Uiav
Subscribe to Firebase → goo.gle/Firebase
#FirebaseSemiLive

Пікірлер: 59
@marlokessler
@marlokessler 4 жыл бұрын
Wow, awesome stuff😊👍🏼
@yannmassard3970
@yannmassard3970 4 жыл бұрын
hello, was just hovering yt swiftui stuff on yt. came accross this. Well, as a FB / FS coder, I can tell you, dear reader, that this is the only video I ve seen so far where it shows EXACTLY how to properly safe connect FS to SUI. the code is solid, could have been little bit safer, but would have made a much longer video. To the creator : well done
@rasheedm2084
@rasheedm2084 3 жыл бұрын
Hey !! these propertyWrappers are great 🤯
@timchenkoms
@timchenkoms 2 жыл бұрын
I am a database developer and not a fan of classes and structures. However, this video motivates me to use structures, that's so useful.
@ste9421
@ste9421 3 жыл бұрын
These are the most helpful videos thank you!
@PeterFriese
@PeterFriese 2 жыл бұрын
Thanks for the kind words, Seth 🙂
@DimNovo
@DimNovo 4 жыл бұрын
Very helpfull 👍🏻 Thank you 👏🏻
@slash0t5
@slash0t5 4 жыл бұрын
Thanks for the helpful video. How would I get a single document and map it to the struct?
@gershonbaryeh
@gershonbaryeh 3 жыл бұрын
Thanks for your great tutorials. Could you please explain how to handle a Timestamp data type in FIrebase that is optional? I get an error if the field doesn't exist in the document
@jasonchancey9508
@jasonchancey9508 2 жыл бұрын
Thanks for these Peter. How would you handle mapping a document that contains a DocumentReference to another document? All the standard firestore datatypes are relatively straightforward (Ints, Strings, Dates, etc.) but I have a data model that's a bit more complicated. To extend your example... a collection of Authors and a separate collection of Books which contain *references* to an Author in the Authors collection.
@DanKardell
@DanKardell 3 жыл бұрын
Great Job. What do you do for enums that are a part of your object?
@darato2015
@darato2015 3 жыл бұрын
I'm having trouble setting my data model where my document field names are unknown during fetching. In Firestore, the field keys are strings and values are numbers to store the reading progress. In my case the key is a slug. The type from Firestore is a dictionary and I tried to dump into a dictionary [String:Double] but I think you can only code and decode from a struct/class/enum. How would you go about modeling this data?
@choiseongwoo8325
@choiseongwoo8325 3 жыл бұрын
Thank you for the helpful video! One question.. Is the document data from firestore saved in local instance "books"? Or is there any other way to save the data locally?
@PeterFriese
@PeterFriese 2 жыл бұрын
Yes, this is cached locally. Firestore has offline support out of the box.
@floatr6441
@floatr6441 4 жыл бұрын
Thanks for these great tutorials of late! How does Firestore's codable implementation deal with sub-collections? ie - How do I design a struct to designate a property on it to be a subcollection rather than an array?
@slash0t5
@slash0t5 4 жыл бұрын
That doesn't exist. You'll have to query the parent document, grab the document ID and then query the sub collection.
@krisnaldoHD
@krisnaldoHD 3 жыл бұрын
I am using default values in my struct, and when I am loading something that has a missing value in my documents, it refuses to decode... Never had any problems with this until now, any suggestions? I do not want to use optional values...
@jofresh228
@jofresh228 3 жыл бұрын
How do you import JSON file to FireStore Cloud?? Thank you!
@virajjc
@virajjc 4 жыл бұрын
Can we connect the Firebase with Power BI? Any solution?
@DanKardell
@DanKardell 3 жыл бұрын
Why does the id not match up to the id that is in the db display? Does UUID().uuidString do something to the actual key of the record?
@isaacwheel525
@isaacwheel525 3 жыл бұрын
is possible to connect this to my home.storyboard ?
@jbruhin
@jbruhin Жыл бұрын
Is it possible to change the `keyEncodingStrategy` and `keyDecodingStrategy` of the JSON encoder/decoder being used here by the FirebaseFirestoreSwift API?
@ayt_daily
@ayt_daily 3 жыл бұрын
Hi, I don't want to create an id with UUID().uuidString, I want to create an id by retrieving the same value in the id document in the firestore database, how do I do that?
@Away88123
@Away88123 3 жыл бұрын
me TOO, you discover that ????
@ayt_daily
@ayt_daily 3 жыл бұрын
@@Away88123 delete UUID().uuidString from your model and it will work
3 жыл бұрын
@@ayt_daily struct Agenda: Identifiable, Codable { @DocumentID var id: String? var nome: String var telefone: String enum CodingKeys: String, CodingKey { case id case nome case telefone } } this ?
@thomasburke2845
@thomasburke2845 2 жыл бұрын
I wanted to accomplish the same and as stated above you need to - remove "UUID().uuidString" AND - add "case id" to the CodingKeys
@mrxxx8198
@mrxxx8198 3 жыл бұрын
How to get Firestore Array items in swiftui
@DavidNitzscheBell
@DavidNitzscheBell 3 жыл бұрын
Why do all the blogs and videos only discuss the simple data types: String, Int and maybe bool? What if my struct has a Date variable? How do I save to Firebase and then read the data back out?
@PeterFriese
@PeterFriese 2 жыл бұрын
For a comprehensive guide to using Codable with Firestore, check out this blog post I wrote: peterfriese.dev/posts/firestore-codable-the-comprehensive-guide/. As for your question: covering fewer / simpler topics allows us to create short videos that more people will benefit from. Covering all the topic from the blog post would probably take a 30 - 45 minute video. If you're interested in this, let me know!
@melmandaragrows
@melmandaragrows Жыл бұрын
6:53 I get an error saying “Type of expression is ambiguous without more context”
@mochithemaltese
@mochithemaltese 4 жыл бұрын
What about when we have to do UpdateData? It doesn't take any Encodable values...
@tahmidazam639
@tahmidazam639 3 жыл бұрын
Just do it manually with the [String: Any] method. The structs don’t have an update feature, so Firestore doesn’t either
@brydeemer
@brydeemer 3 жыл бұрын
What about encoding and decoding server timestamps? Slightly more complicated examples would be nice.
@gokhanozentr1
@gokhanozentr1 Жыл бұрын
How we mapping data match table view?
@user-Keane81
@user-Keane81 4 жыл бұрын
Do you need to add "case id" in "CodingKeys" enum? Otherwise it looks like default value is always using for id instead of actual one.
@peterfriese5076
@peterfriese5076 4 жыл бұрын
Yes, you're right. I've corrected that in peterfriese.dev/swiftui-firebase-codable/ - a bit harder in the video :-)
@arian.nur.4989
@arian.nur.4989 4 жыл бұрын
Y yang perlu Anda tahu yang hanya ada satu hal penting yang harus diperhatikan tentang perawatan kehamilan tidak bisa lagi menyayangimu yang hanya ada satu sama
@marioaparicio
@marioaparicio 4 жыл бұрын
I would like to see a tutorial, with the best way to manage a relational model.
@tahmidazam639
@tahmidazam639 3 жыл бұрын
Any luck?
@nedimkankul8226
@nedimkankul8226 2 жыл бұрын
Hi! i have done the same stuff everything is working but @DocumentID is not. its not giving me the name of document id just it just writes _id : DocumentID any suggestions ? thnx!
@PeterFriese
@PeterFriese 2 жыл бұрын
Feel free to send me a link to a gist with your code and I'll have a look.
@nat.serrano
@nat.serrano 2 жыл бұрын
let's say we want to fetch ONLY books from author X, and in our books collection we have an "authors" array field that contains two authors= X & Y, how to query this?
@nat.serrano
@nat.serrano 2 жыл бұрын
I'm using this but not retrieving any result/documents db.collection("conversations").whereField("users", arrayContains: user!.displayName).addSnapshotListener({ [](snapshot, error)
@nat.serrano
@nat.serrano 2 жыл бұрын
never mind it's actually fetching but I wasn't using the right model in my view lol
@PeterFriese
@PeterFriese 2 жыл бұрын
@@nat.serrano glad to hear it worked. One thing to keep in mind: force unwrapping the user to get their displayname might crash your app if the user is nil. Better to use a authentication state change listener and drive a non-optional user property on your view model.
@danielwatson6529
@danielwatson6529 2 жыл бұрын
awesomne, what about updating a document?
@Firebase
@Firebase 2 жыл бұрын
Hi, Daniel! you can find more on how to update documents with this link 🙂: firebase.google.com/docs/firestore/manage-data/add-data#update-data. You might also want to check out Peter's comprehensive guide about mapping Firestore documents from/to Swift using Codable: peterfriese.dev/posts/firestore-codable-the-comprehensive-guide
@gerardsk8ordie
@gerardsk8ordie Жыл бұрын
There is no method called "data(as:)"
@askforlearning9469
@askforlearning9469 2 жыл бұрын
I'm use import FirebaseFirestore import FirebaseFirestoreSwift and create document On FirebaseFirestore with stract Codable but connot ( .updateData or .setValue or .setData , merge ) give me error setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key urlImages. I'm sure the name is the same in modle and Object and On site Firebase. how ( .updateData or .setValue or .setData , merge ) when use stract Codabl , Because it does not work in any way ?
@askforlearning9469
@askforlearning9469 2 жыл бұрын
struct Note: Codable { @DocumentID var iddecument: String? = UUID().uuidString var dateNote: String var tagNote: String var titilNote: String var textNote: String var timeFirebase: String var urlImages: [InfoImages]? enum CodingKeys: String, CodingKey { case iddecument case dateNote case tagNote case titilNote case textNote case timeFirebase case urlImages } } struct InfoImages: Codable { var nameImage: String var urlImage: String enum CodingKeys: String, CodingKey { case nameImage case urlImage } }
@askforlearning9469
@askforlearning9469 2 жыл бұрын
iam connot update urlImages Because it struct InfoImages: Codable How ( .updateData or .setValue or .setData , merge )
@askforlearning9469
@askforlearning9469 2 жыл бұрын
when use var urlImages: [[String: String]]? give me reason: 'Nested arrays are not supported , How fix this issue ? iam need fix it with var urlImages: [InfoImages]? but connot !
Cloud Firestore Get Data (and other operations) with SwiftUI
41:31
CodeWithChris
Рет қаралды 41 М.
Heartwarming Unity at School Event #shorts
00:19
Fabiosa Stories
Рет қаралды 25 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 16 МЛН
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 22 МЛН
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 4,3 МЛН
What's going on with Windows Laptops?
10:30
Marques Brownlee
Рет қаралды 2,6 МЛН
Connect Firebase to Xcode: A Step-by-Step Tutorial | Firebase Bootcamp #1
21:07
Unit testing security rules with the Firebase Emulator Suite
31:24
Firestore Data Modeling - Five Cool Techniques
11:44
Fireship
Рет қаралды 254 М.
The Story of Next.js
12:13
uidotdev
Рет қаралды 560 М.
Faster Firestore via Data Aggregation
9:28
Fireship
Рет қаралды 61 М.
iPhone 15 Pro в реальной жизни
24:07
HUDAKOV
Рет қаралды 495 М.
Yanlışlıkla Telefonumu Parçaladım!😱
0:18
Safak Novruz
Рет қаралды 588 М.
Опасность фирменной зарядки Apple
0:57
SuperCrastan
Рет қаралды 12 МЛН