objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

How to listen to the first of ToMany

Open TinoGuo opened this issue 7 years ago • 4 comments

@Entity
class Session {
    @Id
    var id: Long = 0
    
    lateinit var messages: ToMany<Message>
}

In my app, there is a session list, we need to listen to the first of messages, but now I can't find any direct method to listen.

My question: Is there any simple workaround to listen? BTW, now I need to add a extra field latestContent to solve this problem. Just as below.

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

    //when update messages, update this field
    lateinit var latestContent: String
    
    lateinit var messages: ToMany<Message>
}

TinoGuo avatar Sep 28 '18 07:09 TinoGuo

You can subscribe to a query for your Session entity. Your code will get a new set or results (in this case one) each time the entity is modified then.

Does that work for you? -ut

greenrobot-team avatar Oct 01 '18 09:10 greenrobot-team

If the messages were very big amount of items for example 100,000 , will it be very slow? Thanks.

TinoGuo avatar Oct 02 '18 08:10 TinoGuo

By default ToMany is loaded on access. But to get the first item you probably will do that. If there are many items then yes, it may be slow and require a lot of memory. Improvements may be part of #510.

Another approach might be to change your model and try subscribing to a query on the Message box instead. However, until #303 it is not possible to set a limit when subscribing to a Query. -ut

greenrobot-team avatar Oct 02 '18 12:10 greenrobot-team

@greenrobot-team For my situation, I need to subscribe the last message that means insert will change the message entity. I can't get benifit from the two approach.

BTW, any feature #510 or #303 will come in next release?

TinoGuo avatar Oct 08 '18 08:10 TinoGuo