mirror of
https://github.com/caoer/CodableFirebase.git
synced 2026-04-01 12:23:16 +08:00
cbf348f01d950c8b2157f8e703a9436f5325190c
CodableFirebase
Overview
This library helps you to use your custom type that conform to Codable protocol with Firebase. Here's an example of model:
struct Model: Codable {
let stringExample: String
let booleanExample: Bool
let numberExample: Double
let dateExample: Date
let arrayExample: [String]
let nullExample: Int?
let objectExample: [String: String]
}
And this is how you would encode it with Firebase Firestore:
import Firebase
import CodableFirebase
let model: Model // here you will create an instance of Model
let docData = try! FirestoreEncoder().encode(model)
Firestore.firestore().collection("data").document("one").setData(docData) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
And this is how you would decode the same model with Firebase Firestore:
Firestore.firestore().collection("data").document("one").getDocument { (document, error) in
if let document = document {
let model = try! FirestoreDecoder().decode(Model.self, from: document.data())
print("Model: \(model)")
} else {
print("Document does not exist")
}
}
Languages
Swift
98.5%
Ruby
1.1%
Objective-C
0.4%