Platformsios 9.0, osx 10.11, watchos 2.0, tvos 9.0, requires ARC

Use Codable with Firebase

Carthage compatible
Build Status


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 nullExample: Int?
    let objectExample: [String: String]
    let myEnum: MyEnum

Firebase 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)


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)
    } catch let error {

Firestore usage

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:
        print("Model: (model)")
    } else {
        print("Document does not exist")

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

In order to use these 2 types with 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.


CocoaPods (iOS 9+)

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

platform :ios, '9.0'

target 'MyApp' do
pod 'CodableFirebase'

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"

Latest podspec

    "name": "CodableFirebase",
    "version": "0.2.0",
    "summary": "Use Codable with Firebase",
    "description": "This library helps you use your custom models that conform to Codable protocol with Firebase Realtime Database and Firestore",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Oleksii Dykan": "[email protected]"
    "platforms": {
        "ios": "9.0",
        "osx": "10.11",
        "watchos": "2.0",
        "tvos": "9.0"
    "requires_arc": true,
    "source": {
        "git": "",
        "tag": "0.2.0",
        "branch": "master"
    "source_files": "CodableFirebase/*.swift",
    "swift_version": "4.1"

Pin It on Pinterest

Share This