0

I have a problem in parceling a list of objects inside a RealmObject.

I have a RealmObject called Player which extends Parcelable. Every Player has a list of RealmObject Game(s) (more specifically 3 Games). The Game object has fields like score, avg...

The way that I have built the app is that after querying the db I copy all the Players to an arraylist and when a rotation of the device occurs I use the Parcelable to be able to recreate the arraylist of Players. I do not know if it is the correct procedure but I thought it would be better not to read/write every time from/to the db.

The problem is that I can't find a way to Parcel the list of Games inside the Player object. I tried to use an ArrayList<Game> which I know that I can parcel but Realm shows me an error! If I try to use RealmList I do not know how to parcel the RealmList.

I have read many articles in the web but I have not managed to find a suggested solution. Is there a suggested way that we can use to parcel a RealmList or to use an ArrayList inside a RealmObject?

Thanks a lot.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I think you should use another object for your business logic (and implementing Parcelable) and other object extending RealmObject only to save/restore from Realm Database and use a Mapper to map between ObjectLogic and ObjectRealm. – MGDiez Feb 23 '17 at 12:01
  • Hello thanks for your answers! @MGDiez I haven't thought of using two different objects (one for logic and one for saving). Because I haven't used any Mapper before can you suggest any library? Thanks! – liakourinio Feb 23 '17 at 12:09
  • Use your own code! Read about Clean Architecture :) – MGDiez Feb 23 '17 at 12:11
  • `after querring the db I copy all the Players to an arraylist ` see, that's where you are using Realm wrong, you are supposed to just obtain `RealmResults` none of this `mapping to ArrayList` magic - if you use `RealmResults`, then you don't need to parcel anything – EpicPandaForce Feb 23 '17 at 15:28
  • Hello @EpicPandaForce. So, that means that every time I want to change a value of an object (lets say the score) I must use realmResults.get(i).setScore(10) inside a transaction and everything is stored in the db? And if I have set a field in an object as ignore because I do not want it in the db? Sorry for the questions but I try to understand the philosophy of Realm! – liakourinio Feb 24 '17 at 09:20
  • Refer to [**this example**](https://github.com/Zhuinden/realm-book-example/blob/master/app/src/main/java/com/zhuinden/realmbookexample/paths/books/BooksPresenter.java#L117-L130), technically all RealmResults are lazy-evaluated and auto-update because they just point to the results evaluated at a given version, but all the elements are in the Realm, so no additional parcellation is necessary – EpicPandaForce Feb 24 '17 at 10:38

0 Answers0