我一直在努力了解RxJava 2以及如何使用它。我也试图通过将我的代码转换为RxJava代码来理解RxJava。但我似乎仍然无法理解它。如果有人能够进一步解释我如何重新编码它,我会真的很有帮助。
这是我试图在RxJava中编写的代码:
<?xml version='1.0' encoding='utf-8'?>
<widget id="my-id" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" versionCode = "10">
<name>My App</name>
<description>
My awesome description!
</description>
<author email="me@email.com" href="http://my-website.com">
Me
</author>
<platform name="android" />
<content src="index.html" />
<preference name="InAppBrowserStorageEnabled" value="true" />
<access origin="*" />
<icon src="www/img/icon.png" />
</widget>
我试图将getMyUsers()方法变成Observable。但我仍然对如何解决这个问题感到困惑。如果有人能指出我正确的方向,我将非常感激。 :)
答案 0 :(得分:1)
您应该可以执行以下操作:
public Observable<User> getMyUsers() {
return Observable.create(subscriber -> {
open();
try {
Cursor cursor = userLocalDatabase.query("User", null, null, null, null, null, null);
while (cursor.moveToNext()) {
User user = getUserFomCursor(cursor);
subscriber.onNext(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
subscriber.onCompleted();
});
}