简体   繁体   English

如何从Observable更新数据,而不必重新订阅/添加可处理对象,而我可以在可以加载数据的情况下延迟layout.xml的渲染?

[英]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.

相关问题 当我在“layout.xml”文件中添加工具栏时,Android Studio RecyclerView 不会显示我的数据 - Android Studio RecyclerView wont show my data when i add toolbar at my `layout.xml` file layout.xml 文件在我使缓存无效并重新启动之前无法识别 - layout.xml files not recognized until I invalidate cache and restart 在调用setContentView(View)函数之前,如何通过代码自定义我的layout.xml? - How can I customize my layout.xml from code before calling setContentView(View) function? 是否可以重新订阅Retrofit 2 observable? - Is it possible to re-subscribe to a Retrofit 2 observable? 如何以编程方式将 layout.xml 添加到另一个 layout.xml - How to add layout.xml to another layout.xml programmatically RxAndroid 重新订阅 Observable onError 和 onComplete - RxAndroid Re-Subscribe to Observable onError and onComplete 如何等待 Preferences DataStore 中的数据加载完毕? - How do I wait until data from Preferences DataStore is loaded? 我可以删除layout.xml文件中定义的片段吗? - Can I remove a fragment defined in a layout.xml file? 如何在android layout.xml文件中创建2行按钮 - How can I create 2 rows of buttons in android layout.xml file 从可观察对象订阅数据接收后如何更新视图? - How to update view after data received from subscribe on an observable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM