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


Java LiveData.observeForever方法代码示例

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


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

示例4: loadRepoFromNetwork

import android.arch.lifecycle.LiveData; //导入方法依赖的package包/类
@Test
public void loadRepoFromNetwork() throws IOException {
    MutableLiveData<Repo> dbData = new MutableLiveData<>();
    when(dao.load("foo", "bar")).thenReturn(dbData);

    Repo repo = TestUtil.createRepo("foo", "bar", "desc");
    LiveData<ApiResponse<Repo>> call = successCall(repo);
    when(service.getRepo("foo", "bar")).thenReturn(call);

    LiveData<Resource<Repo>> data = repository.loadRepo("foo", "bar");
    verify(dao).load("foo", "bar");
    verifyNoMoreInteractions(service);

    Observer observer = mock(Observer.class);
    data.observeForever(observer);
    verifyNoMoreInteractions(service);
    verify(observer).onChanged(Resource.loading(null));
    MutableLiveData<Repo> updatedDbData = new MutableLiveData<>();
    when(dao.load("foo", "bar")).thenReturn(updatedDbData);

    dbData.postValue(null);
    verify(service).getRepo("foo", "bar");
    verify(dao).insert(repo);

    updatedDbData.postValue(repo);
    verify(observer).onChanged(Resource.success(repo));
}
 
开发者ID:googlesamples,项目名称:android-architecture-components,代码行数:28,代码来源:RepoRepositoryTest.java

示例5: loadContributors

import android.arch.lifecycle.LiveData; //导入方法依赖的package包/类
@Test
public void loadContributors() throws IOException {
    MutableLiveData<List<Contributor>> dbData = new MutableLiveData<>();
    when(dao.loadContributors("foo", "bar")).thenReturn(dbData);

    LiveData<Resource<List<Contributor>>> data = repository.loadContributors("foo",
            "bar");
    verify(dao).loadContributors("foo", "bar");

    verify(service, never()).getContributors(anyString(), anyString());

    Repo repo = TestUtil.createRepo("foo", "bar", "desc");
    Contributor contributor = TestUtil.createContributor(repo, "log", 3);
    // network does not send these
    contributor.setRepoOwner(null);
    contributor.setRepoName(null);
    List<Contributor> contributors = Collections.singletonList(contributor);
    LiveData<ApiResponse<List<Contributor>>> call = successCall(contributors);
    when(service.getContributors("foo", "bar"))
            .thenReturn(call);

    Observer<Resource<List<Contributor>>> observer = mock(Observer.class);
    data.observeForever(observer);

    verify(observer).onChanged(Resource.loading( null));

    MutableLiveData<List<Contributor>> updatedDbData = new MutableLiveData<>();
    when(dao.loadContributors("foo", "bar")).thenReturn(updatedDbData);
    dbData.setValue(Collections.emptyList());

    verify(service).getContributors("foo", "bar");
    ArgumentCaptor<List<Contributor>> inserted = ArgumentCaptor.forClass((Class) List.class);
    verify(dao).insertContributors(inserted.capture());


    assertThat(inserted.getValue().size(), is(1));
    Contributor first = inserted.getValue().get(0);
    assertThat(first.getRepoName(), is("bar"));
    assertThat(first.getRepoOwner(), is("foo"));

    updatedDbData.setValue(contributors);
    verify(observer).onChanged(Resource.success(contributors));
}
 
开发者ID:googlesamples,项目名称:android-architecture-components,代码行数:44,代码来源:RepoRepositoryTest.java

示例6: onCreate

import android.arch.lifecycle.LiveData; //导入方法依赖的package包/类
@Override
public void onCreate() {
    super.onCreate();

    PreferenceManager.setDefaultValues(this, R.xml.settings, false);

    enableStrictMode();

    SearchEngineManager.getInstance().init(this);

    TelemetryWrapper.init(this);

    registerActivityLifecycleCallbacks(visibilityLifeCycleCallback = new VisibilityLifeCycleCallback(this));

    final LiveData<List<Session>> sessions = SessionManager.getInstance().getSessions();
    sessions.observeForever(new TelemetrySessionObserver());
    sessions.observeForever(new CleanupSessionObserver(this));
}
 
开发者ID:mozilla-mobile,项目名称:firefox-tv,代码行数:19,代码来源:FocusApplication.java


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