What are the tradeoffs between performSelector:withObject:afterDelay: and dispatch_after

后端 未结 2 719
故里飘歌
故里飘歌 2020-12-08 05:17

The only functional difference I have encountered is that I can cancel the message scheduled with performSelector:withObject:afterDelay:. I don\'t know of a way

2条回答
  •  青春惊慌失措
    2020-12-08 05:50

    Another big advantage of using GCD instead of performSelector is the ability to very simply use multiple local variables as part of the block operation. If you want to defer the execution of a method that takes more than one argument until a later time using performSelector, you have to wrap the arguments you want to use in another object, such as an array. With dispatch_after you can very simply pass any number of local variables to the block. This also applies to non-objects, which you can't pass to a performSelector call without first wrapping in an object, such as an NSValue for passing a CGRect. GCD lets you pass primitives, structs, and objects to the operation you want to defer.

提交回复
热议问题