My Realm不会将新数据更新到realm数据库

问题描述 投票:0回答:1

我正在尝试将数据更新到数据库,但没有任何反应。

当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类中。

realm update.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也没有错误。

java android realm realm-java
1个回答
0
投票

很抱歉这是一个无意义的问题。现在我发现了我的错误,我的错误在于我没有发布的初始方法。

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()

最后,我将标记这篇文章,因为这是一个无意义的问题。

© www.soinside.com 2019 - 2024. All rights reserved.