My goal is to persist an object instantiated with GSON to the database with realm.
My problem is the following:
I can instantiate an object with GSON, so GSON takes care of all the properties, but then it won't be persisted to db
I can instantiate an object with Realm, but then I have to fill in all the setters for the properties.
In my oncreate, this part is the same for both methods
//delete previous realm file
Realm.deleteRealmFile(this);
//gson instance
Gson gson = new Gson();
//realm instance
realm = Realm.getInstance(this);
** The name of my class is Vacature **
Info: I am aware that the following code needs to be inside:
realm.beginTransaction();
// code here
realm.commitTransaction();
Option 1:
//get class with gson
Vacature vacatureGson = gson.fromJson(jsonString, Vacature.class);
This won't work because you have to instantiate a realm object by using
Class instance = realm.createObject(Class.class); // Create a new object
Option 2:
//get instance with gson
Vacature vacatureGson = gson.fromJson(jsonString, Vacature.class);
//make instance with realm
Vacature realmVacature = realm.createObject(Vacature.class);
realmVacature = vacatureGson;
This won't work because this is not a proper way to copy all the properties from one object to another. If there is however a good way to copy all the properties from the gsonObject to the realmObject, option 2 might work.
Any ideas on how to solve this puzzle?
The following sequence of events shows when objects are merged: The old object is decompiled. The new object is decompiled. The objects are merged. The resulting object is compiled. If any one of these steps fail, the merge will be discontinued. There are two options for merging:
Merge objects using Object.assign () method The Object.assign () method allows you to copy all enumerable own properties from one or more source objects to a target object, and return the target object: Object.assign (target, sourceObj1, sourceObj2,...);
To recursively merge own and inherited enumerable string keyed properties of source objects to a target object, you can use the Lodash ._merge () method: In this tutorial, you have learned how to merge objects in JavaScript using the spread operator ( ...) and Object.assign () method.
When we merged these objects, the result object ( remoteJob) has the country property with the value from the second object ( location ). The Object.assign () method allows you to copy all enumerable own properties from one or more source objects to a target object, and return the target object: Object .assign (target, sourceObj1, sourceObj2, ...);
Emanuele from Realm here.
Right now the solution is suboptimal and is presented in one of the examples: https://github.com/realm/realm-java/blob/master/examples/gridViewExample/src/main/java/io/realm/examples/realmgridview/GridViewExampleActivity.java
We're working to make the experience way smoother!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With