Add the ability to access a realm instance from RealmObject
It can be very useful to have access to the Realm instanse from a RealmObject. For example, in complex get logic that involving more then 1 object type or Repostiories RealmObject, it can also can function as code alternative to the join query that are not supported by realm
Interestingly, final Realm realm = (Realm) ((MyObjectRealmProxy)(myObject)).realmGet$proxyState().getRealm$realm(); would work.
this is workarround, maybe it will be better to implement offical way to get it.
I actually agree, there is no reason why RealmObject couldn't have a getRealm() method.
I've never needed it but why not? 😄
@cmelchior ?
Mostly because adding methods on the RealmObject class restrict what the model class can do themselves, and since we haven't seen any requests for it before now, we would rather keep the public API thin.
I'm not totall against the idea though. What do you think @realm/java ?
One thing I'd like to note is that the Swift version allows accessing the Realm of their managed proxy via an accessor: https://github.com/realm/realm-cocoa/issues/4816#issuecomment-291688421
Then we need the same method for other accessors:
RealmList.getRealm();
RealmResults.getRealm();
RealmObject.getRealm();
BTW, cocoa has it https://realm.io/docs/swift/2.6.2/api/Classes/Object.html#/s:vC10RealmSwift6Object5realmGSqCS_5Realm_ https://realm.io/docs/swift/2.6.2/api/Classes/Results.html#/s:vC10RealmSwift7Results5realmGSqCS_5Realm_
@idok595 Could you explain the use case in more details?I lack imagination this morning for the only case I can come up with is:
doSomething(obj) {
Realm realm obj.getRealm();
realm.beginTransaction();
newObj = realm.createObject();
realm.commitTransaction();
}
when you don't keep a reference to a Realm instance in your class.
@idok595 Can you provide an example of how you would use this feature? As @kneth says, why not just keep the reference to the Realm that you got when you opened it...
Hey @idok595 I think the consensus is that this is a plausible feature but that we don't want to pollute our interface unless there is a reason to do so. Without a use-case, we have no good reason to add the functionality.
I'm going to close this issue. By all means, re-open it if you have a use case and would like to pursue the idea!