I would like to request for verification/validation of the realm configuration to be flagged as error if the RealmConfiguration is found to be null or empty. Currently need to manually call realm.isEmpty to check in Kotlin , not auto Flagged as error during execution
Refer to kotlin example below on relam-java version 4.3.3 running on Android Fragment
correct configuration is config
incorrect configuration is config1 ie the realmobject Grocery_Master mistakenly omit the .realm extension
However I noticed during the query execution , if the wrong config1 instance was call , no exception triggered by Realm ? Any query call relying on config1 would still execute as normal
var config = RealmConfiguration.Builder()
.name("Grocery_Master.realm")
.schemaVersion(2)
.migration(Grocery_Realm_Migration())
.build()
var config1 = RealmConfiguration.Builder()
.name("Grocery_Master")
.schemaVersion(2)
.migration(Grocery_Realm_Migration())
.build()
val realm = Realm.getInstance(config)
val realm1 = Realm.getInstance(config1)
Hi @TSG9876
I'm not entirely sure what you are asking? Are you asking for a flag that throws an exception if the Realm file wasn't found?
Adding such a flag probably doesn't make much sense, as otherwise, how will you create the Realm in the first place?
If you truly have this use case, you can use
RealmConfiguration config = getConfig();
if (!new File(config.getPath()).exists()) {
// File not found.
}
var config1 = RealmConfiguration.Builder()
.name("Grocery_Master")
.schemaVersion(2)
.migration(Grocery_Realm_Migration())
.build()
Look at the above configuration , this is quite clear that the realm file name is not coded correctly , and yet when a query call such as realm1.where
Realm can't know that you intended to call your file blah.realm or blah. It doesn't automatically postfix it with .realm.
I can't understand your explanation , every realm class object must be created with .realm extension , this is stated in your document . That also means that any realm configuration initiated must also be start with a valid realm class also or else error need to be flag out which could saved valuable time in debugging any query execution or transaction relying on the configuration instance which fail to work as expected. Who would have thought that Realm is so poor in identifying error and not able to detect any fail or invalid configuration initiated in the first place , I deemed this as one big weakness during execution.
A minimal configuration can be created using:
RealmConfiguration config = new RealmConfiguration.Builder().build()
This will create a RealmConfiguration with the following properties.
Realm file is called "default.realm"
It is saved in Context.getFilesDir()
It has its schema version set to 0.
Modifier and Type | Constant Field | Value
-- | -- | --
public聽static聽final聽String | DEFAULT_REALM_NAME | "default.realm"
io.realm.RealmConfiguration
Modifier and Type | Constant Field | Value
-- | -- | --
public聽static聽final聽String | DEFAULT_REALM_NAME | "default.realm"
public聽static聽final聽int | KEY_LENGTH | 64
I think you are misunderstanding our docs. Just because we use default.realm as _our_ default, does not imply that you must end your filenames with .realm.. This is not the case.
You can argue that having such a rule could be beneficialfor increasing compatibility with Realm Studio, but it does not prevent against copy-paste errors.
By the way , would any standard SQL database ever allow non-existence table name to be called or not upon query execution in practice ?
If that is not the case in realm, rewrite the poorly documented Realm guide ,suggest any good practice to follow,not blindly letting user to guess what should be the right way of doing thing ,I am particularly against any flexible copy and paste ,the compiler should have some common sense to make thing right.
The realm examples must also include the empty realm check such as ! realm1.isEmpty,where realm1 is an instance of valid not null realm configuration initialisation . I feel it just not right , to allow any query or transaction to flow through against an empty Realm , just pause for a second and think what is the point of calling query upon an empty object in the first place then?
I am not arguing whether realm can be empty or not , I am however very particular that Realm was designed to allow an instance of empty realm during any query call or transactions execution , this is totally unacceptable at all from programming logic ,not withstanding the fact it is also unwise to allow such irrelevant logic to pass through without triggering any exceptions cause by invalid "table Name".
companion object {
const val GROCERY_MASTER = "Grocery_Master.realm"
}
var config = RealmConfiguration.Builder()
.name(GROCERY_MASTER)
.schemaVersion(2)
.migration(GroceryRealmMigration())
.build()
var config1 = RealmConfiguration.Builder()
.name(GROCERY_MASTER)
.schemaVersion(2)
.migration(GroceryRealmMigration())
.build()
would any standard SQL database ever allow non-existence table name to be called or not upon query execution in practice ?
Realm doesn't either, that's why you get RealmMigrationNeededException when you first open it and you have an existing schema, and schema mismatch because you have classes that are not in the Realm, or you have fields that are not in the Realm, or they have a mismatched field attribute.
The realm examples must also include the empty realm check
No, you are not forced to have data in your Realm. That's why you can insert data into the Realm.
There is nothing on the Realm side that should verify that you made a copy paste error in your own code.
to allow any query or transaction to flow through against an empty Realm
Well, it returns a result set of 0 because zero elements were found.
It makes perfect sense, no? You can create a result set, observe it, and write into the Realm asynchronously and it will all "magically" pop up in the result set. There is no reason to say "hold on, Realm has no data, EXCEPTION" because writes should generally be asynchronous (be done on a background thread) anyways.
I am however very particular that Realm was designed to allow an instance of empty realm during any query call or transactions execution
Well a Realm you've just created will clearly be empty.
this is totally unacceptable at all from programming logic
No it's not, read what I wrote above.
not withstanding the fact it is also unwise to allow such irrelevant logic to pass through without triggering any exceptions cause by invalid "table Name".
The table is not invalid because Realm initializes the schema with the existing models in your code if the Realm is new and has just been created.
I do not understand what you are talking about, sorry
Let's set the thing right and in plain English hopefully one can understand better , alright for the last and final discussion as Time is so precious . It will be a totally unfair to mess and argue on the topic without reaching a satisfactory conclusion perhaps due to different academic opinions .
My proposal is very simple , set a ground rule right in the context of android realm development , any realm file created must end with mandatory filename .realm extension ,these files normally stored under /data/data/package/files location .
Any realm object instance should also be start a valid file name verification end with .realm extension in the RealmConfiguration . The whole purpose of enforcing this rule is try to catch or avoid any query called upon an empty realm at the earliest opportunity.
open class Grocery_Master : RealmObject() is defined as the object class
var config = RealmConfiguration.Builder()
.name("Grocery_Master.realm")------> must end with .realm extension
.schemaVersion(2)
.migration(Grocery_Realm_Migration())
.build()
var config1 = RealmConfiguration.Builder()
.name("Grocery_Master")--> this is already a violation
.schemaVersion(2)
.migration(Grocery_Realm_Migration())
.build()
val realm = Realm.getInstance(config1)---> Realm should immediately take notice this is a not found object exception as the Grocery_Master does not end with .realm file extension or any silly mistake such as Grocery_Master1.realm would amount to no such file name or object being found in the expected location under data/data/package/files
val query_call=realm.where
Flag the query_call as exception whenever the realm is not found or deemed to be empty similar to invalid table name exception in standard SQL call .
It now up to You All as Realm Ultimate Owner to evaluate my proposal and decide whether to adopt into the Programming logic design as improvement enhancement of which in my opinion would certainly help to debug any failed query which did not yield any result not because of empty records but rather an outright plain invalid "table name" wrongly defined in the realm instance initiated which is a totally nightmare to debugger.
By the way , thanks for the valuable comment and input in the past.
Again, you seem to be mixing two concepts. The _name_ of the Realm file has _nothing_ to do with what kind of objects are inside, and having an empty Realm with no objects is a perfectly valid state of the file to be in.
If you managed to open the Realm, then doing this:
val query_call=realm.where<Grocery_Master>().findAll()
Should always return 0 or worst-case throw a RealmClass not part of the schema exception, but since your configuration does not limit the schema that shouldn't happen.
Realm _do_ schema validation on startup, so the "invalid table" exception you talk about should never happen unless you use our dynamic API (which you might very well in your migration code), but the code snippet you reference should work.
With regard to forcing the .realm file name extension, then that is a valid request, and something we will consider, but since it will be a breaking change for all those files already out in the wild, it is not something we can add without being careful.
If you managed to open the Realm, then doing this:
**val query_call=realm.where
Should always return 0 or worst-case throw a RealmClass not part of the schema**
How to make sure the above always throw RealmClass not part of the schema and not return 0 as the result call so much so that silently let the execution pass through without error under current practice?
If you want to restrict a Realm to only certain classes, you can use a RealmModule. You can read more here: https://realm.io/docs/java/latest/#schemas
Does that means the .migration need to be removed for this to work with expected exception? Realm Module is just to restrict to certain subset class only .
Must that instance need to be change also , not quite practical as I defined instance as global instance object .
_With regard to forcing the .realm file name extension, then that is a valid request, and something we will consider, but since it will be a breaking change for all those files already out in the wild, it is not something we can add without being careful._
Got your point , however I can suggest this as optional setting to be defined as the place with the Realm.init(this) is as global on/off setting rule , off as optional for existing , on for future in the config , all the realm projects under my team I enforce such rule for practicality . I always see this as too much flexibility issue , all the global standard set file name in consideration to be adopted as ISO standard in the future and good practice and not withstanding Global IP intellectual CopyRight property also.