简体   繁体   中英

Correct way to update a Realm database?

This is the structure of my Realm database:

    public class ARDatabase extends RealmObject
    {
    @PrimaryKey
    private String uid;

    private String namex;
    private String desc;
    private boolean isVideo;
    private boolean isDeleted;
    private String urlImg;
    private String urlApp;
    private int updates;
    private boolean isDownloaded;
    private String location;

    public ARDatabase(){}

    public String getUid()
    {
        return uid;
    }

    public void setUid(String uid)
    {
        this.uid = uid;
    }

    public String getNamex()
    {
        return namex;
    }

    public void setNamex(String namex)
    {
        this.namex = namex;
    }

    public String getDesc()
    {
        return desc;
    }

    public void setDesc(String desc)
    {
        this.desc = desc;
    }

    public boolean getIsVideo()
    {
        return isVideo;
    }

    public void setIsVideo(boolean isVideo)
    {
        this.isVideo = isVideo;
    }

    public boolean getIsDeleted()
    {
        return isDeleted;
    }

    public void setIsDeleted(boolean isDeleted)
    {
        this.isDeleted = isDeleted;
    }

    public String getUrlImg()
    {
        return urlImg;
    }

    public void setUrlImg(String urlImg)
    {
        this.urlImg = urlImg;
    }

    public String getUrlApp()
    {
        return urlApp;
    }

    public void setUrlApp(String urlApp)
    {
        this.urlApp = urlApp;
    }

    public int getUpdates()
    {
        return updates;
    }

    public void setUpdates(int updates)
    {
        this.updates = updates;
    }

    public boolean getIsDownloaded()
    {
        return isDownloaded;
    }

    public void setIsDownloaded(boolean isDownloaded)
    {
        this.isDownloaded = isDownloaded;
    }

    public String getLocation()
    {
        return location;
    }

    public void setLocation(String location)
    {
        this.location = location;
    }
    }

And I can successfully add objects to the database.
The problem comes when I need to update an object.

This is what I tried:

private void downloadUpdateDatabase(String uid,String location_address) throws RealmException
{

    mRealm.beginTransaction();
    ARDatabase db = new ARDatabase();
    db.setUid(uid);
    db.setIsDownloaded(true);
    db.setLocation(location_address);
    mRealm.copyToRealmOrUpdate(db);
    mRealm.commitTransaction();
    Log.e("TAG","DOWNLOAD UPDATE COMPLETED");

}

The problem here is when I invoke this method. The mentioned fields get updated, but the not mentioned fields in this method become null or zero.
Of course I can set values for all fields by invoking their setters, however from where I invoke this method, I can't get all the field values.

So, the Question is : How do I update my realm database in such a way that the existing fields don't become null ?

PS:

My Realm version is :0.84.1, compile 'io.realm:realm-android:0.84.1'

the field that are mentioned gets updated, however the fields that are not mentioned in this method becomes null or zero

Well, yes, all fields are their defaults at this point.

ARDatabase db = new ARDatabase();

Have you tried to query for the current record, then update the fields, then put that object back?

In other words, you have String uid , so something like

private void downloadUpdateDatabase(String uid,String location_address) throws RealmException
{
    mRealm.beginTransaction();
    ARDatabase db = mRealm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
    db.setIsDownloaded(true);
    db.setLocation(location_address);
    mRealm.copyToRealmOrUpdate(db);
    mRealm.commitTransaction();
}

Or, probably better in async fashion .

private void downloadUpdateDatabase(final String uid, final String location_address) throws RealmException
{
    mRealm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            ARDatabase db = realm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
            db.setIsDownloaded(true);
            db.setLocation(location_address);
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            // Transaction was a success.
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            // Transaction failed and was automatically canceled.
        }
    });
}

Instead of

mRealm.beginTransaction();
ARDatabase db = new ARDatabase();
db.setUid(uid);
db.setIsDownloaded(true);
db.setLocation(location_address);
mRealm.copyToRealmOrUpdate(db);
mRealm.commitTransaction();
Log.e("TAG","DOWNLOAD UPDATE COMPLETED");

There should be

mRealm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        ARDatabase db = realm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
        if(db == null) {
            db = realm.createObject(ARDatabase.class, uid);
        }
        db.setIsDownloaded(true);
        db.setLocation(location_address);
    }
});
Log.e("TAG","DOWNLOAD UPDATE COMPLETED");

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.

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