Realm - Copy Realm Object to Another Realm Object

2019-09-16 14:47发布

问题:

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());
                        }
标签: android realm