Swift PerformSelector

一、Runloop


無輸入的sources 或 timers事件源,那么runloop會立即退出。
image.png

一、PerformSelector延遲事件


perform(<#T##aSelector: Selector##Selector#>, with: <#T##Any?#>, afterDelay: <#T##TimeInterval#>)
作用向當(dāng)前Runloop添加一個(gè)timer事件源

1.1、在子線程執(zhí)行未開啟runloop

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        print("touchesBegan1")
        DispatchQueue.global().asyncAfter(deadline: .now() + 3) { [self] in
            print("touchesBegan2")
            self.perform(#selector(onPerformAction), with: nil, afterDelay: 1)
            print("touchesBegan3")
        }
        print("touchesBegan4")
    }
    
    @objc func onPerformAction() {
        print("onPerformAction:\(Thread.current)");
    }
image.png
1.2、在子線程執(zhí)行開啟runloop 結(jié)果: 執(zhí)行onPerformAction成功

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        print("touchesBegan1")
        DispatchQueue.global().asyncAfter(deadline: .now() + 3) { [self] in
            print("touchesBegan2")
            // 下面這行代碼等于向當(dāng)前runloop添加timer事件源
            self.perform(#selector(onPerformAction), with: nil, afterDelay: 1)
           // 開啟runloop時(shí),已經(jīng)有timer事件源,所以runloop不會因?yàn)闊o任何事件源而退出
            RunLoop.current.run()
            print("touchesBegan3")
        }
        print("touchesBegan4")
    }
image.png

image.png
1.3、在子線程執(zhí)行開啟runloop 結(jié)果:執(zhí)行onPerformAction失敗

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       super.touchesBegan(touches, with: event)
       print("touchesBegan1")
       DispatchQueue.global().asyncAfter(deadline: .now() + 3) { [self] in
           print("touchesBegan2")
           RunLoop.current.run()
           self.perform(#selector(onPerformAction), with: nil, afterDelay: 1)
           print("touchesBegan3")
       }
       print("touchesBegan4")
   }
image.png
image.png
1.4、取消PerformSelector延遲事件

@objc func onCancelPerformAction() {
        /** 1.單獨(dú)取消perform的執(zhí)行事件*/
        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(onPerformAction), object: nil)
        /** 2.取消perform的所有執(zhí)行事件*/
        NSObject.cancelPreviousPerformRequests(withTarget: self)
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基礎(chǔ)用法 performSelecor響應(yīng)了OC語言的動態(tài)性:延遲到運(yùn)行時(shí)才綁定方法。當(dāng)我們在使用以下方法時(shí): 編...
    KB_MORE閱讀 213評論 0 1
  • 最近在面試的過程中才發(fā)現(xiàn)太多沒有注意的細(xì)節(jié),每一個(gè)問題問到最后都是在懷疑人生中度過... 正好趁著工作敲定了之后將...
    李周閱讀 17,702評論 10 100
  • 1、performSelector簡單使用 performSelector(方法執(zhí)行器),iOS中提供了如下幾種常...
    luonaerduo閱讀 759評論 0 0
  • 轉(zhuǎn)自公眾號:NA分享 performSelector延遲調(diào)用 這個(gè)方法其實(shí)是增加了一個(gè)定時(shí)器,而這時(shí)aSelect...
    iwakevin閱讀 1,923評論 0 2
  • 上一節(jié)《說一說基類 NSObject(三)》中我們學(xué)習(xí)了NSObject類中的三個(gè)方法: 簡單回憶一下。新建一個(gè)t...
    小曼blog閱讀 880評論 0 6

友情鏈接更多精彩內(nèi)容