Search in sources :

Example 1 with User

use of com.pushtorefresh.storio3.sqlite.integration.User 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);
}
Also used : Disposable(io.reactivex.disposables.Disposable) Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with User

use of com.pushtorefresh.storio3.sqlite.integration.User in project storio by pushtorefresh.

the class RxQueryTest method updateEmission.

@Test
public void updateEmission() {
    final List<User> users = putUsersBlocking(10);
    final Queue<List<User>> expectedUsers = new LinkedList<List<User>>();
    final List<User> updatedList = new ArrayList<User>(users.size());
    int count = 1;
    for (User user : users) {
        updatedList.add(User.newInstance(user.id(), "new_email" + count++));
    }
    expectedUsers.add(users);
    expectedUsers.add(updatedList);
    final EmissionChecker emissionChecker = new EmissionChecker(expectedUsers);
    final Disposable disposable = emissionChecker.subscribe();
    // Should receive all users
    emissionChecker.awaitNextExpectedValue();
    storIOSQLite.put().objects(updatedList).prepare().executeAsBlocking();
    // Should receive updated users
    emissionChecker.awaitNextExpectedValue();
    emissionChecker.assertThatNoExpectedValuesLeft();
    disposable.dispose();
}
Also used : Disposable(io.reactivex.disposables.Disposable) AbstractEmissionChecker(com.pushtorefresh.storio3.test.AbstractEmissionChecker) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with User

use of com.pushtorefresh.storio3.sqlite.integration.User in project storio by pushtorefresh.

the class ObserveChangesOfTagTest method updateEmission.

@Test
public void updateEmission() {
    final List<User> users = putUsersBlocking(10);
    final List<User> updated = new ArrayList<User>(users.size());
    for (User user : users) {
        updated.add(User.newInstance(user.id(), user.email()));
    }
    final Queue<Changes> expectedChanges = new LinkedList<Changes>();
    expectedChanges.add(Changes.newInstance(UserTableMeta.TABLE, UserTableMeta.NOTIFICATION_TAG));
    final EmissionChecker emissionChecker = new EmissionChecker(expectedChanges);
    final Disposable disposable = emissionChecker.subscribe();
    storIOSQLite.put().objects(updated).prepare().executeAsBlocking();
    // Should receive changes of Users table
    emissionChecker.awaitNextExpectedValue();
    emissionChecker.assertThatNoExpectedValuesLeft();
    disposable.dispose();
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) Disposable(io.reactivex.disposables.Disposable) AbstractEmissionChecker(com.pushtorefresh.storio3.test.AbstractEmissionChecker) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with User

use of com.pushtorefresh.storio3.sqlite.integration.User in project storio by pushtorefresh.

the class DeleteOperationDesignTest method deleteObjectBlocking.

@Test
public void deleteObjectBlocking() {
    User user = newUser();
    DeleteResult deleteResult = storIOSQLite().delete().object(user).withDeleteResolver(UserTableMeta.DELETE_RESOLVER).prepare().executeAsBlocking();
}
Also used : DeleteResult(com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult) Test(org.junit.Test)

Example 5 with User

use of com.pushtorefresh.storio3.sqlite.integration.User in project storio by pushtorefresh.

the class PutOperationDesignTest method putObjectBlocking.

@Test
public void putObjectBlocking() {
    User user = newUser();
    PutResult putResult = storIOSQLite().put().object(user).withPutResolver(UserTableMeta.PUT_RESOLVER).prepare().executeAsBlocking();
}
Also used : PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 Disposable (io.reactivex.disposables.Disposable)10 AbstractEmissionChecker (com.pushtorefresh.storio3.test.AbstractEmissionChecker)9 LinkedList (java.util.LinkedList)9 Changes (com.pushtorefresh.storio3.sqlite.Changes)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 NonNull (android.support.annotation.NonNull)4 Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)3 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)3 User (com.pushtorefresh.storio3.sample.db.entities.User)2 BaseTest (com.pushtorefresh.storio3.sqlite.integration.BaseTest)2 User (com.pushtorefresh.storio3.sqlite.integration.User)2 DeleteResult (com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult)2 RawQuery (com.pushtorefresh.storio3.sqlite.queries.RawQuery)2 TestSubscriber (io.reactivex.subscribers.TestSubscriber)2 Optional (com.pushtorefresh.storio3.Optional)1 TweetWithUser (com.pushtorefresh.storio3.sample.db.entities.TweetWithUser)1 UserWithTweets (com.pushtorefresh.storio3.sample.db.entities.UserWithTweets)1