Coder Social home page Coder Social logo

firebase-android-coroutines's Introduction

Deprecation notice

A better implementation has been merged into the official kotlin/kotlinx.coroutines repository.

Please, use the official implementation instead.

Module kotlinx-coroutines-firebase

Integration with Firebase Task and DatabaseReference for Android.

Extension functions:

Name Description
Task.await Awaits for completion of the Task and return the result
DatabaseReference.readValue Awaits for a single value to be retrieved from Firebase Database
DatabaseReference.readList Awaits for a List of values to be retrieved from Firebase Database

Example

Working with Firebase Authentication

The following code shows how to get an user from Firebase and delete it using two approaches.

The standard approach is to write nested callback functions.

val auth = FirebaseAuth.getInstance()

auth.getUserByEmail(email)
    .addOnSuccessListener { user ->
        auth.deleteUser(user.uid)
            .addOnSuccesListener { println("User deleted") }
            .addOnFailureListener { exception -> println(exception) }
    }
    .addOnFailureListener { exception -> println(exception) }

The other approach is the use of a suspended coroutine to make the code flatter, so we can call the await() method to wait the first asyncronous operations (retrieve the user) to complete before starting the second one (delete the user).

val auth = FirebaseAuth.getInstance()

try {
  val user = auth.getUserByEmail(email).await()

  auth.deleteUser(user.uid).await()

  println("User deleted")
} catch (exception: FirebaseAuthException) {
  println(exception)
}

Working with Firebase Database

Another usage of this module is to access nodes from a Firebase Database instance.

There are two suspend functions that encapsulate callbacks: readValue and readList.

The usage of both methods can be seen in the code below. It shows how to read a a single value using readValue to read the player name and readList function to read his/her last five points in the game.

val database = FirebaseDatabase.getInstance()
val gameNode = database.getReference("game")
 
val person = gameNode.child("player").readValue<String>()
val scores = gameNode.child("scores").readList<Int>()
 
println("Scores of $person")
scores.forEach(::println)

It's important to mention that since this modules exposes coroutines, they must be run inside a context like launch {}, async {} or runBlocking for testing purpose.

Test results

JUnit test report

Package kotlinx.coroutines.firebase.android

Integration with Firebase Task and DatabaseReference for Android.

firebase-android-coroutines's People

Contributors

lucasvsme avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

pstens rbryan21

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.