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();
}
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));
}
Aggregations