Search in sources :

Example 16 with Query

use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.

the class BriteDatabaseTest method querySubscribedToDuringTransactionOnDifferentThread.

@Test
public void querySubscribedToDuringTransactionOnDifferentThread() throws InterruptedException {
    Transaction transaction = db.newTransaction();
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {

        @Override
        public void run() {
            db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
            latch.countDown();
        }
    }.start();
    // Wait for the thread to block on initial query.
    Thread.sleep(500);
    o.assertNoMoreEvents();
    // Allow other queries to continue.
    transaction.end();
    // Wait for thread to observe initial query.
    latch.await(500, MILLISECONDS);
    o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").isExhausted();
}
Also used : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 17 with Query

use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.

the class ItemsFragment method onResume.

@Override
public void onResume() {
    super.onResume();
    String listId = String.valueOf(getListId());
    disposables = new CompositeDisposable();
    Observable<Integer> itemCount = // 
    db.createQuery(TodoItem.TABLE, COUNT_QUERY, listId).map(new Function<Query, Integer>() {

        @Override
        public Integer apply(Query query) {
            Cursor cursor = query.run();
            try {
                if (!cursor.moveToNext()) {
                    throw new AssertionError("No rows");
                }
                return cursor.getInt(0);
            } finally {
                cursor.close();
            }
        }
    });
    Observable<String> listName = db.createQuery(TodoList.TABLE, TITLE_QUERY, listId).map(new Function<Query, String>() {

        @Override
        public String apply(Query query) {
            Cursor cursor = query.run();
            try {
                if (!cursor.moveToNext()) {
                    throw new AssertionError("No rows");
                }
                return cursor.getString(0);
            } finally {
                cursor.close();
            }
        }
    });
    disposables.add(Observable.combineLatest(listName, itemCount, new BiFunction<String, Integer, String>() {

        @Override
        public String apply(String listName, Integer itemCount) {
            return listName + " (" + itemCount + ")";
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {

        @Override
        public void accept(String title) throws Exception {
            getActivity().setTitle(title);
        }
    }));
    disposables.add(db.createQuery(TodoItem.TABLE, LIST_QUERY, listId).mapToList(TodoItem.MAPPER).observeOn(AndroidSchedulers.mainThread()).subscribe(adapter));
}
Also used : Query(com.squareup.sqlbrite3.SqlBrite.Query) Cursor(android.database.Cursor) Consumer(io.reactivex.functions.Consumer) BiFunction(io.reactivex.functions.BiFunction) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Aggregations

Query (com.squareup.sqlbrite3.SqlBrite.Query)15 Test (org.junit.Test)14 Cursor (android.database.Cursor)6 MatrixCursor (android.database.MatrixCursor)6 Transaction (com.squareup.sqlbrite3.BriteDatabase.Transaction)4 SimpleSQLiteQuery (android.arch.persistence.db.SimpleSQLiteQuery)3 Employee (com.squareup.sqlbrite3.TestDb.Employee)3 Function (io.reactivex.functions.Function)3 TestObserver (io.reactivex.observers.TestObserver)3 SdkSuppress (android.support.test.filters.SdkSuppress)2 QueryObservable (com.squareup.sqlbrite3.QueryObservable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TargetApi (android.annotation.TargetApi)1 SupportSQLiteOpenHelper (android.arch.persistence.db.SupportSQLiteOpenHelper)1 Configuration (android.arch.persistence.db.SupportSQLiteOpenHelper.Configuration)1 Factory (android.arch.persistence.db.SupportSQLiteOpenHelper.Factory)1 FrameworkSQLiteOpenHelperFactory (android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory)1 ContentObserver (android.database.ContentObserver)1 CheckResult (android.support.annotation.CheckResult)1