0

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());
                        }
Joe Ginley
  • 301
  • 3
  • 15
  • I am sorry, but I don't really get your question. What exactly do you want to do? Copy an instance of `RealmObjectA` as an instance of `RealmObjectB`? – beeender Oct 07 '15 at 04:06
  • yes please. The data in RealmObjectA would be copied to RealmObjectB but A and B are 2 different instances. Example PropertyObject extends RealmObject FavoriteObject extends RealmObject. – Joe Ginley Oct 07 '15 at 05:20
  • 1
    I think there is no standard way to do this in Realm. I would like to create a static function in `RealmObjectA` like `copyToRealmObjectB` which takes two params, one is `RealmObjectA` and the other is `RealmObjectB`. And call setters and getters in the static function to do the copy. I am sorry but it doesn't look like a normal and reasonable requirement. Why don't you just use a `RealmObject` filed instead? See https://realm.io/docs/java/latest/#field-types – beeender Oct 07 '15 at 05:43
  • I understand, thank you. – Joe Ginley Oct 07 '15 at 19:54

0 Answers0