0

I'm building the chat application using JavaFX and MvvmFX and got into little system design challenge.

I want to avoid using FX Observable lists in my model due to my model can be updated in multithreading environment and I don't need my model be JavaFX dependent at all.
So the model will be simple jdk lists and view-model will be Observable JavaFX lists that is bound by view.

But what is a proper way to keep the model and view-model in sync?

For example, when the new contact is added to the model list, or the contact status changed the model changes -> so the view-model also needs to be changed.

There is a solution like glazed lists https://github.com/glazedlists/glazedlists, providing additional features like built-in change listeners for collections but it feels like not ideal solution as well. But even if I use it, every-time it triggers, I need to rebuild my view-model or write some logic how to figure out what item in model is changed to sync it in VM.

Anyone has solved that before? Please share your ideas! Thanks in advance!

nettleclaw
  • 101
  • 1
  • 6

1 Answers1

1

That's a very interesting question which I also investigated a little bit. My conclusion is, that in a pure Java environment Reactive Streams are probably the way to go. In Kotlin you could even do better with Coroutines and Flows.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • 1
    No matter what you do, you'll probably need a wrapper around the Lists in your multi-threaded environment to invoke changes to the ObservableLists on the FXAT. It seems trivial to implement, unless you have have crazy concurrency issues that you'll need to resolve into asynchronous events on the FXAT. – DaveB Apr 22 '21 at 14:12
  • 2
    Maybe it is worth having a look at this: https://github.com/ReactiveX/RxJavaFX – mipa Apr 22 '21 at 14:17
  • I would also recommend RxJavaFX. I've used RxJavaFX together with mvvmFX in the past and it was working quite well. However, you have to have some RxJava knowledge to get the concurrency issues right. The JavaFX part is not that much of a problem as long as all changes are done on the FX thread, which is what RxJavaFX is all about. – Manuel Mauky Apr 23 '21 at 11:07