我正在創建一個NSTimer並將其添加到後臺線程的runloop。 我的代碼是像該答案後臺線程例如:iPhone-SDK:Call a function in the background?NSTimer無效後,後臺線程NSRunloop運行不會退出!爲什麼?
創建計時器,它從GDB attching到runloop後我po runLoop
並輸出這樣的:
<CFRunLoop 0x695b090 [0x16a62c0]>{wakeup port = 0x6907, stopped = false,
current mode = kCFRunLoopDefaultMode,
common modes = <CFBasicHash 0x6936e60 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
1 : <CFString 0x16abba8 [0x16a62c0]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = <CFBasicHash 0x695e160 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
0 : <CFRunLoopTimer 0x69398a0 [0x16a62c0]>{valid = Yes, interval = 6, next fire date = 329774303, callout = __NSFireTimer (0x212399), context = <CFRunLoopTimer context 0x6903a10>}
}
,
modes = <CFBasicHash 0x6904120 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
1 : <CFRunLoopMode 0x6946180 [0x16a62c0]>{name = kCFRunLoopDefaultMode, port set = 0x6807, timer port = 0x6b03,
sources0 = (null),
sources1 = (null),
observers = (null),
timers = <CFArray 0x695e180 [0x16a62c0]>{type = mutable-small, count = 1, values = (
0 : <CFRunLoopTimer 0x69398a0 [0x16a62c0]>{valid = Yes, interval = 6, next fire date = 329774303, callout = __NSFireTimer (0x212399), context = <CFRunLoopTimer context 0x6903a10>}
)}
},
}
}
這表明1個定時器連接到runloop 後來經過我的無效計時器NSRunloop run方法不退出,但之後我暫停調試器和GDB從我po runLoop
再次,它看起來是這樣的:
<CFRunLoop 0x695b090 [0x16a62c0]>{wakeup port = 0x6907, stopped = false,
current mode = kCFRunLoopDefaultMode,
common modes = <CFBasicHash 0x6936e60 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
1 : <CFString 0x16abba8 [0x16a62c0]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = <CFBasicHash 0x695e160 [0x16a62c0]>{type = mutable set, count = 0,
entries =>
}
,
modes = <CFBasicHash 0x6904120 [0x16a62c0]>{type = mutable set, count = 1,
entries =>
1 : <CFRunLoopMode 0x6946180 [0x16a62c0]>{name = kCFRunLoopDefaultMode, port set = 0x6807, timer port = 0x6b03,
sources0 = (null),
sources1 = (null),
observers = (null),
timers = <CFArray 0x695e180 [0x16a62c0]>{type = mutable-small, count = 0, values =()}
},
}
}
現在「計時器」條目有0個對象。但線程繼續運行。我經常離開屏幕然後回來,所以這導致建立後臺線程,在使用太多資源後最終會終止應用程序。定時器在失效後不會觸發,但後臺線程保持不變。
我知道我可以將定時器移回主線程或使用NSThread sleepForTimeInterval創建我自己的簡單定時器線程,但我想保留GUI更新的主線程並儘可能使用NSTimer。