0

So I'm attempting to parse a JSON response that returns the groups of a specific user. Everything is returned correctly and I try to add it to a mutable list which should be persistent across my whole file.

private var GROUPS: MutableList<GroupObject> = ArrayList()

And my RxJava call here

val getUserGroups = ApiProvider.getUserGroups()
    compositeDisposable.add(
            getUserGroups.getUserGroups(prefs!!.accessToken)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe ({
                        result ->
                        result.groups.mapTo(GROUPS) {
                            GroupObject (
                                    it.id, it.groupName
                            )
                        }
                    }, { error ->
                        error.printStackTrace()
                    })
    )

Everything looks good when I print out result.groups and even printing out GROUPS[0] gives me the coprrect information. However, anywhere outside of this compositeDisposable when I try to print out GROUPS[0] the app crashes and says its null. And I dealing with a blatant scope issue here? Does this compositeDisposable only save my data within the subscribe method itself? Any help here would be much appreciated.

Edit: To add further, the following code, in the first system.out the information is shown, in the second it is null.

val getUserGroups = ApiProvider.getUserGroups()
    compositeDisposable.add(
            getUserGroups.getUserGroups(prefs!!.accessToken)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe ({
                        result ->
                        result.groups.mapTo(GROUPS) {
                            GroupObject (
                                    it.id, it.groupName
                            )
                        }
                        System.out.println(GROUPS[0])
                    }, { error ->
                        error.printStackTrace()
                    })
    )
    System.out.println(GROUPS[0])
David Parks
  • 77
  • 1
  • 11
  • You should learn about synchronous and asynchronous. https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean – muthuraj Jul 10 '17 at 18:43
  • @muthuraj I'm somewhat familiar. In my current implementation does that mean I don't actually access the resulting values? How can I assign my asynchronously obtained values to the groups object? – David Parks Jul 10 '17 at 18:55

1 Answers1

0

Moving the instantiation of my view inside the async response resulted in the correct data displaying.

David Parks
  • 77
  • 1
  • 11
  • Yes this is because you're calling `System.out.println(GROUPS[0])` before your `Observable` has actually called `onNext`. As you guessed, its because your disposable is asynchronous. – StuStirling Jul 10 '17 at 20:53