Java Code Examples for android.arch.lifecycle.LiveData#observeForever()

The following examples show how to use android.arch.lifecycle.LiveData#observeForever() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LiveDataTestUtil.java    From livedata-call-adapter with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
static <T> T getLiveDataValue(final LiveData<T> liveData, final long timeOutInSeconds)
        throws InterruptedException {
    final Object[] resultHolder = new Object[1];
    final CountDownLatch countDownLatch = new CountDownLatch(1);

    liveData.observeForever(new Observer<T>() {
        @Override
        public void onChanged(@Nullable T t) {
            resultHolder[0] = t;
            countDownLatch.countDown();
            liveData.removeObserver(this);
        }
    });
    countDownLatch.await(timeOutInSeconds, TimeUnit.SECONDS);
    if (resultHolder.length == 0) {
        throw new AssertionError("Unable to retrieve LiveData result");
    }
    return (T) resultHolder[0];
}
 
Example 2
Source File: LiveDataTestUtil.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
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];
}
 
Example 3
Source File: NewsRepository.java    From NewsApp with GNU General Public License v3.0 5 votes vote down vote up
public LiveData<List<Article>> getHeadlines(final Specification specs) {
    final LiveData<List<Article>> networkData = newsApiService.getHeadlines(specs);
    networkData.observeForever(new Observer<List<Article>>() {
        @Override
        public void onChanged(@Nullable List<Article> articles) {
            if (articles != null) {
                networkArticleLiveData.setValue(articles);
                networkData.removeObserver(this);
            }
        }
    });
    return headlinesDao.getArticleByCategory(specs.getCategory());
}
 
Example 4
Source File: NewsRepository.java    From NewsApp with GNU General Public License v3.0 5 votes vote down vote up
public LiveData<List<Source>> getSources(final Specification specs) {
    final LiveData<List<Source>> networkData = newsApiService.getSources(specs);
    networkData.observeForever(new Observer<List<Source>>() {
        @Override
        public void onChanged(@Nullable List<Source> sources) {
            if (sources != null) {
                networkSourceLiveData.setValue(sources);
                networkData.removeObserver(this);
            }
        }
    });
    return sourcesDao.getAllSources();
}
 
Example 5
Source File: SavedNewsService.java    From NewsApp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Handle action Foo in the provided background thread with the provided
 * parameters.
 */
private void handleActionNext(final int currentIndex) {
    final LiveData<List<Article>> articlesLiveData = NewsRepository.getInstance(getApplicationContext()).getSaved();
    articlesLiveData.observeForever(new Observer<List<Article>>() {
        @Override
        public void onChanged(@Nullable List<Article> articles) {
            if (articles != null && articles.size() > currentIndex + 1) {
                handleUpdateWidgets(articles, currentIndex + 1);
                articlesLiveData.removeObserver(this);
            }
        }
    });
}
 
Example 6
Source File: SavedNewsService.java    From NewsApp with GNU General Public License v3.0 5 votes vote down vote up
private void handleActionPrevious(final int currentIndex) {
    final LiveData<List<Article>> articlesLiveData = NewsRepository.getInstance(getApplicationContext()).getSaved();
    articlesLiveData.observeForever(new Observer<List<Article>>() {
        @Override
        public void onChanged(@Nullable List<Article> articles) {
            if (articles != null && articles.size() > 0 && currentIndex > 0) {
                handleUpdateWidgets(articles, currentIndex - 1);
                articlesLiveData.removeObserver(this);
            }
        }
    });
}
 
Example 7
Source File: TestFeatureLiveDataRuntime.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * Test run JQL 1.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testRunJQL1() throws InterruptedException {
	LiveData<List<Person>> liveData = BindApp1DataSource.getInstance().getDaoPerson1().selectAll();
	
	liveData.observeForever(new Observer<List<Person>>() {
		
		@Override
		public void onChanged(List<Person> t) {
			log(">> observed changes for person. Now thera are %s", t.size());
			
		}
	});
	
	// https://www.adityathakker.com/android-content-observer-react-on-content-change/
	MyObserver myObserver = new MyObserver(new Handler());		
	getApplicationContext().getContentResolver().registerContentObserver(BindApp1ContentProvider.URI_PERSON_SELECT_ALL, true, myObserver);
	
	
	insertRows(10);
	
	
	Thread.sleep(2000);
}