[英]My Realm not update new data to realm database
我正在嘗試將數據更新到數據庫,但沒有任何反應。
當Setting.java為onPause()時,數據將更新
Setting.java Setting.java的某些部分
@Override
public void onPause(){
RealmUpdate realmUpdate = new RealmUpdate();
realmUpdate.upsertUserProfile( application.getEmail(),
textView_fname.getText().toString(), textView_lname.getText().toString(),
textView_gender.getText().toString(), textView_birthdate.getText().toString(),
"074123456");
Log.d(getClass().getSimpleName(), "Prepare to caretaker Profile");
realmUpdate.upsertCaretakerProfile( application.getEmail(),
textView_carefname.getText().toString(), textView_carelname.getText().toString(),
textView_careemail.getText().toString(), textView_caretel.getText().toString());
startActivity( new Intent( context, MainActivity.class) );
super.onPause();
}
我已經檢查過所有textView都能獲得價值。 因此,問題可能出在我的RealmUpdate類中。
RealmUpdate.java
public class RealmUpdate {
private Realm realm;
/**
*
* @param email Email of User that use in this application.
* @param fname First Name of User
* @param lname Last Name of User
* @param gender Gender of User
* @param birthdate Birth Date of User
* @param tel Telephone Number of User
*/
public void upsertUserProfile( final String email, final String fname, final String lname,
final String gender, final String birthdate, final String tel){
realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(@NonNull Realm realm){
try{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String[] arr_date = birthdate.split("/");
String real_date = arr_date[2] + "-" + arr_date[1] + "-" + arr_date[0];
Date date = format.parse(real_date);
UserProfile data = realm.where(UserProfile.class)
.equalTo("Email", email)
.findFirst();
if( data == null){
UserProfile userProfile = realm.createObject(UserProfile.class, email);
userProfile.setFName(fname);
userProfile.setLName(lname);
userProfile.setGender(gender);
userProfile.setBirthdate(date);
userProfile.setTel(tel);
return;
}
data.setFName(fname);
data.setLName(lname);
data.setGender(gender);
data.setBirthdate(date);
data.setTel(tel);
} catch(ParseException exception) {
exception.printStackTrace();
}
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
realm.close();
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(@NonNull Throwable error) {
realm.close();
}
});
if (!realm.isClosed())
realm.close();
}
public void upsertCaretakerProfile( final String email, final String cfname, final String clname
, final String cemail, final String ctel){
realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(@NonNull Realm realm) {
CaretakerProfile data = realm.where(CaretakerProfile.class)
.equalTo("Email", email)
.findFirst();
Log.d(getClass().getSimpleName(), email);
if ( data == null ){
CaretakerProfile cprofile = realm.createObject( CaretakerProfile.class, email);
cprofile.setcFName(cfname);
Log.d(getClass().getSimpleName(), cfname);
cprofile.setcLName(clname);
Log.d(getClass().getSimpleName(), clname);
cprofile.setcTel(ctel);
Log.d(getClass().getSimpleName(), ctel);
cprofile.setcEmail(cemail);
Log.d(getClass().getSimpleName(), cemail);
Log.d(RealmUpdate.class.getSimpleName(), "Create Caretaker Profile Object");
return;
}
data.setcFName(cfname);
Log.d(getClass().getSimpleName(), cfname);
data.setcLName(clname);
Log.d(getClass().getSimpleName(), clname);
data.setcEmail(cemail);
Log.d(getClass().getSimpleName(), cemail);
data.setcTel(ctel);
Log.d(getClass().getSimpleName(), ctel);
Log.d(getClass().getSimpleName(), "Add/change profile");
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
realm.close();
Log.d(RealmUpdate.class.getSimpleName(), "Caretaker Profile add/change complete!!!");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(@NonNull Throwable error) {
realm.close();
}
});
if ( !realm.isClosed())
realm.close();
}
}
我的問題是方法upsertProfile可以更新數據,但是upsertCaretakerProfile無法。 logcat中也沒有錯誤。
非常抱歉問一個廢話。 現在,我發現了自己的錯誤,我的錯誤是由於我未發布的初始方法。
public void initialValue(){
RealmSearch search = new RealmSearch();
UserProfile profile = search.searchUserProfile(application.getEmail());
Log.d(getClass().getSimpleName(), application.getEmail());
textView_fname.setText( profile.getFName());
textView_lname.setText( profile.getLName());
textView_birthdate.setText( (new SimpleDateFormat("dd/MM/yyyy")).format( profile.getBirthdate()) );
textView_gender.setText( profile.getGender());
textView_tel.setText( profile.getTel());
CaretakerProfile cprofile = search.searchCaretakerProfile(application.getEmail());
textView_carefname.setText( cprofile.getcFName());
textView_carelname.setText( cprofile.getcLName());
textView_careemail.setText( cprofile.getcEmail());
textView_caretel.setText( cprofile.getcTel());
}
在編輯代碼之前,我將使用其類的Object作為初始cprofile,因此它具有空值,因為cprofile不會從數據庫查詢數據。
CaretakerProfile cprofile = new CaretakerProfile()
最后,由於這是一個無聊的問題,我將標記此職位以關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.