public class Store extends RealmObject {
@PrimaryKey
private int id;
private String name;
private Boolean isfavorite;
}
I want if field isfavorite is null to NOT save it to Realm. But if it not null then save to Realm.
Is it possible?
@lmdic
boxed types (ex: Boolean) can be nullable. If you don't want to save the field isfavorite you can annotate it with @Ignore.
so you either can:
null values)null is a valid value for a nullable field...
No. I want to save field isfavorite if it value is NOT null. Else if it value = null then not save to Realm.
You can't. a field is either persisted or not.
You'll need to call the setters manually then, because Realm cannot tell if your field is meant to be set to null value, or if it should be ignored.
So the value is updated with the new value.
Why boolean is null? This is wrong in many ways...
@ppamorim technically he wants something like https://github.com/realm/realm-java/issues/2288
Why boolean is null? This is wrong in many ways...
I try to found solution. In one case (e.g. click button) I need to store field isFavorite to Realm. But in another case (when import from incoming json) not save this field to Realm.
Personally I put the favorited IDs into SharedPreferences and when I was synchronizing data, I was updating the field in the RealmObject according to what's in SharedPref for this.
That way favorites also survived across deleteIfMigrationNeeded() so I could afford to be lazy :D
I try to found solution. In one case (e.g. click button) I need to store field isFavorite to Realm. But in another case (when import from incoming json) not save this field to Realm.
@lmdic A simple hack will be you just use a different field name in json (or rename it to a different field name in jason when it's null). Realm will ignore unknown field names in JSON objects.
@lmdic I assume the above answered your question. Feel free to reopen if this is not the case
@Zhuinden - Could you please show some code how to do that ? I tried, but its not working
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document
public class Person {
@Id
private String id;
@Field
private String firstName;
@Field
@Setter(value = AccessLevel.NONE)
private String lastName;
@Field
private String emailId;
@Field
private List<Hobbies> hobbies;
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Person ravi = new Person();
ravi.setFirstName("Ravi");
ravi.setLastName(null);
ravi.setEmailId("[email protected]");
ravi.setHobbies(Arrays.asList(hobbies1, hobbies2));
repository.save(ravi);
you need to use the managed object and merge the new properties into the managed object.
If you send in
ravi.setFirstName("Ravi");
ravi.setLastName(null);
ravi.setEmailId("[email protected]");
ravi.setHobbies(Arrays.asList(hobbies1, hobbies2));
you'll only set firstname lastname emailId hobbies, and potentially clear everything else.
Most helpful comment
You'll need to call the setters manually then, because Realm cannot tell if your field is meant to be set to
nullvalue, or if it should be ignored.So the value is updated with the new value.