我有一个登录用户(Coach),并且在个人资料中,我想显示该实体的更多字段,例如程序计数等,来自网络调用,该字段将保存到Coach实体表中,而其他字段例如名称,用户ID不会更改。
我已经进行了调用,并且能够将所需的字段记录到logcat中,这意味着它们存在,但是当我尝试在db中更新其值时,它不能反映出来。我正在使用Stetho来查看数据库。
在片段中,我已调用存储库以使用如下的viewmodel获取和更新配置文件:
mViewModel = ViewModelProviders.of(this).get(ProfileViewModel.class);
mViewModel.getCoach();
viewmodel调用存储库
public Coach getCoach(){
coach = repository.getCoachProfile();
Log.d(TAG, "CPrCountVM:\t" + coach.programsCount);
return coach;
}
在存储库中,我调用了getCoachProfile方法来处理所有newtork和db交互,例如:
public class CoachRepository {
private static final String TAG = CoachRepository.class.getSimpleName();
public CoachDAO coachDAO;
private Context context;
private boolean hasLoggedIn = false;
private Coach coach;
public CoachRepository(Application application) {
SportsDatabase database = SportsDatabase.getInstance(application);
coachDAO = database.coachDAO();
context = application.getApplicationContext();
}
public boolean attemptLogin(){
Call<CoachDetails> call = RestClient.getRestInstance().getLoginService()
.loginCoach("snehil@hi5academy.com", "123@abcd");
call.enqueue(new Callback<CoachDetails>() {
@Override
public void onResponse(Call<CoachDetails> call, Response<CoachDetails> response) {
if (response.isSuccessful()){
hasLoggedIn = true;
CoachDetails details = response.body();
Coach coach = details.getCoach();
Log.d(TAG, "Nick:\t" + coach.nickName);
Log.d(TAG, "gender:\t" + coach.gender);
new PrefsUtils(context).saveCoachId(coach.coachId);
new PrefsUtils(context).saveLoginStatus(hasLoggedIn);
context.startActivity(new Intent(context, MainActivity.class));
saveCoachBio(coach);
}
}
@Override
public void onFailure(Call<CoachDetails> call, Throwable t) {
Toast.makeText(context, "Login Failed", Toast.LENGTH_SHORT).show();
}
});
return hasLoggedIn;
}
public Coach getCoachProfile(){
Call<CoachDetails> call = RestClient.getRestInstance().getCoachBioService()
.profileCoach(new PrefsUtils(context).getCoachId());
call.enqueue(new Callback<CoachDetails>() {
@Override
public void onResponse(Call<CoachDetails> call, Response<CoachDetails> response) {
if (response.isSuccessful()){
Log.d(TAG, "Got Profile Response");
CoachDetails details = response.body();
coach = details.getCoach();
Log.d(TAG, "coach id:\t" + coach.coachId);
Log.d(TAG, "coach programs count:\t" + coach.programsCount);
updateCoachBio(coach);
}
}
@Override
public void onFailure(Call<CoachDetails> call, Throwable t) {
}
});
return coach;
}
public void saveCoachBio(Coach coach) {
new SaveCoachBioTask(coachDAO).execute(coach);
}
public void updateCoachBio(Coach coach) {
new UpdateCoachBioTask(coachDAO).execute(coach);
}
public class SaveCoachBioTask extends AsyncTask<Coach, Void, Void> {
private CoachDAO dao;
public SaveCoachBioTask(CoachDAO dao) {
this.dao = dao;
}
@Override
protected Void doInBackground(Coach... coaches) {
dao.saveCoachBio(coaches[0]);
return null;
}
}
public class UpdateCoachBioTask extends AsyncTask<Coach, Void, Void> {
private CoachDAO dao;
public UpdateCoachBioTask(CoachDAO dao) {
this.dao = dao;
}
@Override
protected Void doInBackground(Coach... coaches) {
dao.updateCoachBio(coaches[0]);
return null;
}
}
}
这是我的dao界面,用于定义:
@Dao
public interface CoachDAO {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void saveCoachBio(Coach coach);
@Update
void updateCoachBio(Coach coach);
// @Query("select * from coach_bio where coachId = :id")
// LiveData<Coach> getCoachId(String id);
//
@Query("select * from coach_bio where coachId = :id limit 1")
Coach getCoachDetails(String id);
// @Query("select coachId from coach_bio")
// String id;
}
有人可以帮助我了解为什么更新不起作用。我也尝试过在更新之前按ID和手动设置字段进行查找,但这是行不通的。在这种情况下最好的方法是什么? 谢谢。