当前位置: 首页>>代码示例>>Java>>正文


Java LiveData.removeObserver方法代码示例

本文整理汇总了Java中android.arch.lifecycle.LiveData.removeObserver方法的典型用法代码示例。如果您正苦于以下问题:Java LiveData.removeObserver方法的具体用法?Java LiveData.removeObserver怎么用?Java LiveData.removeObserver使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.arch.lifecycle.LiveData的用法示例。


在下文中一共展示了LiveData.removeObserver方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getValue

import android.arch.lifecycle.LiveData; //导入方法依赖的package包/类
/**
 * Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
 * Once we got a notification via onChanged, we stop observing.
 */
public static <T> T getValue(final LiveData<T> liveData) throws InterruptedException {
  final Object[] data = new Object[1];
  final CountDownLatch latch = new CountDownLatch(1);
  Observer<T> observer = new Observer<T>() {
    @Override
    public void onChanged(@Nullable T o) {
      data[0] = o;
      latch.countDown();
      liveData.removeObserver(this);
    }
  };
  liveData.observeForever(observer);
  latch.await(2, TimeUnit.SECONDS);
  //noinspection unchecked
  return (T) data[0];
}
 
开发者ID:mapbox,项目名称:mapbox-plugins-android,代码行数:21,代码来源:LiveDataTestUtil.java

示例2: getValue

import android.arch.lifecycle.LiveData; //导入方法依赖的package包/类
public static <T> T getValue(LiveData<T> liveData) throws InterruptedException {
    final Object[] data = new Object[1];
    CountDownLatch latch = new CountDownLatch(1);
    Observer<T> observer = new Observer<T>() {
        @Override
        public void onChanged(@Nullable T o) {
            data[0] = o;
            latch.countDown();
            liveData.removeObserver(this);
        }
    };
    liveData.observeForever(observer);
    latch.await(2, TimeUnit.SECONDS);
    //noinspection unchecked
    return (T) data[0];
}
 
开发者ID:hbmartin,项目名称:firebase-chat-android-architecture-components,代码行数:17,代码来源:LiveDataTestUtil.java

示例3: getValue

import android.arch.lifecycle.LiveData; //导入方法依赖的package包/类
/**
 * Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds.
 * Once we got a notification via onChanged, we stop observing.
 */
public static <T> T getValue(final LiveData<T> liveData) throws InterruptedException {
    final Object[] data = new Object[1];
    final CountDownLatch latch = new CountDownLatch(1);
    Observer<T> observer = new Observer<T>() {
        @Override
        public void onChanged(@Nullable T o) {
            data[0] = o;
            latch.countDown();
            liveData.removeObserver(this);
        }
    };
    liveData.observeForever(observer);
    latch.await(2, TimeUnit.SECONDS);
    //noinspection unchecked
    return (T) data[0];
}
 
开发者ID:googlesamples,项目名称:android-architecture-components,代码行数:21,代码来源:LiveDataTestUtil.java


注:本文中的android.arch.lifecycle.LiveData.removeObserver方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。