DolDurma
DolDurma

Reputation: 17380

Android simply use Realm to save and get saved results doesnt work

i'm newbie in Realm database solution and i want to use that in my application, but after test codes during read the realm documents my codes doesn't work and my result return zero record.

Application:

public class EManApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                .name("EmanClient.db")
                .build();

        Realm.setDefaultConfiguration(realmConfiguration);

    }
}

manifest:

<application
    android:name=".EManApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

UserAccountInfo model:

public class UserAccountInfo extends RealmObject {

    @PrimaryKey
    private String id;
    private String userId;
    private String name;
    private String family;
    private String mobileNumber;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFamily() {
        return family;
    }

    public void setFamily(String family) {
        this.family = family;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }
}

MainActivity:

// Defined realm field   
private Realm realm;

/*OnCreate*/
realm = Realm.getDefaultInstance();

realm.beginTransaction();

UserAccountInfo userAccountInfo = new UserAccountInfo();
userAccountInfo.setId(UUID.randomUUID().toString());
userAccountInfo.setUserId("1");
userAccountInfo.setName("MyName");
userAccountInfo.setFamily("MyFamily");
userAccountInfo.setMobileNumber("+98746321");

realm.commitTransaction();

RealmResults<UserAccountInfo> results = realm.where(UserAccountInfo.class).findAll();
for (UserAccountInfo u : results) {
    Log.e("", u.getId());
}

in this above get result results variable is zero

Upvotes: 0

Views: 290

Answers (2)

Vipul
Vipul

Reputation: 28103

Alternative to @Fabio's answer

new UserAccountInfo();

should be replaced with

realm.createObject(UserAccountInfo.class);

Upvotes: 0

Fabio Venturi Pastor
Fabio Venturi Pastor

Reputation: 2529

You have to use copyToRealm to add a new realm object:

realm.beginTransaction();

UserAccountInfo userAccountInfo = new UserAccountInfo();
userAccountInfo.setId(UUID.randomUUID().toString());
userAccountInfo.setUserId("1");
userAccountInfo.setName("MyName");
userAccountInfo.setFamily("MyFamily");
userAccountInfo.setMobileNumber("+98746321");

realm.copyToRealm(userAccountInfo); // add this line

realm.commitTransaction();

Upvotes: 4

Related Questions