Realm - Copy Realm Object to Another Realm Object Type

老子叫甜甜 提交于 2019-12-12 05:30:03

问题


I am trying to copy one realm object data to another realm object data. Is this possible without having to set each property individually? I am doing the following but the app just stops responding and ends up crashing.

Realm realmThread = Realm.getDefaultInstance();

                    PropertyObject currProperty = realmThread.where(PropertyObject.class).equalTo("propertyId", propertyId).findFirst();
                    if (currProperty != null) {
                        int propertyFollow = Integer.parseInt(params[1]);

                        realmThread.beginTransaction();
                        currProperty.setPropertyFollowed(propertyFollow);
                        realmThread.commitTransaction();

                        if (propertyFollow == 1) {
                            realmThread.beginTransaction();
                            FavoriteObject newFavProperty = realmThread.createOrUpdateObjectFromJson(FavoriteObject.class, new Gson().toJson(currProperty));
                            realmThread.commitTransaction();

EDIT:

I have updated my code a bit and it does not crash anymore or stop responding but now when the new object is created the properties are empty. The properties do match, they are just different objects with a few additional properties to one of the objects.

Realm realmThread = Realm.getDefaultInstance();

                    PropertyObject currProperty = realmThread.where(PropertyObject.class).equalTo("propertyId", propertyId).findFirst();
                    if (currProperty != null) {
                        int propertyFollow = Integer.parseInt(params[1]);

                        realmThread.beginTransaction();
                        currProperty.setPropertyFollowed(propertyFollow);
                        realmThread.commitTransaction();

                        if (propertyFollow == 1) {
                            String propertyString = visnetawrap.gsonClient.toJson(currProperty);

                            realmThread.beginTransaction();
                            FavoriteObject newFavProperty = realmThread.createOrUpdateObjectFromJson(FavoriteObject.class, propertyString);
                            realmThread.commitTransaction();

                            Log.d("NewFavorite", newFavProperty.toString());
                        }

来源:https://stackoverflow.com/questions/32978484/realm-copy-realm-object-to-another-realm-object-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!