I’m wondering if I can have an iOS program that stays in a loop, while the UI still works.
For example:
while (1) {
//do some calc here
Application.ProcessMessages();
}
Here the program stays in a loop, and uses Application.ProcessMesages() in order to update the screen, and get UI clicks, scrolls etc.
Is anything like this possible in IOS, maybe by using some form of a RunLoop?
I tried using a runLoop like this, it works for sometime, then the UI freezes eventually depending on what DoSomeWork(…) is doing for example downloading something from internet.
while(1) {
DoSomeWork(....);
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, YES);
}
I know there are other ways of doing things, but I was wondering if anything like this can work in IOS?
Or Do you know what could cause the freeze?
Thanks
The UI runs on the main thread, if you ahve an infinite loop doing work in the main thread it will block, you need to spawn a different thread in order to do the work, when you need to update the UI you should do so on the main thread as UIKit is not thread safe… here is one example of how to achive this, in my example the thread is being spawned to a selector (you can create subclasses of NSThread if you wish as well)
There is also the
NSObject performSelectorOnMainThreadthat will also have the same effect, note that if you call the dispatch main thread queue from the main thread it will block and lock.Hoope this helps