Be assured I have seen all the answer for the very same questions . following this link enter switch-from-local-to-synced-realm
I have a local realm from where I want to copy to synced realm .
public void InitiateSyncForPost()
{
realm = Realm.GetInstance(ConfigForSync);
//realm.WriteAsync((Realm obj) =>
//{
// var realmOld = Realm.GetInstance(Config);
// var ding = realmOld.All<Post>().ToList();
// var dang = realmOld.All<Comment>().ToList();
// var ting = realmOld.All<ImageData>().ToList();
// foreach (var t in dang)
// {
// obj.Add(t, false);
// }
// foreach (var t in ding)
// {
// obj.Add(t, false);
// }
//foreach (var t in ting)
// {
// obj.Add(t, false);
// }
//});
realm.Write(() => {
var realmOld = Realm.GetInstance(Config);
var ding = realmOld.All<Post>().ToList();
var dang = realmOld.All<Comment>().ToList();
var ting = realmOld.All<ImageData>().ToList();
realmOld.Dispose();
foreach (var t in ting)
{
realm.Add(t, true);
}
foreach (var t in dang)
{
realm.Add(t, true);
}
foreach (var t in ding)
{
realm.Add(t, true);
}
});
}
as you can see I tried both async write and write method . both are giving me exception
When I am using WriteAsync
I get exception something like condition not met array length == 1 (don't exactly remember )
When I am using current code I am getting this exception
Realms.Exceptions.RealmObjectManagedByAnotherRealmException: Cannot start to manage an object with a realm when it's already managed by another realm
Is there anything wrong I am doing in copying the object from local realm to synced realm ?
You need to create a non-managed copy of your
RealmObject
subsclass before adding it to the new Realm.You can do this manually or via a helper extension.
Via
NonManagedCopy
extension fromRealmJson.Extensions
(1):Manual Copy:
Assign each property of your RealmObject subclass to a newly instanced non-managed object.
(1) Disclaimer
RealmJson.Extensions
is an extension I wrote, available via nuget package or source:re: https://sushihangover.github.io/Realm.Json.Extensions/