Giter VIP home page Giter VIP logo

codablefirebase's Introduction

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"

codablefirebase's People

Contributors

marcosgriselli avatar serjooo avatar netspencer avatar alickbass avatar inquisitivesoft avatar netizen01 avatar wiingaard avatar natan avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.