use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.
the class TweetsSQLiteFragment method reloadData.
void reloadData() {
uiStateController.setUiStateLoading();
final Disposable disposable = storIOSQLite.get().listOfObjects(Tweet.class).withQuery(QUERY_ALL).prepare().asRxFlowable(// it will be subscribed to changes in tweets table!
LATEST).delay(1, // for better User Experience :) Actually, StorIO is so fast that we need to delay emissions (it's a joke, or not)
SECONDS).observeOn(mainThread()).subscribe(new Consumer<List<Tweet>>() {
@Override
public void accept(List<Tweet> tweets) {
// So you just need to check if it's empty or not
if (tweets.isEmpty()) {
uiStateController.setUiStateEmpty();
tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
} else {
uiStateController.setUiStateContent();
tweetsAdapter.setTweets(tweets);
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
// In cases when you are not sure that query will be successful
// You can prevent crash of the application via error handler
Timber.e(throwable, "reloadData()");
uiStateController.setUiStateError();
tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
}
});
// Preventing memory leak (other rx operations: Put, Delete emit result once so memory leak won't live long)
// Because io.reactivex.Flowable from Get Operation is endless (it watches for changes of tables from query)
// You can easily create memory leak (in this case you'll leak the Fragment and all it's fields)
// So please, PLEASE manage your subscriptions
// We suggest same mechanism via storing all disposables that you want to dispose
// In something like CompositeSubscription and dispose them in appropriate moment of component lifecycle
disposeOnStop(disposable);
}
use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.
the class TweetsContentResolverFragment method reloadData.
void reloadData() {
uiStateController.setUiStateLoading();
final Disposable disposable = storIOContentResolver.get().listOfObjects(Tweet.class).withQuery(QUERY_ALL).prepare().asRxFlowable(// it will be subscribed to changes in tweets table!
LATEST).observeOn(mainThread()).subscribe(new Consumer<List<Tweet>>() {
@Override
public void accept(List<Tweet> tweets) {
// So you just need to check if it's empty or not
if (tweets.isEmpty()) {
uiStateController.setUiStateEmpty();
tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
} else {
uiStateController.setUiStateContent();
tweetsAdapter.setTweets(tweets);
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
// In cases when you are not sure that query will be successful
// You can prevent crash of the application via error handler
Timber.e(throwable, "reloadData()");
uiStateController.setUiStateError();
tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
}
});
// Preventing memory leak (other Flowables: Put, Delete emit result once so memory leak won't live long)
// Because io.reactivex.Flowable from Get Operation is endless (it watches for changes of tables from query)
// You can easily create memory leak (in this case you'll leak the Fragment and all it's fields)
// So please, PLEASE manage your subscriptions
// We suggest same mechanism via storing all disposables that you want to dispose
// In something like CompositeSubscription and dispose them in appropriate moment of component lifecycle
disposeOnStop(disposable);
}
use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.
the class PutOperationTest method updateContentValuesAsCompletable.
@Test
public void updateContentValuesAsCompletable() {
TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
Uri insertedUri = contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value").toContentValues());
TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value2");
storIOContentResolver.put().contentValues(testItem.toContentValues()).withPutResolver(testItemContentValuesPutResolver).prepare().asRxCompletable().blockingAwait(15, SECONDS);
Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
Assertions.assertThat(cursor).hasCount(1);
cursor.moveToFirst();
assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
changesTestSubscriber.assertNoErrors();
changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.
the class PutOperationTest method updateNullFieldToNotNull.
@Test
public void updateNullFieldToNotNull() {
TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
Uri insertedUri = // firstly, optional value is null
contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value", null).toContentValues());
// change to not null
TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value", "optionalValue");
PutResult updateResult = storIOContentResolver.put().object(testItem).prepare().executeAsBlocking();
assertThat(updateResult.wasUpdated()).isTrue();
Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
Assertions.assertThat(cursor).hasCount(1);
cursor.moveToFirst();
assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
changesTestSubscriber.assertNoErrors();
changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.
the class PutOperationTest method insertContentValuesAsRxFlowable.
@Test
public void insertContentValuesAsRxFlowable() {
TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
TestItem testItem = TestItem.create(null, "value");
ContentValues cv = testItem.toContentValues();
PutResult insertResult = storIOContentResolver.put().contentValues(cv).withPutResolver(testItemContentValuesPutResolver).prepare().asRxFlowable(BackpressureStrategy.MISSING).blockingFirst();
assertThat(insertResult.wasInserted()).isTrue();
Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
Assertions.assertThat(cursor).hasCount(1);
cursor.moveToFirst();
assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
changesTestSubscriber.assertNoErrors();
changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Aggregations