若需要在背景執行緒中定期執行某個方法,可先建立執行緒,再於該執行緒的 RunLoop 中加入 NSTimer。
可使用 NSThread detachNewThreadSelector:toTarget:withObject: 建立新的執行緒,並指定要執行的 method。
在新方法中建立 NSTimer,設定重複間隔與要呼叫的 selector,然後取得 currentRunLoop 並呼叫 run,讓 RunLoop 持續運作。
NSTimer 需要 RunLoop 才能觸發;若背景執行緒沒有 RunLoop,計時器可能不會執行。UI 更新仍應回到主執行緒。