Giter VIP home page Giter VIP logo

rx-tasks's Introduction

Android RxTasks

Simple and lightweight RxJava2 conversion for the Google Tasks APIs

The Tasks API

Starting with Google Play services version 9.0.0, you can use a Task API and a number of methods that return Task or its subclasses. Task is an API that represents asynchronous method calls, similar to PendingResult in previous versions of Google Play Services.

Usage

A common method that returns a Task is FirebaseAuth.signInAnonymously(). It returns a Task<AuthResult> which means the task will return an AuthResult object when it succeeds.

For example the Firebase sign in API asynchronously returns an AuthResult which can be consumed via toSingle method as an extension of Task<T>.

If consuming from Java code, the class RxTasks.toSingle<T>(task) can be used, this is marked as the direct extensions SingleTaskKt.toSingle<T>(Task<T>) and CompletableTaskKt.toCompletable(Task<*>) should be preferred.

It is currently only possible to create a Completable from a Task<Void>, as this honours the correct API behaviour and it is not the responsibility of this library to convert between reactive types.

FirebaseAuth
  .getInstance()
  .signInAnonymously()
  .toSingle()
  .subscribe { /* ... */ }

Gradle

implementation 'io.ashdavies.rx.rxtasks:rx-tasks:+'

Kotlin DSL

implementation(group = 'io.ashdavies.rx.rxtasks', name = 'rx-tasks', version = '2.1.2')

Threading

Listeners attached to a thread are run on the application main (UI) thread by default. When attaching a listener, you can also specify an Executor that is used to schedule listeners.

You may pass an Executor to the Task extension functions to relay to the Tasks API, to specify that the Task listener will execute on the provided Executor.

FirebaseAuth
  .getInstance()
  .signInAnonymously()
  .toSingle(Executor { Thread(it).run() })
  .subscribe { /* ... */ }

Activity-scoped listeners

If you are listening for task results in an Activity, you may want to add activity-scoped listeners to the task. These listeners are removed during the onStop method of your Activity so that your listeners are not called when the Activity is no longer visible.

Additionally, you may pass an Activity to the Task extension function to relay to the Tasks API, which will result in the Task listener being scoped to the provided Activity and will be removed during onStop.

FirebaseAuth
  .getInstance()
  .signInAnonymously()
  .toSingle(activity)
  .subscribe { /* ... */ }

Future development

Further development for this libary has not been planned, and will soon become deprecated, it is recommended to use Kotlin Coroutines integration with Google Play Services Tasks API.

rx-tasks's People

Contributors

ashdavies avatar josh-burton avatar shipkit-org avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

rx-tasks's Issues

Feature Request: Allow scheduler to be supplied for receiving task results

Currently the task results are always delivered on the main thread, which is okay. It's easy enough to switch back off using observeOn, but it would be nice to be able to optionally avoid this thread jump if you know you're going right back onto the IO or computation scheduler to process results.

I can submit a PR for this if you like. I was thinking overloads of RxTasks (and corresponding extensions for Kotlin).

Can't seem to access v2.1.0

I have tried including the new version of this library in my project, but it cannot be resolved. Version 2.0.0 can be found just fine.

Doing a little poking around, it doesn't appear that 2.1.0 has been made available via Maven, nor has the Maven metadata been updated. Perhaps I'm wrong, and am missing something?

2.0.0 - Getting IllegalArgumentException when wrapping a Task<Void> into a Completable

I was using 1.2.0 previously but after upgrading to 2.0.0 I get an IAE when wrapping a Task<Void> in a Completable. Not sure if other Source types are affected. Anyway, here's my code that worked fine in 1.2.0 but crashes in 2.0.0:

val aFirestoreInsertionTask: Task<Void> = ...

RxTasks.completable(aFirestoreInsertionTask)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(...)

  java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter void
        at io.ashdavies.rx.rxtasks.CompletableEmitterSuccessListener.onSuccess(CompletableEmitterSuccessListener.kt)
        at io.ashdavies.rx.rxtasks.CompletableEmitterSuccessListener.onSuccess(CompletableEmitterSuccessListener.kt:6)
        at com.google.android.gms.tasks.zzj.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Compile statement incorrect

Hey Ash the compile statement in the readme is wrong - it should be:

compile 'io.ashdavies.rx:rx-tasks:1.1.3'

Task<T> extension functions are not available outside of module

Because the SingleTaskOnSubscribe and CompletableTaskOnSubscribe are internal, it prevents the extension functions in TaskExtensions from being available outside of the module. This, at least, on Kotlin 1.2.21.

Implementing the extensions instead using the public RxTasks functions resolves the issue.

Post 2.0.0 release after #10 merged?

The current README has been updated to reflect the extension methods on Task, e.g. toSingle() but there's no public release including it. Any plans to do so in the near future?

Null values

I've come across an issue using the fusedlocationprovider task with this library - it can emit null values.

I think the only way around this would be to wrap null values with some kind of optional in the library?

Is the Kotlin Scripting JVM dependency actually needed?

I'm wondering what is the 'org.jetbrains.kotlin:kotlin-scripting-jvm:1.3.11' dependency used for and do I need it on my runtime classpath when I use this library.

I downloaded this repository, commented out that dependency and ran ./gradlew clean assemble and build succeeded, so maybe it's not needed?

Edit: also executed all the tests and they ran fine.

It pulls in dependencies with known security vulnerabilities, which I'd prefer to avoid: https://nvd.nist.gov/vuln/search/results?form_type=Advanced&results_type=overview&search_type=all&cpe_vendor=cpe%3A%2F%3Ajetbrains&cpe_product=cpe%3A%2F%3Ajetbrains%3Akotlin

RxJava3 support

Hi.
Do you have any plans to update to RxJava3?

I see that you consider to deprecate the library completely in favour of coroutines ( #23 ), but I'd say the are many projects that continue using RxJava and don't plan/cannot migrate to coroutines.

Thanks

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.