So, I’ve been working on an app that has a Plan model with a number of different inputs and outputs, and the layout of the app has slider controls for the inputs and labels for the outputs. When an input changes, it updates the model, which then runs a calculation, and then updates the views. I didn’t think there was anything wrong with this architecture at first, but even simple calculations seem to run really slowly, blocking the UI thread. Granted, I do have a somewhat complicated way of updating things:
- Slider (in a viewgroup subclass) updates its value and sends a message to a delegate (which implements an interface specific to that viewgroup subclass).
- Delegate (which holds the model and the control subviews) tells the Plan instance to set a new value, which triggers the plan to recalculate its outputs.
- Once the plan finishes its calculations, it sends another message to the delegate, which then tells its output views to update with the new values.
I’ve modeled this architecture off of an iOS app that I developed which didn’t seem to have as big of a problem running the calculations.
Now, I know that Android is significantly different than iOS, so I’m wondering if I’m going about this completely wrong. Is there a way to just tell these views to watch the Plan model for changes and then grab the value it’s supposed to display?
Another major issue that I’m seeing here is with the slider input. If I put the model update calculations into a thread, every time the slider changes, a new thread will be created. These threads (as I’ve seen) will more or less finish in random order, updating the view in such a way as too make very little sense when you should see incremental changes. Is there a good way of threading calculations that are supposed to be changeable with a seekbar?
Have you looked at Observer and Observable?
Maybe your observed model can perform the update using Runnable and then notify the observer.