All Projects β†’ alickbass β†’ Codablefirebase

alickbass / Codablefirebase

Licence: mit
Use Codable with Firebase

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Codablefirebase

Easystash
πŸ—³Easy data persistence in Swift
Stars: ✭ 303 (-52.28%)
Mutual labels:  codable
Laravel Firebase
A Laravel package for the Firebase PHP Admin SDK
Stars: ✭ 369 (-41.89%)
Mutual labels:  firebase-database
Angular Shoppingcart
ShoppingCart (Ecommerce) πŸ›’ Application using Angular10, Firebase, PWA, Drag&Drop, Materialized Bootstrap and i18n πŸš€πŸ”₯πŸ‘¨β€πŸ’»
Stars: ✭ 483 (-23.94%)
Mutual labels:  firebase-database
Firebase Mock
Firebase mock library for writing unit tests
Stars: ✭ 319 (-49.76%)
Mutual labels:  firebase-database
Binarycodable
Swift Codable-like interfaces for binary representations.
Stars: ✭ 359 (-43.46%)
Mutual labels:  codable
Bamboots
Bamboots - Extension 4 Alamofire
Stars: ✭ 434 (-31.65%)
Mutual labels:  codable
Internalappstore
πŸ“¦ Manage your own internal Android App Store.
Stars: ✭ 295 (-53.54%)
Mutual labels:  firebase-database
Angular Commerce
Angular components for scaffolding online store
Stars: ✭ 526 (-17.17%)
Mutual labels:  firebase-database
Morecodable
MoreCodable expands the possibilities of `Codable`.
Stars: ✭ 366 (-42.36%)
Mutual labels:  codable
Swiftai
SwiftAI, write Swift code smart. SwiftAI can generate Model class from JSON now. Codable and HandyJSON is supported. More features will be add.
Stars: ✭ 470 (-25.98%)
Mutual labels:  codable
Firebase Js Sdk
Firebase Javascript SDK
Stars: ✭ 3,844 (+505.35%)
Mutual labels:  firebase-database
React Gatsby Firebase Authentication
🐣πŸ”₯Starter Project / Boilerplate for Authentication with Firebase and plain React in Gatsby.js
Stars: ✭ 356 (-43.94%)
Mutual labels:  firebase-database
Querybase
Bringing the where statement to the Firebase Database.
Stars: ✭ 455 (-28.35%)
Mutual labels:  firebase-database
Mechahamster
Mecha Hamster is a game where you roll through customizable environments that you can share with your friends.
Stars: ✭ 314 (-50.55%)
Mutual labels:  firebase-database
Meal Prep
Source code for a 4-part series I wrote about Vue, Vue Router, Vuex and Vuetify
Stars: ✭ 496 (-21.89%)
Mutual labels:  firebase-database
Defaultcodable
A convenient way to handle default values with Swift Codable types
Stars: ✭ 297 (-53.23%)
Mutual labels:  codable
Userdefaultsstore
Why not use UserDefaults to store Codable objects πŸ˜‰
Stars: ✭ 416 (-34.49%)
Mutual labels:  codable
Quickstart Unity
Firebase Quickstart Samples for Unity
Stars: ✭ 553 (-12.91%)
Mutual labels:  firebase-database
Rxfirebase
Rxjava 2.0 wrapper on Google's Android Firebase library.
Stars: ✭ 509 (-19.84%)
Mutual labels:  firebase-database
Xmlcoder
Easy XML parsing using Codable protocols in Swift
Stars: ✭ 460 (-27.56%)
Mutual labels:  codable

CodableFirebase

Use Codable with Firebase

CocoaPods Carthage compatible Build Status

Overview

This library helps you to use your custom types that conform to Codable protocol with Firebase. Here's an example of a custom model:

struct Model: Codable {
    enum MyEnum: Int, Codable {
        case one, two, three
    }
    
    let stringExample: String
    let booleanExample: Bool
    let numberExample: Double
    let dateExample: Date
    let arrayExample: [String]
    let optionalExample: Int?
    let objectExample: [String: String]
    let myEnumExample: MyEnum
}

Firebase Realtime Database usage

This is how you would use the library with Firebase Realtime Database:

import Firebase
import CodableFirebase

let model: Model // here you will create an instance of Model
let data = try! FirebaseEncoder().encode(model)

Database.database().reference().child("model").setValue(data)

And here is how you would read the same value from Firebase Realtime Database:

Database.database().reference().child("model").observeSingleEvent(of: .value, with: { snapshot in
    guard let value = snapshot.value else { return }
    do {
        let model = try FirebaseDecoder().decode(Model.self, from: value)
        print(model)
    } catch let error {
        print(error)
    }
})

Firebase Cloud Firestore usage

This is how you would encode a model with Firebase Cloud 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) { error in
    if let error = error {
        print("Error writing document: \(error)")
    } else {
        print("Document successfully written!")
    }
}

And this is how you would decode the same model with Firebase Cloud 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")
    }
}

How to use GeoPoint, DocumentRefence, FieldValue, Timestamp in Cloud Firestore

In order to use these types with Cloud Firestore, you need to add the following code somewhere in your app:

extension DocumentReference: DocumentReferenceType {}
extension GeoPoint: GeoPointType {}
extension FieldValue: FieldValueType {}
extension Timestamp: TimestampType {}

and now they become Codable and can be used properly with FirestoreEncoder and FirestoreDecoder.

PLEASE NOTE that as FieldValue is only used to setData() and updateData(), it only adopts the Encodable protocol.

Integration

CocoaPods (iOS 9+)

You can use CocoaPods to install CodableFirebase by adding it to your Podfile:

platform :ios, '9.0'
use_frameworks!

target 'MyApp' do
  pod 'CodableFirebase'
end

Note that this requires CocoaPods version 36, and your iOS deployment target to be at least 9.0:

Carthage (iOS 9+)

You can use Carthage to install CodableFirebase by adding it to your Cartfile:

github "alickbass/CodableFirebase"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].