views:

81

answers:

3

hi there!

i'm kinda new to programs which feature an mvc architecture and i want to get the right idea right up front...

i have this (android) application, where a thread (separate from the UI thread) computes some data. when finished, the results are written to a member of an object. now: how do i get the UI thread to "realize" that there has been an update (i.e., that the UI thread has to redo the screen output).

the most obvious way - to run an endless loop in the UI thread that checks in every loop if there has been an update - is pretty lame. so how do i do this "right"?

  • should i use a handler, that is called by the computation thread every time new data is available. this handler would then trigger a function that redraws the UI.

  • should i use wait() and notify() for the UI thread to be made aware of the fact that there is new data available?

i'm a bit lost here. although i know the basics of these concepts, i dont know which one is the "best" one to use here...

EDIT: to clarify:

i have a class A (which is an android Activity) and a class B which is instantiated by A. This class B has a member variable X and runs a thread, that computes the value of X. when the thread finishes, it sends a message (with the computed value) to a handler of B. this handler then sets the value of X. now: since B stores no reference of A, how do i "notify" A that there has been a change of the value of X? do i send another message object which is "caught" by a handler in class A?

+2  A: 

Are you aware of AsyncTask? It will solve all these complex problems for you.

kgiannakakis
+1  A: 

Use a handler and let the background thread notify the UI thread if an update is available.

Don't block the UI-Thread with a loop or with wait. This will make your User Interface unresponsive and get your App killed.

Janusz
+1  A: 

...your question seems to be more about thread management and its design than about mvc.

Then you should not use the class thread or the interface runnable but AsyncTask. It makes it a lot more easy to update UI and design your business logic. The goal of AsyncTask is to take care of thread management for you.

Have a look at the android developers blog. Painless threading

ArtWorkAD