Reputation: 69
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])
Upvotes: 0
Views: 158
Reputation: 69
Moving the instantiation of my view inside the async response resulted in the correct data displaying.
Upvotes: 0