I have seen the use of #Ignore for certain fields but I’m looking for something slightly different. https://realm.io/docs/java/latest/#models
Is it possible to specify skipping a nested object when writing a parent object to realm?
The reason for this:
I have a complex JSON object which I’m parsing and then saving to my Realm.
This object can get really large so there is some optimisation on my backend to return:
A complete object
A preview object
At some points I get a preview user object which returns only a subset of fields.
When saving to realm this overwrites the complete object (as expected) and wipes the fields not present.
The problem is that I still need those wiped fields later on.
You are not using the JSON support of Realm? If so you can use this, see last item (my emphasis):
Parsing JSON with Realm is subject to the following rules.
Creating object with JSON which has the field with a null value:
For a not-required field, set it to null which is the default value.
For a required field, throw an exception.
Updating object with JSON which has the field with a null value:
For a not-required field, set it to null.
For a required field, throw an exception.
JSON doesn’t have the field: Leave the value unchanged for both required and not-required fields.
Source:
https://realm.io/docs/java/latest/#json
Related
I've created classes for each of my data types and use them to read snapshot values whenever possible:
MyClass my = snapshot.getValue(MyClass.class);
Everything is fine until I try to read a particular record:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type java.lang.Boolean
Oops. It turns out that another client mistakenly wrote the string literal "true" instead of the boolean value true to a child of this record. Many of my properties including this one are optional. Is it possible through annotations or otherwise for Firebase to still read this object and just ignore properties that are not of the expected type/format, or do I have to deal with this exception and drop the entire record?
Previously, when there was no stable version of realm for Java (Android), we could not store null values in realm and we had to perform some unnatural hack to be able to do so, as explained in this post.
But as of now realm 1.0 is released, are there any update about being able to store null value?
For example : unfortunate cases when there is no field in JSON which I want to store in realm after parsing but haven't handled it manually.
I have the following code:
realmObject.setData(jsonObject.getString("SELECTOR"));
the program flow stops and exits the block the code is located inside.
the logcat shows
W/System.err: org.json.JSONException: No value for SELECTOR
But when I do:
realmObject.setData(null);
The program flow does not stop and continues across my realm statement realmObject.setData(null);
In some cases, there is no value for the tag "SELECTOR" in my Json file.
And I want it to be null in default.
I actually found out that the problem is actually with just :
jsonObject.getString("SELECTOR")
not the whole statement:
realmObject.setData(jsonObject.getString("SELECTOR"));
so the fix for me was
realmObject.setData(jsonObject.optString("SELECTOR"));
you can use has that will check whether key is available of not and basis of that save value to realm object
if (jsonObject.has("SELECTOR")) {
realmObject.setData(jsonObject.getString("SELECTOR"));
}
else{
realmObject.setData(null);
}
One of the coolest features of the Android data binding support is that it also generates fields for View with IDs set. This tidies up the codebase as no field or findViewById() calls are necessary.
But the problem is that the binding instance can only be retrieved via the bind() call which tends to schedule binding. This is bad when the data is being received asynchronously and commonly the NullPointerException gets thrown.
Can the binding instance with View fields be retrieved minus the actual data binding process?
Stack-trace:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at com.app.android.databinding.ActivityRestaurantDetailsBinding.executeBindings(ActivityRestaurantDetailsBinding.java:381)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350)
at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167)
at android.databinding.ViewDataBinding$5.onViewAttachedToWindow(ViewDataBinding.java:137)
at android.view.View.dispatchAttachedToWindow(View.java:14525)
This doesn't seem to make sense, data binding will ignore null variables thus no null pointer should be thrown, that is, i believe, one of its most promoted features. If you need to modify variables after async calls etc you can just use dataBinding.executePendingBindings()
From the docs
The generated binding class will have a setter and getter for each of the described variables. The variables will take the default Java values until the setter is called — null for reference types, 0 for int, false for boolean, etc.
and
Generated data binding code automatically checks for nulls and avoid null pointer exceptions. For example, in the expression #{user.name}, if user is null, user.name will be assigned its default value (null). If you were referencing user.age, where age is an int, then it would default to 0.
Got the same problem with java.lang.Boolean. Solved by using primitive boolean type instead.
<variable
name="var"
type="boolean" />
My code:
AppCFG appCFG = new AppCFG();
if(jsonToParse != null) {
Realm realm = Realm.getInstance(AppController.getInstance());
appCFG.setOid(ParserJsonMethods.getOid(jsonToParse));
appCFG.setBaseResourceUrl(jsonToParse.optString(AppCFGContract.BASE_RESOURCE_URL));
appCFG.setClientName(jsonToParse.optString(AppCFGContract.CLIENT_NAME));
appCFG.setBucketName(jsonToParse.optString(AppCFGContract.BUCKET_NAME));
appCFG.setConfigUpdatedOn(StringConvertions.stringDateToMillis(jsonToParse.optString(AppCFGContract.CONFIGURATION_UPDATED_ON)));
appCFG.setDefaultOutputVideoMaxFps(jsonToParse.optInt(AppCFGContract.DEFAULT_OUTPUT_VIDEO_MAX_FPS));
appCFG.setLatestPackagePublishedOn(StringConvertions.stringDateToMillis(jsonToParse.optString(AppCFGContract.LATEST_PACKAGE_PUBLISHED_ON)));
appCFG.setOnboardingPassed(jsonToParse.optBoolean(AppCFGContract.ONBOARDING_PASSED));
appCFG.setOnboardingUsingPackage(jsonToParse.optString(AppCFGContract.ONBOARDING_USING_PACKAGE));
appCFG.setPrefferedFootageOID(jsonToParse.optString(AppCFGContract.PREFFERED_FOOTAGE_OID));
appCFG.setTweaks(jsonToParse.optString(AppCFGContract.TWEAKS));
appCFG.setUploadUserContent(jsonToParse.optString(AppCFGContract.UPLOAD_USER_CONTENT));
appCFG = parseMixedScreen(appCFG, jsonToParse);
realm.beginTransaction();
realm.copyToRealmOrUpdate(appCFG);
realm.commitTransaction();
The result of pulling all results:
All fields are empty.. but somehow the json is attached to the object.. What am I doing wrong??
EDIT:
When I do result.getBaseResourceUrl() after pulling all results and opening this result (the result from the image), for instance, I get back a good answer which is:http://blah.blah. but when I try to get results from realm based on baseResourceUrl = "http://blah.blah" I get back nothing...
Realm uses a zero-copy architecture with proxy objects. This means that all your data is always kept inside our internal storage engine in C++ and not actually copied to Java. This also means that Realm doesn't really use the Java variables and as such they always have uninitialised value when looking at them through a debugger (null for objects, 0 for ints, "" for strings)
You can see your object is really a <yourtype>RealmProxy. What this proxy class does is overriding all getters and setters to access the data in C++ instead of Java. So if you use the normal getters you can access you data. The proxy also creates a proper toString() method which is why your popup shows the correct output.
I have json with field that contains two different types.
"fields":[{"value":"ZIELONE OKO"},{"value":{"#nil":"true"}}]
I have problem with deserializing these. My class with model contains:
private String value;
And I simply need to translate {"#nil":"true"} into null. Right now I get error:
The JsonDeserializer StringTypeAdapter failed to deserialized json object {"#nil":"true"} given the type class java.lang.String
Ideally, I would change code that produces that odd JSON: shouldn't second value just be JSON null? I guess it is being produced by some complex process, starting with XML (which must use 'isNul' to differentiate between null String and empty String).
But if that is not possible, I think both Jackson and Gson would require either custom deserializer; or to first bind to a generic Map and then handle value oddities explicitly. In second case, you just declare type to bind to as Map (possible with generic type info using reference; but that should be optional).