Dhiraj Gupta Dhiraj Gupta - 3 days ago 5
JSON Question

Realm createOrUpdateAllFromJson to create RealmModel objects without persisting?

I'm using realm Java in my in-development Android app quite happily. I receive

JSONObject
arrays from the server API, and realm dutifully ingests them through the convenient
createOrUpdateAllFromJson
on the main realm instance object.

The typical flow for this looks like:

// get API results into JSONArray jsonObjArrs variable
realm.beginTransaction();
realm.createOrUpdateAllFromJson(MyCoolModel.class,jsonObjArrs);
realm.commitTransaction();


However, I've now run into a situation where I would like to create a temporary, in-memory array of the same
MyCoolModel
objects without actually saving them to Realm, because I want to throw these away as soon as the specific use case is done. It looks to me like
createOrUpdateAllFromJson
and its sibling methods are only available on the main realm instance object.

Is there any way to harness this convenient
JSON
to object model creator, or do I have to write an alternate implementation with manual / automated JSON parsing?

Answer

If you want in-memory objects from a JSON structure, your should look at a standard JSON parser like GSON, Jackson or Moshi. Realm does not have any inbuilt way of doing this.

Comments