realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

Immutable Frozen Objects #7645

Closed pedronveloso closed 2 years ago

pedronveloso commented 2 years ago

Problem

Brief description: Frozen objects are mutable, resulting in unpredictable runtime crashes.

Background We've adopted Frozen objects for all of our queries. We query Realm on a background Thread, and use those objects for UI rendering. Frozen objects as you know will throw an error if you were to attempt to modify a property, however nothing on the Java/Kotlin side is immutable therefore these type of issues are only caught at runtime. I was wondering if there are best practices around preventing a Frozen access to be modified, in a way that it could enforce at compile time for added peace of mind and building reliable applications.

We thought about creating immutable wrappers for all of the Realm models, but this way we end up creating a log of boilerplate that needs to be maintained and thus lose some of the benefits of using Realm.

Solution

Realm Frozen objects should produce immutable (read--only) versions of the DB Models.

Alternatives

No response

How important is this improvement for you?

Would be a major improvement

clementetb commented 2 years ago

Unfortunately there is no good way to handle this at the moment. We cannot catch immutability of frozen objects in the type system right now. We have an ongoing idea about providing the ability to define projection #5426 that could assist in doing what you describe, but it the feature is not fully specified and are not in the short term roadmap.

clementetb commented 2 years ago

Closing due to inactivity.