realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Add the ability to access a realm instance from RealmObject

Open idok595 opened this issue 9 years ago • 9 comments

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

idok595 avatar Apr 27 '17 08:04 idok595

Interestingly, final Realm realm = (Realm) ((MyObjectRealmProxy)(myObject)).realmGet$proxyState().getRealm$realm(); would work.

Zhuinden avatar Apr 27 '17 08:04 Zhuinden

this is workarround, maybe it will be better to implement offical way to get it.

idok595 avatar Apr 27 '17 09:04 idok595

I actually agree, there is no reason why RealmObject couldn't have a getRealm() method.

I've never needed it but why not? 😄

@cmelchior ?

Zhuinden avatar Apr 27 '17 09:04 Zhuinden

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 ?

cmelchior avatar Apr 27 '17 11:04 cmelchior

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

Zhuinden avatar Apr 27 '17 11:04 Zhuinden

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_

beeender avatar Apr 27 '17 12:04 beeender

@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.

kneth avatar Apr 28 '17 08:04 kneth

@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...

bmeike avatar May 16 '17 19:05 bmeike

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!

bmeike avatar May 17 '17 22:05 bmeike