New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial updates with copyToRealmOrUpdate() (or other method) #2179

Closed
guillermomuntaner opened this Issue Jan 28, 2016 · 5 comments

Comments

Projects
None yet
4 participants
@guillermomuntaner

guillermomuntaner commented Jan 28, 2016

Feature request:

Partial updates

How?

Related to issue #1853
I agree that only updating not-null values is not a correct approach, since null can be sometimes a desired value and also because of not nullable primitive types with default values.

But, what about a call to specify a list of the fields that one want to overwrite (or preserve)?
copyToRealmOrUpdateOverwritingFields("field1","field2")
copyToRealmOrUpdatePreservingFields("field1","field2")
copyToRealmOrUpdate(obj).preserve("field1","field2")

In case the object does not exist in realm, the "fields to be preserved" can be initialized with default values, which i think is quite solid from a design pov.

Thoughts on this?

My issue scenario:

Im doing a cache with Realm where objects from one class can be updated online or locally based on user interaction. This is a problem, because every single online update using copyToRealmOrUpdate() will overwrite all the local updates which i would like to keep. The option to use json would work for me, but since im dealing with complex json and doing custom deserialization plus other manipulations in order to get the POJO i am storing, having to serialize to Json just to update feels a bit excessive.

As reference found also other related issue with a scenario like mine #1540

@beeender beeender added the Pending label Jan 29, 2016

@beeender

This comment has been minimized.

Show comment
Hide comment
@beeender

beeender Jan 29, 2016

Contributor

@guillermomuntaner May I know how did you create the object and pass it to copyToRealmOrUpdate?

Is it something like:

MyObject obj = new MyObject();
obj.setId(id);
obj.setString("aaa") // the field needs to be update
// then update the realm
realm.beginTransaction();
realm.copyToRealmOrUpdate(obj);
realm.commitTransaction();

If the above is the case, i am thinking the other interface requested by other user might be more suitable for you, see #2166
With that interface, you can use something like below:

realm.beginTransaction();
MyObject obj = realm.getOrCreate(id);
obj.setString("aaa") // the field needs to be update
realm.commitTransaction();

Wouldn't it be simpler and easier for your case?

Contributor

beeender commented Jan 29, 2016

@guillermomuntaner May I know how did you create the object and pass it to copyToRealmOrUpdate?

Is it something like:

MyObject obj = new MyObject();
obj.setId(id);
obj.setString("aaa") // the field needs to be update
// then update the realm
realm.beginTransaction();
realm.copyToRealmOrUpdate(obj);
realm.commitTransaction();

If the above is the case, i am thinking the other interface requested by other user might be more suitable for you, see #2166
With that interface, you can use something like below:

realm.beginTransaction();
MyObject obj = realm.getOrCreate(id);
obj.setString("aaa") // the field needs to be update
realm.commitTransaction();

Wouldn't it be simpler and easier for your case?

@guillermomuntaner

This comment has been minimized.

Show comment
Hide comment
@guillermomuntaner

guillermomuntaner Jan 29, 2016

@beeender Indeed im using the first approach, because my object instances come from Retrofit GSON deserialization. Then i am using the copyToRealmOrUpdate(List) to persist batches of objects into realm in a single elegant call.

Your proposed solution with getOrCreate() can be a solution to my issue, so thanks!
Thought, it requires to iterate over all the objects and copy each of the fields from the non-realm object to the realm object. Not elegant at all, but works. Example code:

List<MyObject> objects = getMyObjectsFromNetwork();
realm.beginTransaction();
for(MyObject object : objects) {
    MyObject realmObject = realm.getOrCreate(object.getId()); 
    realmObject.setField1(object.getField1());
    ... 
    realmObject.setFieldN(object.getFieldN());
}
realm.commitTransaction();

I still think that being able to partialy update objets and collections of objects with a simpler interface would be a nice feature in case you are interested. Also i guess some performance improvements could be achieved when updating batches of objects together.

Thanks!

guillermomuntaner commented Jan 29, 2016

@beeender Indeed im using the first approach, because my object instances come from Retrofit GSON deserialization. Then i am using the copyToRealmOrUpdate(List) to persist batches of objects into realm in a single elegant call.

Your proposed solution with getOrCreate() can be a solution to my issue, so thanks!
Thought, it requires to iterate over all the objects and copy each of the fields from the non-realm object to the realm object. Not elegant at all, but works. Example code:

List<MyObject> objects = getMyObjectsFromNetwork();
realm.beginTransaction();
for(MyObject object : objects) {
    MyObject realmObject = realm.getOrCreate(object.getId()); 
    realmObject.setField1(object.getField1());
    ... 
    realmObject.setFieldN(object.getFieldN());
}
realm.commitTransaction();

I still think that being able to partialy update objets and collections of objects with a simpler interface would be a nice feature in case you are interested. Also i guess some performance improvements could be achieved when updating batches of objects together.

Thanks!

@beeender beeender added T:Enhancement P2 and removed Pending labels Jan 29, 2016

@beeender

This comment has been minimized.

Show comment
Hide comment
@beeender

beeender Jan 29, 2016

Contributor

Yes, it does make sense to me. Let's see what can we improve from Realm side. :)

Contributor

beeender commented Jan 29, 2016

Yes, it does make sense to me. Let's see what can we improve from Realm side. :)

@cmelchior

This comment has been minimized.

Show comment
Hide comment
@cmelchior

cmelchior Feb 17, 2016

Contributor

Merging this issue into #2288. Closing.

Contributor

cmelchior commented Feb 17, 2016

Merging this issue into #2288. Closing.

@cmelchior cmelchior closed this Feb 17, 2016

@cmelchior cmelchior removed the S:P2 Backlog label Feb 17, 2016

@sashatinkoff

This comment has been minimized.

Show comment
Hide comment
@sashatinkoff

sashatinkoff Jul 26, 2017

have you tried to use json string?

String json = "[{"name":"name","ts":0}]";
realm.createOrUpdateAllFromJson(SampleAccount.class, json);

It seems like it works as expected

sashatinkoff commented Jul 26, 2017

have you tried to use json string?

String json = "[{"name":"name","ts":0}]";
realm.createOrUpdateAllFromJson(SampleAccount.class, json);

It seems like it works as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment