From SO question basically requesting the equivalent of the Java copyFromRealm and copyToRealmOrUpdate
Could loosely classify as _waiting for the user_ as I suggested possibly using different XAML binding techniques, to discourage people losing object liveliness.
User definitely wants this as has since lodged an issue (I closed as duplicate of this).
+1 on this for sure.
Question about this. When this gets implemented will we be able to make changes to objects outside of a transaction? The pattern that I try to follow for data access this would be necessary.
I am very unwilling to pass around Transaction objects in my viewmodel / view layer of my application. I'd much rather deal with unmanaged objects in write / update scenarios and remap down in my data layer to a managed object. While this may not be the most efficient way, not being coupled to a data access implementation is important to me.
Having unmanaged objects would allow me to not have to duplicate my model classes one inheriting from realm object one not. But if that is what I have to do I'll deal.
@j-hayes would #647 help in your situation? It will allow you to bind to realm objects without explicitly creating and committing transactions. Or is your situation different?
Waiting for #852 as it needs the schema in the standalone
Hi, does anyone have a workaround for this? (PCL)
I've tried using Json serialize/deserialize however getting a null reference exception which assume is due to #852
All i really want is to store a static user RealmObject that my application requires frequently. What I'm having to do is pass around a realm instance through my function hierarchy and pull it out of Realm each time it's required. Ideally I want get the object once when the application launches as it will be required on every view.
Hi @Harvinator sorry for the delay, I must have missed your comment. Can you paste a stacktrace of the exception, as #852 shouldn't be an issue anymore.
I've just tried to reproduce the problem and it's working now! Unsure what I was doing differently before. Apologies for the false alarm
I have just encountered an excellent scenario showing exactly why this needs to become a higher priority issue.
App has a Pattern object with many Pixel children. They are building up the Pattern in memory as the user may not decide to Save.
Their Save code looks like this:
public async Task SavePatternAsync(Pattern pattern)
{
await _realm.WriteAsync(realm =>
{
realm.Add(pattern);
});
}
They are getting an exception rom the UI thread:
_Essentially, the view that I've already left is trying to draw the pattern, while Realm is trying to save it at the same time_
Of course, the pattern argument they are passing in, is being adopted by the temporary thread in the WriteAsync.
What they need to be able to do is CopyToRealm so instead of an Add which adopts the pattern, it is copied. However, that also needs them to use Find on the result to get back an object on the local Realm.
I think it's better to guide them how to best fit their use-case to the Realm paradigms rather than trying to support every app scenario people can come up.
For example, in this case a much better approach would be to add a bool IsConfirmed { get; set; } property to their Pattern model and set that to true after saving. If the user decides not to save, they can delete the object. And if the app crashes or the user leaves to take a call and forgets about it, they can display a prompt at startup about finishing or discarding what they were doing last. Once they do that, they can work with managed objects only and there'll be no need to copy things around.
Alternatively, they can always use tools like automapper to copy the object before adding it to the Realm, so I can't see a reason for us to support that out of the box.
Further feedback from this user - this is a situation I can't see an easy workaround on. The approaches you mention above work for a single object but not a parent with bunch of children.
I realized an issue with the managed objects. Since I allow my users to select patterns they've previously made, make changes to them and then decide whether they want to save or discard said changes, I can't do this with managed objects, right?
What I mean, my pattern/pixel properties are currently updating to the database, even if I haven't explicitly tapped the save button. I guess I'd need an unmanaged copy, then somehow replace the managed version with the new version.
It would work with children once we get cascading deletes, now it would just be annoying. But in their situation, it could be inapplicable indeed :/ Perhaps if they shared their complete model and briefly described how their app behaves, we could try to figure something out.
How about keeping a transaction open until the user taps "save"? I know it's discouraged, especially with sync, but it might work for their case?
It would work with children once we get cascading deletes
I don't see how. Cascading deletes will take out all children of an object. They want to be able to undo only _changes_ represented by adding some children, preserving others.
Could I ask why this issue was closed? In the current project I am working in I'd like to have methods like copyFromRealm and copyToRealmOrUpdate.
I don't think it's intuitive that this code is causing an exception:
var myObject = await _myService.LoadMyObjectFromDbAsync();
myObject.Text = "Hello Realm";
await _myService.SaveMyObjectToDbAsync(myObject);
I'd like to use Realm like this because we separated the database logic from the view model logic to enable loose coupling. The only workaround I found was to serialize and deserialize my Realm object, like it's written in this StackOverflow post: https://stackoverflow.com/a/43999908/1584648
The issue was closed because we decided not to expose the copyFromRealm method as there are better third party tools for the job and writing a generic method to extract a standalone object from the database is a serious task that provides very little value.
We have added a method to add a standalone object to the database, so that direction has been covered.
The code snippet you posted provides very little information to be able to diagnose the reason for getting an exception, so please file a new issue with as much detail as possible and we'll investigate.
After wasting too much time in 3rd party libraries like AutoMapper, I've created my own extension function which works pretty well. This function simply uses Reflection with the recession. (Currently, Only for List type. You can extend the functionality for Dictionary and other types of the collection very easily or you can completely modify the functionality based on your own requirements.).
I didn't do too much time and complexity analysis. I've tested only for my test case which contains many nested RealmObject, built from a 3500+ line of JSON object, took only 15 milliseconds to clone the object.
Here the complete extension available via Github Gist. If you want to extend the functionality of this extension please update this Github Gist, So, other developers can take advantage of it.
https://gist.github.com/vineetchoudhary/94407b9c2bc217c36e3c244ed1af3e4f
@vineetchoudhary I've improved your code a bit and pushed a NuGet package https://www.nuget.org/packages/Realm.Clone/
Source code: https://github.com/pfedotovsky/realm-dotnet-clone/blob/master/Realm.Clone/Realm.Clone/RealmExtensions.cs
Most helpful comment
After wasting too much time in 3rd party libraries like AutoMapper, I've created my own extension function which works pretty well. This function simply uses Reflection with the recession. (Currently, Only for List type. You can extend the functionality for Dictionary and other types of the collection very easily or you can completely modify the functionality based on your own requirements.).
I didn't do too much time and complexity analysis. I've tested only for my test case which contains many nested RealmObject, built from a 3500+ line of JSON object, took only 15 milliseconds to clone the object.
Here the complete extension available via Github Gist. If you want to extend the functionality of this extension please update this Github Gist, So, other developers can take advantage of it.
https://gist.github.com/vineetchoudhary/94407b9c2bc217c36e3c244ed1af3e4f