Popularity
0.7
Stable
Activity
0.0
Stable
15
3
1

Programming language: Swift
License: MIT License
Tags: Database    
Latest version: v1.0.0

FirebaseHelper alternatives and similar libraries

Based on the "Database" category.
Alternatively, view FirebaseHelper alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of FirebaseHelper or a related project?

Add another 'Database' Library

README

FirebaseHelper

<!--CI Status--> 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.


*Note that all licence references and agreements mentioned in the FirebaseHelper README section above are relevant to that project's source code only.