[英]How do I update data from Observable without having to re-subscribe/add disposable where I can delay the rendering of layout.xml until data is loaded?
My ViewModel is returning nullfields for current User how to fix ? 我的ViewModel返回当前用户的nullfield如何解决 ?
Say I had the following Disposable. 说我有以下一次性用品。
getUserProfile
calls a Retrofit 2 API that returns Observable<Response<GetUserProfileResponseBody>
when the Activity is started in onStart
. getUserProfile
调用Retrofit 2 API,该API在Activity在onStart
启动时返回Observable<Response<GetUserProfileResponseBody>
。 Is this a one-time call to the Retrofit 2 API? 这是对Retrofit 2 API的一次性调用吗? Say the user profile has a bunch of posts and user details which is retrievable in the
Response<GetUserProfileResponseBody>
. 假设用户个人资料中有一堆帖子和用户详细信息,这些内容可以从
Response<GetUserProfileResponseBody>
检索。 I want the user profile to automatically update when stuff changes or gets added, and if it doesn't, I would just swipe down and call onRefresh
. 我希望用户配置文件在内容更改或添加时自动更新,如果没有更改,我只需向下滑动并调用
onRefresh
。 For example, say in onStart
I call the following code: 例如,在
onStart
说我调用以下代码:
public void load(String userId) {
GetUserProfileRequest request = new GetUserProfileRequest(userId);
Disposable disposable = mViewModel
.getUserProfile(request)
.subscribe((Response<GetUserProfileResponseBody> response) -> {
if (response.body() != null) {
mViewModel.setCurrentUser(response.body().getCurrentUser());
}
}, (Throwable ex) -> {
});
mCompositeDisposable.addDisposable(disposable);
}
and then I go ahead and edit my profile (in the same activity so mCompositeDisposable.clear()
is not called). 然后继续编辑我的个人资料(在同一活动中,因此不会调用
mCompositeDisposable.clear()
)。 Afterwards, I expect the changes to reflect immediately in the user profile. 之后,我希望所做的更改会立即反映在用户个人资料中。 Do I have to call
load(userId
) one more time, which in turns adds another disposable to composite disposable, or will Observable
automatically detect the changes from the Retrofit 2 API to update the UI? 我是否必须再调用一次
load(userId
),这又将另一个一次性对象添加到复合一次性对象中,或者Observable
将自动检测Retrofit 2 API中的更改以更新UI? I want the Observable
that I'm subscribing to to get updated so I don't have to resubscribe. 我希望我订阅的
Observable
得到更新,所以我不必重新订阅。
Similarly, everytime I swipe refresh, I don't want to call all of load
which add another disposable either, I just want the thing I'm subscribing to to call the Retrofit 2 API again so I can have updated data in the SAME disposable. 同样,每次刷卡刷新时,我也不想调用所有会增加另一个一次性对象的
load
,我只想让我订阅的东西再次调用Retrofit 2 API,这样我就可以在SAME一次性对象中获得更新的数据。
Update 更新资料
In subscribe
I'm setting current user in viewmodel. 在
subscribe
我在viewmodel中设置当前用户。 At the same time, in a Fragment of this activity, I am accessing the viewmodel field in the layout.xml, but that field is null, because mViewModel.setCurrentUser
has not yet been called when the layout.xml is rendered. 同时,本次活动的一个片段,我访问视图模型字段中layout.xml,但字段为空,因为
mViewModel.setCurrentUser
尚未当layout.xml渲染调用。
<variable
name="viewModel"
type="com.skoolar.viewmodels.GetUserProfileViewModel" />
<ImageView
android:id="@+id/image_user_avatar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
How do I delay the rendering of the layout.xml until mViewModel.currentUser
is rednerred? 如何延迟对layout.xml的渲染,直到
mViewModel.currentUser
调整mViewModel.currentUser?
You can use background polling to automatically refresh the user posts on the profile every X seconds. 您可以使用后台轮询每X秒自动刷新一次个人资料上的用户帖子。 Lets say you have a
postService
to retrieve user wall feed. 假设您有一个
postService
来检索用户墙供稿。
Observable.interval(30, TimeUnit.SECONDS, Schedulers.io())
.map(tick -> postService.getRecentPost())
.subscribe(posts -> {
// the subscriber is called with List<Message> every time
// the remote service is polled
for (Post post : posts) {
log.info(post.toString())
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.