My app is susceptible to sudden spikes in memory usage, and, in rare cases, even a memory leak, so I created a kind of daemon task that keeps an eye on memory usage, reports it, and in extreme circumstances (if memoryUsage > userDefinedMaxMemoryAllowed), it exits the app.
I figured out how to run the *selector* (i.e. the user-defined function called by the NSTimer) of the NSTimer task in a separate thread ... using:
However, this is no good, because the NSTimer itself runs in the main thread ! So, if the main thread is blocked waiting for, say, an open modal dialog to close (user selecting files to open), then the NSTimer will be blocked, and the async task will not be able to run ! I verified this easily. I need my timed daemon task to run no matter what happens in the main thread, which is to say, independently of user actions.
So, what ends up happening, every timer interval, is this:
Timer (from main thread) -> spawn new async task (in separate thread) for selector
Timer (from main thread) -> spawn new async task (in separate thread) for selector
Timer (from main thread) -> spawn new async task (in separate thread) for selector
...
What I want, every timer interval, is this:
Timer (from separate thread) -> do task in same thread
Timer (from separate thread) -> do task in same thread
Timer (from separate thread) -> do task in same thread
...
So, I want the NSTimer itself to run completely in a separate thread, not just spawn a new async task each time it fires. If I could tell the NSTimer which thread to use, I could accomplish this, but the NSTimer API doesn't seem to allow this. How do I do this ?
Thanks.
I figured out how to run the *selector* (i.e. the user-defined function called by the NSTimer) of the NSTimer task in a separate thread ... using:
Code:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
...
})
However, this is no good, because the NSTimer itself runs in the main thread ! So, if the main thread is blocked waiting for, say, an open modal dialog to close (user selecting files to open), then the NSTimer will be blocked, and the async task will not be able to run ! I verified this easily. I need my timed daemon task to run no matter what happens in the main thread, which is to say, independently of user actions.
So, what ends up happening, every timer interval, is this:
Timer (from main thread) -> spawn new async task (in separate thread) for selector
Timer (from main thread) -> spawn new async task (in separate thread) for selector
Timer (from main thread) -> spawn new async task (in separate thread) for selector
...
What I want, every timer interval, is this:
Timer (from separate thread) -> do task in same thread
Timer (from separate thread) -> do task in same thread
Timer (from separate thread) -> do task in same thread
...
So, I want the NSTimer itself to run completely in a separate thread, not just spawn a new async task each time it fires. If I could tell the NSTimer which thread to use, I could accomplish this, but the NSTimer API doesn't seem to allow this. How do I do this ?
Thanks.
Last edited: