Feature request. Create standalone copy from a RealmObject / copyFromRealm
If I get object reference from realm, sometimes I want to have raw object (with all fields filled), not a reference, since reference may become invalid. It would be great if we will have 'out of the box' method like getRawObject.
E.g.
Dog dog = realmDatabase.getDogById(1000).getRawObject();
Hi @dmytrodanylyk Yes, we have been discussing adding something like that internally as well, but havn't decided either way yet.
@cmelchior if you count third part people +1 from me
We very much do. That is one of the reasons we are on GitHub, so third part people can easily chime in :)
+1 from me too. I use EventBus and the only thing I can effectively post are "MyModelRealmProxy" classes. Although this is doable and works, Android Studio's gradle linting system counts this as an unknown class and marks it in red. Having access to a Raw Objects might partly solve that issue. Unless there is another way I am unaware of...
:+1:
+1 from me as well. This feature is essential as Realm objects cannot be accessed across threads. I currently instantiate a parcelable object using AutoParcel in Android and NSDictionary in Objective c for realm objects which is not too bad but makes boilerplate codes.
That might solve the threading issue as @njovy pointed out but I don't think that would be a good solution, because how would you handle relationships? will we basically get the standalone object with everything that it links to? if you did that, it might be a costly operation for large object graphs, and if you didn't and only returned the object with null relationships that wouldn't be a practical solution either.
Currently the solution that we are using to handle this problem (with the help of retrofit / rx-java) is to do all processing on background thread, then switch to UI thread, and get the same object using a shared ui-realm in order to pass it to the UI code, something like:
public Observable<User> getUser(String id) {
return usersApi.getUser(id)
.doOnNext(user -> usersStorage.saveUser(user))
.observeOn(AndroidSchedulers.mainThread())
.flatMap(user -> usersStorage.getUser(id));
}
Actually if #878 would work, we could pass them on in bundles. Triggering a readparcelable would trigger the re-establishment of the connection and give us a valid object with relations.
@osama-lionheart Yeah I do the same thing, it's not optimal.
+1
+1
:+1:
The +1's @JackWink @RyanCopley @ahornerr would be very helpful if you could also quickly state which issue it would solve for you. Other solutions for passing objects across threads are on it's way, so it's important for us to understand if this is just for that purpose.
@bmunkholm Specifically I have an object that is being fetched once and then passed between multiple threads where I no longer need Realm to update if there are changes, I just need the data the first time it is fetched. Quick code example:
Realm realm = Realm.getInstance(this);
final Server server = realm.where(Server.class).equalTo("UUID", serverUUID).findFirst();
realm.close()
//Do multi-threaded operations which require the data in the Server class but don't need to subscribe to changes
//In each one of the connector.method() methods, there are several other threads which are executed, none of which would modify any data in the Server object.
connector.testServer(server, new IServerConnectionListener() {
...
connector.testAuthentication(server, new IServerAuthListener(){
...
//And so on and so forth
}
});
My workaround for this would be to copy the data out into a non-realm object like so:
Realm realm = Realm.getInstance(this);
Server realmServer = realm.where(Server.class).equalTo("UUID", serverUUID).findFirst();
Server server = new Server();
server.setHost(realmServer.getHost());
server.setApiKey(realmServer.getApiKey());
server.setUsername(realmServer.getUsername());
server.setPassword(realmServer.getPassword());
realm.close();
@RyanCopley is +1'ing for the same reason as I am, we are collaborating on the same project.
Ok - thanks! Yeah if you really don't want to use the object with Realm again, or see updates to it, it could be useful. There are still issues though with how to do this with objects having relations.
I understand that relations could be an issue. For my use case, there are no relations inside the Server object, thankfully.
Sure - the issue fo us in supporting this in general is to find a solution that makes sense and works in all situations :-(
@bmunkholm Could you ELI5 how relations would cause an issue? Why couldn't you just pull the entire relationship tree out?
Nothing really prevents loops in your relations. So that would have to be detected.
Loops are not really the problem, it is more the risk of references causing the entire object graph to be copied, which could potentially take a long time as well as use a lot of memory. This leaves two choices:
- We do it anyway and let it be up to developers to avoid that situation.
- Add some option to control if references are copied and potentially how deep.
Yeah a max depth copy sounds like it would work from my viewpoint. Are there any other hurdles you all know of?