Latest 1.0.0
Homepage https://github.com/quanvo87/FirebaseHelper
License MIT
Platforms ios 8.0
Dependencies Firebase/Database
Authors ,

Version
License
Platform

FirebaseHelper is a small wrapper over Firebase’s realtime database, providing streamlined methods for get, set, delete, and increment values.

Features

  • [x] Setup Firebase Realtime Database Ref
  • [x] Read values (get)
  • [x] Write/Update values (set)
  • [x] Remove values (delete)
  • [x] Increment values (increment)

Requirements

Swift 4

Installation

FirebaseHelper is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'FirebaseHelper'

Usage

Initialize an instance of FirebaseHelper:

import Firebase
import FirebaseHelper

let firebaseHelper = FirebaseHelper(FirebaseDatabase.Database.database().reference())

FirebaseHelper(_ ref: DatabaseReference) takes in a DatabaseReference. Generally you’d want this to be the root of your database.

For convenience, you can add something like this to your project:

extension FirebaseHelper {
    static let main: FirebaseHelper = {
        FirebaseHelper(FirebaseDatabase.Database.database().reference())
    }()
}

And now you can simply call FirebaseHelper.main to access this instance of FirebaseHelper from anywhere in your project.

Common Database Operations

Get

Example:

FirebaseHelper.main.get("users", "john123", "favoriteFood") { result in
    switch result {
      case .failure(let error):
        // handle error
      case .success(let data):
        // get string from data
    }
}

API:

public func get(_ first: String, _ rest: String..., completion: @escaping (Result<DataSnapshot, Error>) -> Void)

get() takes in a variadic parameter of child nodes that will be built on the DatabaseReference you initialized your instance of FirebaseHelper on.

The callback returns a Result:

public enum Result<T, Error> {
    case success(T)
    case failure(Error)
}

In this case, T is DataSnapshot. An error case will either be because an invalid child was passed in or some other error in your database.

Set, Delete, Increment

set(), delete(), and increment() work similarly, but instead of returning a Result, they simply return an Error if one occurred, or nil otherwise.

Examples:

// The first parameter is an `Any` that gets set at the specified path.
FirebaseHelper.main.set("pizza", at: "users", "john123", "favoriteFood") { error in
    if let error = error {
      // handle error
  }
}

FirebaseHelper.main.delete("users", "john123", "favoriteFood") { error in
    if let error = error {
      // handle error
  }
}

FirebaseHelper.main.increment(by: 5, "users", "john123", "favoriteFoodEatenThisMonth") {
    if let error = error {
      // handle error
  }
}

APIs:

public func set(_ value: Any, at first: String, _ rest: String..., completion: @escaping (Error?) -> Void)

public func delete(_ first: String, _ rest: String..., completion: @escaping (Error?) -> Void)

public func increment(by amount: Int, at first: String, _ rest: String..., completion: @escaping (Error?) -> Void)

Note: You should only set() accepted value types. See Firebase docs.

Safely Make A DatabaseReference

You will often need to call more complex FirebaseDatabase functions, such as building a query and calling observe(_ eventType: with block:) on it. FirebaseHelper can still help with that:

let ref = try? FirebaseHelper.main.makeReference("users", "john123", "eatingHistory")
let handle = ref?.queryOrdered(byChild: "timestamp").queryLimited(toLast: 50).observe(.value) { data in
    // handle data
}

API:

public func makeReference(_ first: String, _ rest: String...) throws -> DatabaseReference

makeReference will throw an error if passed an invalid child.

Collaborators

License

FirebaseHelper is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "FirebaseHelper",
    "version": "1.0.0",
    "summary": "Safe and easy wrappers for common Firebase Database functions.",
    "description": "Safe and easy wrappers for common Firebase Database functions like get, set, delete, and increment. Also for creating a DatabaseReference with specified children added to its path. Filters the children and throws an error if a child key is invalid, such as a blank or double slashes. Without this check, Firebase Database crashes at runtime.",
    "homepage": "https://github.com/quanvo87/FirebaseHelper",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "quanvo87": "[email protected]",
        "Wilson Ding": "[email protected]"
    },
    "source": {
        "git": "https://github.com/quanvo87/FirebaseHelper.git",
        "tag": "1.0.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "swift_version": "4.0.3",
    "source_files": "FirebaseHelper/Classes/**/*",
    "static_framework": true,
    "dependencies": {
        "Firebase/Database": [
            "~> 4.1"
        ]
    }
}

Pin It on Pinterest

Share This