5

I'm having a problema using Realm for .NET, in Xamarin.

I can add, update and delete objects normally, but likewise java, Realm for .NET doesn't have a copyFromRealm object.

My method just open the realm, select data and I need to handle this object outsite the data layer (i'm using a class for query the data and validate it in another layer).

But even use DeepCloner (nuget), the Realm are throwing an exception accusing that the realm is closed, but I'm closing it using the using() instruction for .NET.

I's it possible copy an object from realm, before it's closed and manipulated that object. I just need to read the data in object, not writing it again to Realm.

using(var realm = Realm.GetInstance(config))
{
    var data = realm.All<DadosUsuario>().FirstOrDefault().ShallowCopy();
}
  • 1
    https://stackoverflow.com/questions/46628890/exception-when-converting-local-realm-to-synced-realm-in-xamarin-ios/46629763#46629763 – SushiHangover Dec 18 '17 at 22:08
  • Thank you so much, this solves my problem. Thanks a lot. Could you make an answer to give you credits? And could you explain why changing from AutoMapper to Newtonsoft? – Grupo CDS Informática Dec 19 '17 at 13:24
  • `AutoMapper` is a great library. The core issue with an lib like AutoMapper, DeepClone, ... do they work in a generic fashion, how fast are they for the number of "records" you are copying, and how much setup do they need. I've done a bunch of benchmarks related to **my** needs and using Json.Net is typically as fast for deep-copies as the rest, again for **my** data, your could be wildly different... Also it just works well with RealmObject subclasses; R/W properties are a no brainer, but it handles the Realm `IList` and transverses through the linked object tree. – SushiHangover Dec 19 '17 at 13:37
  • There are a bunch of other considerations like how hard they are on the GC (do they create a bunch of temp/transition objects), etc.. I'm sure there are instances where one will beat the others for a certain usage pattern, and in those cases, usually discovered via profiling the app, you can sub-in a different library or just handle the creating the copies manually and thus you can tune the code to your usage pattern.... – SushiHangover Dec 19 '17 at 13:42
  • Thank you so much for the explanations. Considering my case, the way it is works flawlessly, so for me it's OK. But when I need, I will make some profiles and benchmarking using my tests cases. Again, thanks a lot. – Grupo CDS Informática Dec 19 '17 at 18:46
  • I've created an extension function to deep clone object without including the realm properties. Please check my answer here https://stackoverflow.com/questions/37999630/how-to-get-a-standalone-unmanaged-realmobject-using-realm-xamarin/54361319#54361319 – Vineet Choudhary Jan 25 '19 at 08:22

2 Answers2

1

Yes, there's no reason why that wouldn't be possible. The likely reason why DeepClone fails is that it clones properties from the base RealmObject class (most notably Realm). Instead, you could either write the clone method yourself, or use AutoMapper which should ignore readonly properties out of the box, but requires slightly more configuration.

Nikola Irinchev
  • 1,911
  • 1
  • 13
  • 17
0

For anyone else looking for a way to do this, the first method described here worked well for us.

Breeno
  • 3,007
  • 2
  • 31
  • 30