Giter Site home page Giter Site logo

Comments (4)

i-haz-teh-codez avatar i-haz-teh-codez commented on June 15, 2024 1

Any progress almost a year later? The approach suggested by @norio-agoop is a starting point but it's an approach that doesn't scale that well with an increase in the number of queries — just too much onFailure() and onResponse() boilerplate.

Right now, I have a REST API for my Android app, and using Retrofit to make the HTTP calls. Now, Retrofit has a simple class, RxJava2CallAdapterFactory.java (~100 SLOC) which has a single dependency, CallAdapter.java (~50 SLOC). Those classes are responsible for converting the HTTP calls to observables. Without RxJava2CallAdapterFactory, the code looks like this (for brevity):

query.enqueue(new Callback<List<GitHubRepo>>() {  
    @Override
    public void onResponse(Call<List<GitHubRepo>> call, Response<List<GitHubRepo>> response) {}

    @Override
    public void onFailure(Call<List<GitHubRepo>> call, Throwable t) { }
});

But with RxJava2CallAdapterFactory included, it becomes:

query()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(success -> {}, failure -> {})

And that's a massive improvement! Is there something similar in the works for your SDK?

from aws-mobile-appsync-sdk-android.

scb01 avatar scb01 commented on June 15, 2024

@leijdekkers
We currently do not have support for Rx Java within AppSync.
I am adding @undefobj to this thread to comment on whether this would be a future roadmap item.

from aws-mobile-appsync-sdk-android.

leijdekkers avatar leijdekkers commented on June 15, 2024

Hope you will implement it soon. The reason is that in many Android apps, the AppSync part goes into the "Repository" part (i.e. Model in MVVM architecture) of the app where many developers use RxJava to communicate between the Repository component and the ViewModel. It is really bad practice to use the AppSync SDK in fragments/activities (i.e. the View part of the MVVM model).

Please have a look at Apollo where they already support RxJava. It should not be too hard to fully support it in AppSync SDK.

from aws-mobile-appsync-sdk-android.

 avatar commented on June 15, 2024

@leijdekkers I'm using Rx with AppSync. Please take a look my sample code below.

/**
     * getUser
     */
override fun getUser(userId: String): Single<GetUserQuery.GetUser> {
        Timber.d("GetUserQuery Request: %s", userId)
        return Single.create { emitter ->
            appSyncClient.query(GetUserQuery.builder().user_id(userId).build())
                .responseFetcher(AppSyncResponseFetchers.NETWORK_ONLY)
                .enqueue(getUserCallback(emitter))
        }
    }

private fun getUserCallback(emitter: SingleEmitter<GetUserQuery.GetUser>): GraphQLCall.Callback<GetUserQuery.Data> {
        return object : GraphQLCall.Callback<GetUserQuery.Data>() {
            override fun onFailure(e: ApolloException) {
                Timber.e(e)
                emitter.onError(e)
            }

            override fun onResponse(response: Response<GetUserQuery.Data>) {
                val result = response.data()?.user ?: run {
                    val e = Throwable("GetUserQuery Response is null")
                    Timber.e(e)
                    emitter.onError(e)
                    return
                }
                Timber.d("GetUserQuery Response: %s", result)
                emitter.onSuccess(result)
            }
        }
    }

from aws-mobile-appsync-sdk-android.

Related Issues (20)

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.