本文整理匯總了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];
}
示例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];
}
示例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));
}
示例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));
}
示例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));
}