Pinx0
Pinx0

Reputation: 1258

Updating data periodically from db and pushing changes to UI

I want to automatically update the data shown in a part of my app, no matter what is doing the user.

The data comes from a query returning multiple rows, which I loop and fill a List<T> of custom objects with the data of the query.

Then the View is binded to a a property in the ViewModel which is in some way related to that list, for example as this:

  public int numFabricacionesEnCurso { get { return maquina.fabricacionesEnCurso.Where(fb => fb.idEstado == 1).Count(); } }

Being maquina.fabricacionesEnCurso the static list I mentioned above.

So basically I have two questions:

Upvotes: 0

Views: 391

Answers (1)

Nahum
Nahum

Reputation: 7197

Well the logigal thing to do is to have two threads running. one is the main UI thread. second is the DB thread.

the DB thread will periodicly poll the database. and update the model if the model is changed and EVENT will be triggered..

the viewmodel will subscribe to an EVENT of the model.

when the event is triggered the viewmodel will be updated and hence the view.

don't forget to unsubscribe when the window is closed to avoid memory leaks.

other method is to use the mediator pattern. that is more leak safe.

Upvotes: 1

Related Questions