MIT License

项目介绍

alickbass/CodableFirebase 是一个托管在 GitHub 上的开源项目,主要使用 Swift 编写,采用 MIT License 许可证,目前获得 693 个 Star、77 次 Fork。

仓库全名
alickbass/CodableFirebase
开源许可证
MIT License
Star 数
693
Fork 数
77
未关闭 Issue
31

README 摘要

CodableFirebase Use Codable with Firebase 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: Firebase Realtime Database usage This is how you would use the library with Firebase Realtime Database: And here is how you would read the same value from Firebase Realtime Database: Firebase Cloud Firestore usage This is how you would…

完整内容见 GitHub 原始 README 或本页 。

常见问题

CodableFirebase 用什么语言开发?

主要使用 Swift 编写,采用 MIT License 许可证,目前有 693 个 Star。

CodableFirebase 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 CodableFirebase 的源码?

可以直接克隆仓库:git clone https://github.com/alickbass/CodableFirebase.git,默认分支为 master;安装与运行步骤见仓库 README。

CodableFirebase 最近还在维护吗?

仓库最近一次更新为 2022年10月6日,当前有 31 个未关闭 Issue;Star 数为 693,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

Use Codable with Firebase

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
869
价值分
439
活跃状态
活跃

语言占比

Objective-C
Ruby
Swift

Release

README

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"

评论

暂无评论