在Xamarin.iOS中把本地境界转换为同步境界时出现异常[英] Exception when converting local realm to synced realm in Xamarin.iOS

本文是小编为大家收集整理的关于在Xamarin.iOS中把本地境界转换为同步境界时出现异常的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

请放心,我已经看到了相同问题的所有答案.点击此链接输入 switch-from-local-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);
            }

        });

    }

如您所见,我尝试了异步写入和写入方法.两者都给了我例外

当我使用 WriteAsync
我得到了一些异常,比如条件未满足数组长度 == 1(不完全记得)

当我使用当前代码时,我得到了这个异常

Realms.Exceptions.RealmObjectManagedByAnotherRealmException:当对象已由另一个领域管理时,无法开始管理具有该领域的对象

我将对象从本地领域复制到同步领域有什么问题吗?

推荐答案

您需要为您的 RealmObject 子类创建一个非托管副本在添加它之前新领域.

您可以手动或通过辅助扩展来执行此操作.

通过 RealmJson.Extensions(1) 的 NonManagedCopy 扩展:

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);
    }
});

手动复制:

将 RealmObject 子类的 each 属性分配给新实例化的非托管对象.

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

(1) 免责声明 RealmJson.Extensions 是我编写的扩展,可通过 nuget 包或源获得:

回复:https://sushihangover.github.io/Realm.Json.Extensions/

本文地址:https://www.itbaoku.cn/post/1556951.html

问题描述

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 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/