0

What is the easiest way to implement the same behaviour on Android? I mean I would like to have my thread (not main) wait for network operations and execute them one after another. In javascript, there is simple way to implement this behaviour thanks to Promise.

   exports.someFunction = functions.https.onRequest((req, res) => {
        const userId = req.query.user
        //long time
        ref.orderByChild("parentId").equalTo(userId)
            .on("value", function (snapshot) {

                if (snapshot.val() === null) {
                    res.status(402)
                    return
                }
                snapshot.forEach(function (childSnapshot) {
                    const foo = childSnapshot.val();
                        getAccessToken().then(fistResponse => {

                            var json = JSON.parse(fistResponse);
                            accToken = json.access_token;

                            getMoreInformation(accToken, foo.bar).then(response => {
                                console.log(response)
                                if (isValid(response)) {
                                    res.status(200) 
                                } else {
                                    res.status(402)
                                }
                            })})})})});

Currently I am using interface, but I would like to learn other way.

I found there is something like Future in android and that is probably what I am looking for.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
svkaka
  • 3,942
  • 2
  • 31
  • 55
  • There is no wait operation in java, you need to use an interface – tyczj Jan 19 '18 at 14:34
  • what about [Future](https://developer.android.com/reference/java/util/concurrent/Future.html) I have mentioned. Haven't tried it yet, but I'll be back with more info. – svkaka Jan 19 '18 at 14:39

1 Answers1

2

On Android you'd use Tasks for that. In fact, most of the Firebase Android SDK returns Tasks, but unfortunately the Firebase Database API predates that and works with its own ValueEventListener class.

Firebase's Doug Stevenson is a master at Tasks and has:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807