-1

I'm using kotlin and ObjectBox in my application. My object box entity looks something like

@Entity
class Order {
    @Id var id: Long = 0

    lateinit var customer: ToOne<Customer>
}

@Entity
class Customer {
    @Id var id: Long = 0

    @Backlink
    lateinit var orders: List<Order>
}

But when I use @Parcelize, the properties are being ignored in the parcel. How do I use @Parcelize but still include these properties? I tried overriding writeToParcel but I am not allowed to override it due to @Parcelize.

Sriram R
  • 2,109
  • 3
  • 23
  • 40

1 Answers1

0

According to docs, you have to declare all properties in primary constructor, which should be serialized via @Parcelize. All other ones are ignored.

ObjectBox doesn't support ToOne so you have to write custom Parceler. In the end your solution should look like this:

@Entity
@Parcelize
@TypeParceler<ToOne<Customer>, ToOneCustomerParceler>
class Order(
    @Id var id: Long = 0,
    var customer: ToOne<Customer>
) : Parcelable

@Entity
@Parcelize
class Customer(
    @Id var id: Long = 0,
    @Backlink var orders: List<Order>
) : Parcelable

object ToOneCustomerParceler : Parceler<ToOne<Customer>> {
    override fun create(parcel: Parcel): ToOne<Customer> {
        //Somehow recreate ToOne instance
        ...
    }

    override fun ToOne<Customer>.write(parcel: Parcel, flags: Int) {
        val customer = target
        ...
    }
}

Also don't forget to include correct dependencies:

dependencies {
     compile "io.objectbox:objectbox-android:$objectboxVersion"
     compile "io.objectbox:objectbox-kotlin:$objectboxVersion"
}

P.S. Use different models for each purpose (@Entity and @Parcelize) even if both are the same. It is much easier to manage them since you separate your intentions into 2 models, rather than trying to push everything into single one.

hluhovskyi
  • 9,556
  • 5
  • 30
  • 42
  • I can 100% guarantee that "ToOne" won't work out of the box – EpicPandaForce May 28 '18 at 12:54
  • 1
    @EpicPandaForce Ok, probably you are right. Let me investigate ObjectBox API and I will update answer. I think everything what is needed here is custom `Parceler` for `ToOne` type – hluhovskyi May 28 '18 at 12:56
  • Ok, `ToOne` is really hard to recreate since it contains some internal things like relations. I've experienced similar issue when I used Realm. That's why I suggested to use 2 different models for different cases and I think it is the best way to go now. – hluhovskyi May 28 '18 at 13:28
  • oh I actually had some way for RealmLists https://stackoverflow.com/a/49456435/2413303 but obviously it'll become an unmanaged RealmList – EpicPandaForce May 28 '18 at 13:35
  • I've updated answer but I have no idea how to recreate `ToOne` manually. So basically, my answer is a little less than useless :) – hluhovskyi May 28 '18 at 13:43
  • 1
    By the way, I think it is completely wrong way to mix `@Parcelize` and `@Entity` in one model. I don't how it is going with ObjectBox but with Realm so many things can happen which throw exception (serializing on another thread, closed session so relation no longer available, etc) – hluhovskyi May 28 '18 at 13:48
  • technically if Realm session is closed, **every** variable is no longer available :p so it's like, "if you want to detach obj via parcelable then it's on you" imo, but hey, some people wanna copy out unmanaged objects, and it sometimes makes sense --- but this is an ObjectBox discussion so I'll stop clogging up the comments :D – EpicPandaForce May 28 '18 at 14:00