Reputation: 1093
In order to handle all the realm transitions, I've created an helper 'manager' class, that has the following property:
Realm realm;
Every method has a function that gets the realm instance from a configuration.
RealmConfiguration realmConfig = new RealmConfiguration.Builder(context).build();
realm = Realm.getInstance(realmConfig);
Where the the Realm Builder:
public Builder(Context context) {
if (context == null) {
throw new IllegalArgumentException("A non-null Context must be provided");
}
initializeBuilder(context.getFilesDir());
}
In this way there are concurrency problems like 'this Realm instance has already been closed, making it unusable'. I've written a test which writes/reads large amount of data but I am not able to reproduce the above said error, that occurs quite rarely.
What prevents me from removing the realm property and create a local variable per method, is:
Upvotes: 0
Views: 754
Reputation: 43314
can a configuration be used to spawn multiple realm instances?
Yes.
Christian Melchior also does it himself here
if allowed, is this the best approach?
That is a difficult question. "Best" depends on the situation and context. If you are only using 1 RealmConfiguration (you do not mention that you use multiple, so I assume you use 1), I suggest you define that in an Application class (as do the docs) and use Realm.getDefaultInstance()
to reference it.
Upvotes: 1