将代码转换为 RxJava2 代码
Convert Code To RxJava2 Code
我一直在努力理解 RxJava 2 以及如何使用它。我还试图通过将我的代码转换为 RxJava 代码来理解 RxJava。但我似乎仍然无法理解。如果有人能进一步解释我如何重新编码,我真的会提供帮助。
这是我尝试在 RxJava 中编写的代码:
private static User getUserFomCursor(Cursor cursor) {
if (cursor == null || cursor.getCount() == 0) {
return null;
} else {
int myUserIDColumnIndex = cursor.getColumnIndex("_USER_ID");
int usernameColumnIndex = cursor.getColumnIndex("USER_NAME");
int userPasswordColumnIndex = cursor.getColumnIndex("USER_PASSWORD");
int firstNameColumnIndex = cursor.getColumnIndex("FIRST_NAME");
int middleNameColumnIndex = cursor.getColumnIndex("MIDDLE_NAME");
int lastNameColumnIndex = cursor.getColumnIndex("LAST_NAME");
int emailAddressColumnIndex = cursor.getColumnIndex("EMAIL_ADDRESS");
int phoneNumberColumnIndex = cursor.getColumnIndex("PHONE_NUMBER");
int profilePictureColumnIndex = cursor.getColumnIndex("PROFILE_PICTURE");
try {
User user = new User(
cursor.getInt(myUserIDColumnIndex), cursor.getString(usernameColumnIndex),
cursor.getString(userPasswordColumnIndex), cursor.getString(firstNameColumnIndex),
cursor.getString(middleNameColumnIndex), cursor.getString(lastNameColumnIndex),
cursor.getString(emailAddressColumnIndex), cursor.getString(phoneNumberColumnIndex),
cursor.getString(profilePictureColumnIndex));
return user;
} catch (Exception e) {
return null;
}
}
}
public ArrayList<User> getMyUsers() {
User user = new User();
ArrayList <User> userArrayList = new ArrayList<User>();
open();
try {
Cursor cursor = userLocalDatabase.query("User", null, null, null, null, null, null);
while (cursor.moveToNext()) {
user = getUserFomCursor(cursor);
userArrayList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return userArrayList;
}
我试图将 getMyUsers() 方法变成一个 Observable。但我仍然对如何解决这个问题感到困惑。如果有人能指出正确的方向,我将不胜感激。 :)
您应该可以执行以下操作:
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();
});
}
我一直在努力理解 RxJava 2 以及如何使用它。我还试图通过将我的代码转换为 RxJava 代码来理解 RxJava。但我似乎仍然无法理解。如果有人能进一步解释我如何重新编码,我真的会提供帮助。
这是我尝试在 RxJava 中编写的代码:
private static User getUserFomCursor(Cursor cursor) {
if (cursor == null || cursor.getCount() == 0) {
return null;
} else {
int myUserIDColumnIndex = cursor.getColumnIndex("_USER_ID");
int usernameColumnIndex = cursor.getColumnIndex("USER_NAME");
int userPasswordColumnIndex = cursor.getColumnIndex("USER_PASSWORD");
int firstNameColumnIndex = cursor.getColumnIndex("FIRST_NAME");
int middleNameColumnIndex = cursor.getColumnIndex("MIDDLE_NAME");
int lastNameColumnIndex = cursor.getColumnIndex("LAST_NAME");
int emailAddressColumnIndex = cursor.getColumnIndex("EMAIL_ADDRESS");
int phoneNumberColumnIndex = cursor.getColumnIndex("PHONE_NUMBER");
int profilePictureColumnIndex = cursor.getColumnIndex("PROFILE_PICTURE");
try {
User user = new User(
cursor.getInt(myUserIDColumnIndex), cursor.getString(usernameColumnIndex),
cursor.getString(userPasswordColumnIndex), cursor.getString(firstNameColumnIndex),
cursor.getString(middleNameColumnIndex), cursor.getString(lastNameColumnIndex),
cursor.getString(emailAddressColumnIndex), cursor.getString(phoneNumberColumnIndex),
cursor.getString(profilePictureColumnIndex));
return user;
} catch (Exception e) {
return null;
}
}
}
public ArrayList<User> getMyUsers() {
User user = new User();
ArrayList <User> userArrayList = new ArrayList<User>();
open();
try {
Cursor cursor = userLocalDatabase.query("User", null, null, null, null, null, null);
while (cursor.moveToNext()) {
user = getUserFomCursor(cursor);
userArrayList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
close();
return userArrayList;
}
我试图将 getMyUsers() 方法变成一个 Observable。但我仍然对如何解决这个问题感到困惑。如果有人能指出正确的方向,我将不胜感激。 :)
您应该可以执行以下操作:
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();
});
}