![](/img/trans.png)
[英]Authentication with Architecture Components MVVM, passing token from Repository to ViewModel
[英]Unable to switch to MainActivity after data insertion from repository in MVVM architecture
我正在為應用程序開發實現MVVM架構模式。我有兩個活動MainActivity和AddUser活動。在MainActivity中,我顯示所有注釋的列表,在AddUser活動中,我將用戶插入房間數據庫中。我正在執行所有插入和獲取注釋操作。我正在使用RxJava Completable運算符在數據庫中插入注釋。
我想要的是:插入后,它應該重定向到MainActivity。
問題:當我使用Intent時,無法在onComplete()方法中添加Intent,它顯示錯誤。
錯誤
Process: com.app.notesreactive, PID: 3970
io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with.
下面是我的代碼:
UserDao.java
@Dao
public interface UserDao {
@Insert
void insert(User user);
@Query("SELECT * FROM Users ORDER BY id DESC")
LiveData<List<User>> getAllUsers();
}
UserRepository.java
public class UserRepository {
private UserDb userDb;
private UserDao userDao;
private LiveData<List<User>> allUsers;
private Context ctx;
public UserRepository(Application application) {
userDb = UserDb.getInstance(application);
userDao = userDb.userDao();
allUsers = userDao.getAllUsers();
ctx = application.getApplicationContext();
}
public void insert(final User user){
Completable.fromAction(() -> userDb.userDao().insert(user))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
Intent i = new Intent(ctx,MainActivity.class);
ctx.startActivity(i);
Toast.makeText(ctx,"Data inserted", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
Toast.makeText(ctx,e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
public LiveData<List<User>> getAllUsers(){
return allUsers;
}
}
有人請幫助我如何達到預期的效果。任何幫助將不勝感激。
謝謝
從存儲庫類切換活動,我們需要向Intent添加以下標志。 這是更新的onComplete()方法。
@Override
public void onComplete() {
Intent i = new Intent(ctx,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
ctx.startActivity(i);
Toast.makeText(ctx,"Data inserted", Toast.LENGTH_SHORT).show();
}
希望它會有所幫助。
謝謝
在存儲庫中注視活動不是一個好習慣。 您需要在一個活動中開始一個活動。 如果要使用應用程序上下文啟動活動,則應使用Intent.FLAG_ACTIVITY_NEW_TASK
。 導致使用默認標志從應用程序的Context
啟動活動會制動活動的后堆棧 (層次結構)。 我建議使用像RxJava的觀測 Observable
或LiveData
從AAC通知活動,並開始在它的另一個活動。 如何使用LivaData
?
更新了insert
方法,如下所示:
public LiveData<Boolean> insert(final User user){
MutableLiveData<Boolean> isComplated = new MutableLiveData<>();
Completable.fromAction(() -> userDb.userDao().insert(user))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
isComplated.setValue(true);
Toast.makeText(ctx,"Data inserted", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
Toast.makeText(ctx,e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
return isComplated;
}
在AddUser
活動類中觀察此情況:
viewModel.insert(myUser).observe(this, isComplated -> {
if (isComplated != null && isComplated) {
Intent i = new Intent(AddUser.this, MainActivity.class);
ctx.startActivity(i);
}
});
編輯: ViewModel類應包含以下方法:
public LiveData<Boolean> insert(User user) {
return userRepositoryInstance.insert(user);
}
它應該幫助=)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.