0

My iOS Swift app performs some lengthy (a second or more) calculations based on inputs from a number of sliders. I would like to restart the calculation every time a slider is moved and an input parameter is changed. This means, I guess, that the calculation needs to happen on a background thread, but what's the best way to stop and restart the calculation? If I kill the background thread each time, I could end up restarting it hundreds of times. How can I avoid using excessive memory with recreating objects?

Thanks,

Julian

Julian7
  • 191
  • 1
  • 12

2 Answers2

0

First of all you can't or you should not kill background thread. For better understanding you need to view this link.I would like to suggest that you need to show activity indicator and wait for first calculation execution.If you do calculation on main thread it will also take less time than background thread.

Community
  • 1
  • 1
Matloob Hasnain
  • 1,025
  • 6
  • 21
  • Thanks for that comment, and the reference to the other link. I can see the point about not killing background threads. – Julian7 Jan 05 '17 at 08:58
0

I would put some checkpoints in your lengthy calculation code (before beginning a new iteration, for example) and check if the input params have changed. If yes, abort and start again.

Now, if you want to avoid re-starting the calculation too many times, you could add a delay between a slider change and the start of the lengthy calculation. In this way you give the user a chance to make several adjustments before starting the background work.

Bogdan Farca
  • 3,856
  • 26
  • 38
  • Thanks for those comments. Very helpful. You have made my problem easier to solve than I thought. – Julian7 Jan 05 '17 at 08:57