PLAYGROUND delay operation
Posted by Wang Wei (@ONEVCAT) on 2015/09/16
Chris's presentation from WWDC 14 Keynote shows that Playground is exceptionally powerful, but the idea of Playground in essence is very simple, which is to provide a REPL-like environment that can be instantly edited.
Playground provides us with a sequential execution environment in which the entire file is recompiled after each change, and the original state is emptied and run. This behavior is similar to the individual test cases at the time of testing, so there are some times when testing we encounter problems we will encounter in Playground.
One of the most fundamental of these is the execution of asynchronous code, such that it NSTimer
is not executed in the default Playground:
class MyClass { @objc func callMe() { print("Hi") }}let object = MyClass()NSTimer.scheduledTimerWithTimeInterval(1, target: object, selector: "callMe", userInfo: nil, repeats: true)
The use and marking of selector @objc
can be found in the selector as well as the @objc and dynamic respectively.
After executing the NSTimer
statement, the entire Playground will stop and will Hi
never be printed. Rest assured, this asynchronous operation does not take effect, not because you wrote the wrong thing, but Playground executes all the statements and then exits normally.
In order for the Playground to have the ability to run in time, we need to introduce the Playground "Expansion Pack" XCPlayground
framework. The framework now contains several APIs that interact with Playground's behavior and control the Playground feature, including the black magic that enables Playground to execute late XCPSetExecutionShouldContinueIndefinitely
.
All we need to do is add the following code:
import XCPlayground XCPSetExecutionShouldContinueIndefinitely(true)
You can see Hi
that the frequency is printed at once per second.
In real-world use and development, the asynchronous requirements that we face most often are network requests, and if we want to verify that an API works correctly in Playground, XCPlayground
it is also necessary to use this method to enable deferred execution:
let url = nsurl (string: "Http://httpbin.org/get")! let gettask = nsurlsession.sharedsession (). DataTaskWithURL (URL ) {(data, response, error), void in let dictionary = try! nsjsonserialization. jsonobjectwithdata (data!, Options: []) print (Dictionary)} Gettask.resume ()
There is also a limit to the delay operation. If you are patient enough, you will find that the NSTimer
count of Hi that is printed once per second in the first example will eventually stay at 30 times. This is because Playground does not run forever even if it is enabled for continuous execution, and by default it stops execution 30 seconds after the last sentence of the top-level code runs. This length of time is sufficient for most of the demand scenarios, but if you want to change the time, you can open the Alt + Cmd + 回车
auxiliary editor. Here you will see the console output and the timeline, changing the lower-right corner of the 30 to the number you want, you can set the maximum time to run the delay.
The previous sections, like GCD and deferred calls, also involve time-lapse, and you can apply the techniques here to the previous sample code so you can get the right results in Playground.
PLAYGROUND delay operation