Safe and easy wrappers for common Firebase Realtime Database functions.

Overview

FirebaseHelper

Version License Platform

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

Features

  • Setup Firebase Realtime Database Ref
  • Read values (get)
  • Write/Update values (set)
  • Remove values (delete)
  • 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.

You might also like...
Movies Information DataBase (Add - Delete - Edit - Search)

MoviesInformation Movies Information DataBase (Add - Delete - Edit - Search) This Code Provide Simple Program About Movies Information This Program Ca

A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.
A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.

QuickDB FileManager + CoreData ❗️ Save and Retrieve any thing in JUST ONE line of code ❗️ Fast usage dataBase to avoid struggling with dataBase comple

A simple order manager, created in order to try Realm database
A simple order manager, created in order to try Realm database

Overview A simple order manager, created in order to get acquainted with the features and limitations of the local Realm database. The project is writ

A sample application showcasing Vapor 4 connecting to an Oracle database using SwiftOracle package.

vapor-oracle A sample application showcasing Vapor 4 connecting to an Oracle database using SwiftOracle package. In this Vapor application, we create

A property wrapper for displaying up-to-date database content in SwiftUI views

@Query Latest release: November 25, 2021 • version 0.1.0 • CHANGELOG Requirements: iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+ • Swift 5.5+ /

BucketServer - Small API with SQLite database that saves notes for an iOS appliction called Bucket list

BucketList Server-Side Small API with SQLite database that saves notes for an iO

Innova CatchKennyGame - The Image Tap Fun Game with keep your scores using Core Database
Innova CatchKennyGame - The Image Tap Fun Game with keep your scores using Core Database

Innova_CatchKennyGame The Image Tap Fun Game with keep your scores using Core Da

PostgreSQL database adapter (ORM included)

PostgreSQL PostgreSQL adapter for Swift 3.0. Conforms to SQL, which provides a common interface and ORM. Documentation can be found there. Installatio

A Swift library to fetch the schema from a SQLite3 database.
A Swift library to fetch the schema from a SQLite3 database.

SQLite3Schema A small library to fetch the schema of a SQLite database. Contains a SQLite3 helper module for Linux. TBD. Uses just the raw SQLite API.

Releases(1.0.0)
Owner
Quan Vo
Software Engineer
Quan Vo
Safe and easy wrappers for RealmSwift

RealmWrapper RealmWrapper is wrapper library for RealmSwift in realm-cocoa If you use RealmWrapper, you can easily use UI update through Notification

DongHee Kang 72 Nov 1, 2022
A food delivery app using firebase as the database.

FDA-ONE Food Delivery Application is a mobile application that users can use to find the best restaurant around their location and order the meals the

Naseem Oyebola 0 Nov 28, 2021
A type-safe, protocol-based, pure Swift database offering effortless persistence of any object

There are many libraries out there that aims to help developers easily create and use SQLite databases. Unfortunately developers still have to get bogged down in simple tasks such as writing table definitions and SQL queries. SwiftyDB automatically handles everything you don't want to spend your time doing.

Øyvind Grimnes 489 Sep 9, 2022
Easy direct access to your database 🎯

OHMySQL ★★ Every star is appreciated! ★★ The library supports Objective-C and Swift, iOS and macOS. You can connect to your remote MySQL database usin

Oleg 210 Dec 28, 2022
Commands providing shortcuts to common Postgres introspection queries (Swift port of heroku-pg-extras)

Commands providing shortcuts to common Postgres introspection queries (Swift port of heroku-pg-extras)

Sven A. Schmidt 2 May 27, 2022
Listens to changes in a PostgreSQL Database and via websockets.

realtime-swift Listens to changes in a PostgreSQL Database and via websockets. A Swift client for Supabase Realtime server. Usage Creating a Socket co

Supabase 35 Dec 1, 2022
CodableCloudKit allows you to easily save and retrieve Codable objects to iCloud Database (CloudKit)

CodableCloudKit CodableCloudKit allows you to easily save and retrieve Codable objects to iCloud Database (CloudKit) Features ℹ️ Add CodableCloudKit f

Laurent Grondin 65 Oct 23, 2022
Realm is a mobile database: a replacement for Core Data & SQLite

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the iOS, macOS, tvOS & wa

Realm 15.7k Jan 1, 2023
WCDB is a cross-platform database framework developed by WeChat.

WCDB 中文版本请参看这里 WCDB is an efficient, complete, easy-to-use mobile database framework used in the WeChat application. It's currently available on iOS,

Tencent 9.6k Jan 8, 2023
Sync Realm Database with CloudKit

IceCream helps you sync Realm Database with CloudKit. It works like magic! Features Realm Database Off-line First Thread Safety Reactive Programming O

Soledad 1.8k Jan 6, 2023