I'm trying to update data to database but nothing happen.
Data will update when Setting.java is onPause()
Setting.java some part of 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();
}
I'm already check that all textView can get value. So the problem might be in my RealmUpdate class.
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();
}
}
My problem is that method upsertProfile can update data, but upsertCaretakerProfile cannot. There are no error in logcat too.
Sorry so much for ask a nonsense question. Now I found my mistake, My mistake is in view initial method that I didn't post.
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());
}
Before I edit my code, I'm initial cprofile with the Object of its class so it has empty value because cprofile not query data from database.
CaretakerProfile cprofile = new CaretakerProfile()
finally, I will flag this post to close because it is a nonsense question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.