Last active
August 29, 2015 14:17
-
-
Save yongjhih/f654381072bd9250d31d to your computer and use it in GitHub Desktop.
RxAsyncAwait.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref. http://news.dartlang.org/2015/03/dart-19-release-youve-been-await-ing-for.html?m=1 | |
Future<int> getCount(bool showCustomers) async { | |
try { | |
var database = await connectDb(); | |
var count = await database.queryEmployees().length; | |
if (showCustomers) { | |
count += await database.queryCustomers().length; | |
} | |
return count; | |
} on DbError catch (error, stack) { | |
print("DB Error: $error, $statck"); | |
return null; | |
} | |
} | |
Future<Database> connectDb(); | |
Future<List<User>> Database.queryEmployees(); | |
Future<List<User>> Database.queryCustomers(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Observable<Integer> getCount(boolean showCustomers) { | |
try { | |
Database database = connectDb().toBlocking().single(); | |
Observable<User> users = database.queryEmployees(); | |
if (showCustomers) users.concatWith(database.queryCustomers()); | |
return users.count(); | |
} catch (DbError e) { | |
e.printStackTrace(); | |
return Observable.just(null); | |
} | |
return Observable.empty(); | |
} | |
Observable<Database> connectDb(); | |
Observable<User> Database.queryEmployees(); | |
Observable<User> Database.queryCustomers(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// await vs. toBlocking().single() | |
Observable<Integer> getCount(boolean showCustomers) { | |
return Observable.just(showCustomers).map(b -> { | |
Database database = connectDb().toBlocking().single(); | |
int count = database.queryEmployees().count().toBlocking().single(); | |
if (b) { | |
count += database.queryCustomers().count().toBlocking().single(); | |
} | |
return count; | |
}).onErrorResumeNext(e -> { | |
if (e instanceof DbError) { | |
e.printStackTrace(); | |
return Observable.just(null); | |
} | |
return Observable.error(e); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// with flatMap() | |
Observable<Integer> getCount(boolean showCustomers) { | |
return Observable.just(showCustomers) | |
.flatMap(b -> { | |
Database database = connectDb().toBlocking().single(); | |
Observable<User> users = database.queryEmployees(); | |
if (b) users.concatWith(database.queryCustomers()); | |
return users.count(); | |
}) | |
.onErrorResumeNext(e -> { | |
if (e instanceof DbError) { | |
e.printStackTrace(); | |
return Observable.just(null); | |
} | |
return Observable.error(e); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// without toBlocking().single() | |
Observable<Integer> getCount(final boolean showCustomers) { | |
return connectDb() | |
.flatMap(database -> { | |
Observable<User> users = database.queryEmployees(); | |
if (showCustomers) users.concatWith(database.queryCustomers()); | |
return users.count(); | |
}) | |
.onErrorResumeNext(e -> { | |
if (e instanceof DbError) { | |
e.printStackTrace(); | |
return Observable.just(null); | |
} | |
return Observable.error(e); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// without final by zip() | |
Observable<Integer> getCount(boolean showCustomers) { | |
return Observable.zip(Observable.just(showCustomers), connectDb(), (b, database) -> { | |
Observable<User> users = database.queryEmployees(); | |
if (b) users.concatWith(database.queryCustomers()); | |
return users.count(); | |
}) | |
.flatMap(count -> count) | |
.onErrorResumeNext(e -> { | |
if (e instanceof DbError) { | |
e.printStackTrace(); | |
return Observable.just(null); | |
} | |
return Observable.error(e); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment