Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Partial updates with copyToRealmOrUpdate() (or other method) #2179
Related to issue #1853
But, what about a call to specify a list of the fields that one want to overwrite (or preserve)?
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
@guillermomuntaner May I know how did you create the object and pass it to
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
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?
@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!
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.