简体   繁体   English

我的领域未将新数据更新到领域数据库

[英]My Realm not update new data to realm database

I'm trying to update data to database but nothing happen. 我正在尝试将数据更新到数据库,但没有任何反应。

Data will update when Setting.java is onPause() 当Setting.java为onPause()时,数据将更新

Setting.java some part of Setting.java 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();
}

I'm already check that all textView can get value. 我已经检查过所有textView都能获得价值。 So the problem might be in my RealmUpdate class. 因此,问题可能出在我的RealmUpdate类中。

RealmUpdate.java 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. 我的问题是方法upsertProfile可以更新数据,但是upsertCaretakerProfile无法。 There are no error in logcat too. logcat中也没有错误。

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. 在编辑代码之前,我将使用其类的Object作为初始cprofile,因此它具有空值,因为cprofile不会从数据库查询数据。

CaretakerProfile cprofile = new CaretakerProfile()

finally, I will flag this post to close because it is a nonsense question. 最后,由于这是一个无聊的问题,我将标记此职位以关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM