androidandroid-databaseobjectboxobjectbox-android

Objectbox Relations: Updating/deleting toMany automatically when updating/deleting Object


I have 3 questions about using the toMany Relations with the objectbox database. Assumed that I have a tvchannels entity:

@Entity
data class TvChannel(
        @Id var id: Long = 0,
        var name: String? = "",
    var epgChId: String? = ""
)
{
    lateinit var epgData: ToMany<EpgData>
} 

and a EpgData entity:

data class EpgData(
        @Id var id: Long = 0,
        var name: String? = "",
    var epgChId: String? = "",
    var epgSourceId: Long? = null,
    var date: String? = "",
    var startTimeStamp: Long? = null,
    var stopTimeStamp: Long? = null
)

Adding EpgData to tv channels with:

for (chann in channels) {
val epg = withContext(Dispatchers.IO) {
    epgDataBox.query(EpgData_.epgChId.equal(chann.epgChId)
       .and(EpgData_.date.equal(todayDateString)))
       .build().find()
  }
chann.epgData.addAll(epg)
tvChannBox.put(chann)
}
  1. When different channels can have the same epgChId / the same EpgData, when should I add also
@Backlink(to = "epgData")
 lateinit var tvchannels: ToMany<TvChannel>

to the EpgData Entity? It is not indispensable, correct?

  1. Assuming the user changes the timeoffset of the epgSource and every startTimeStamp & stopTimeStamp property of the EpgData Objects with the related epgSourceId (of the changed EpgSource) will be adjusted and updated. Whats with the epgData from the TvChannel Entity? As it is only a relation to the EpgData I suppose that the TvChannel then uses the updated timestamps, correct?

  2. Main question: What happens when an EpgData object is deleted from the database. For example I want to delete old EpgData from the database (date older then 3 days). It isn't automatically deleted from epgData in the TvChannel Entity or? So if a deleted Object is part of a toMany-relation, it still exists there? If yes, what would be the best way to remove deleted Objects also in the relations?

Problem: Lets say there are 20.000 EpgData Objects to delete and I have 500 tv channels (add. multiple channels use the same EpgData objects relation). When I have to iterate throw all channels and remove the deleted EpgData step by step, its quiet a large amount of data to check / remove. Is there a simple or easier way to do this? Or is @Backlink the solution for this?

Thanks in advance, hopefully one of the objectbox experts can help a beginner hobby developer.


Solution

    1. The backlink property is optional. It is based on an actually existing relation.

    2. The relation is based on object IDs. So yes, the target objects will have updated data when the object owning the relation is get afterwards.

    3. If an object is removed, it is also removed from all relations it was pointed to.