Exception when converting local realm to synced re

2019-07-29 06:08发布

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 ?

1条回答
劫难
2楼-- · 2019-07-29 06:38

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 from RealmJson.Extensions(1):

var nonSyncedRealm = Realm.GetInstance(nonSyncedRealmConfig);
var syncedRealm = Realm.GetInstance(syncedRealmConfig);

var nonSyncedAll = nonSyncedRealm.All<ARealmClass>();
syncedRealm.Write(() =>
{
    foreach (var realmObject in nonSyncedAll)
    {
        var syncedObject = realmObject.NonManagedCopy<ARealmClass>();
        syncedRealm.Add(syncedObject, true);
    }
});

Manual Copy:

Assign each property of your RealmObject subclass to a newly instanced non-managed object.

syncedRealm.Write(() =>
{
   foreach (var realmObject in nonSyncedAll)
   {
        var syncedObject = new ARealmClass
        {
            Id = realmObject.Id,
            Name = realmObject.Name,
            ~~~~~~
            ~~~~~~
            ~~~~~~
            ~~~~~~
        };
        syncedRealm.Add(syncedObject, true);
    }
});

(1) Disclaimer RealmJson.Extensions is an extension I wrote, available via nuget package or source:

re: https://sushihangover.github.io/Realm.Json.Extensions/

查看更多
登录 后发表回答