Fixing Low Fps in Swift Playground

Fixing low FPS in Swift Playground

On the Mac, Xcode's "Playgrounds" are super useful for quick experiments but, due to their nature, terribly slow for "real" tasks.

If your code is more than a few pages long, and/or involves working with the UI, as you do with SpriteKit, the Playground may become really slow, sometimes even unresponsive.

"Playgrounds" are part of Xcode and run on top of the iOS simulator - that's how they display graphics and UI in the "Assitant Editor". The iOS simulator is not really known to be fast either.

On the other hand, "Swift Playgrounds" on iOS is a completely different app, even if it shares a lot with its Mac cousin.

Most importantly, it runs in iOS on the real device, with the real hardware processing, not emulation, which makes it ideal to use for SpriteKit, as Apple themselves often shows in their demos.

I would therefore say that your code should indeed run faster/better/properly on the iPad version.

Even if of course, I can't really know, since I don't know your code - you will probably be the one telling us later if using the iPad version made a difference.

How Do You Debug Swift PlaygroundBook?

From a bug report / suggestion I sent to Apple, I got the following reply:

We’ve actually built tools to help debug the auxilliary sources and we did a presentation at WWDC 2018 that demonstrates it. Please view the presentation and get access to the tools here: https://developer.apple.com/videos/play/wwdc2018/413/

Upon further research, I found that they have recently released a Playgrounds Author Template:

The Swift Playgrounds Author Template is a starter Xcode project that will help you create, debug, and produce a Playground book. Using the template you can step through the code for your live view as if it were an app so that you can identify bugs more easily and develop an efficient workflow for developing your Playground books.

This template, requiring Swift 4.1 to run, includes three different targets:

  • PlaygroundBook
  • Book_Sources
  • LiveViewTestApp

Sample Image

You can use the LiveViewTestApp to fully debug your Playground Book right on your Mac with Xcode.

How to Change ARKit Update Function Frequency (FPS)?

So I was facing the same issue and I found a decent workaround for this problem. You could set a timer variable and based on the timer then call whatever you need in the didUpdate function. I'll provide some code example below where I will call the session Didupdate frame function every 0.5 seconds. (You may alter it to fit whatever looks best)

Code example:

//setting up timer variables and functions 
var time = 0.0
var timer = Timer()

func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}

func resetTimer() {
timer.invalidate()
time = 0
startTimer()
}

@objc func updateTime() {
if time < 0.5 {
time += 0.1
} else {
resetTimer()
}
}

In your viewdidload or viewwillappear call

startTimer()

Finally in your didUpdate function make a conditional statement

if time != 0.5 {
return
} else {
* your code *
}

How do I get SpriteKit Playgrounds working in Xcode 8 beta?

The XCPlayground module has been replaced by the PlaygroundSupport:

import PlaygroundSupport
import SpriteKit

let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 480, height: 320))

let scene = SKScene(size: CGSize(width: 480, height: 320))
sceneView.showsFPS = true
sceneView.presentScene(scene)
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView

let square = SKSpriteNode(imageNamed: "square")
square.name = "shape"
square.position = CGPoint(x: scene.size.width * 0.25, y: scene.size.height * 0.5)

let circle = SKSpriteNode(imageNamed: "circle")
circle.name = "shape"
circle.position = CGPoint(x: scene.size.width * 0.50, y: scene.size.height * 0.5)

let triangle = SKSpriteNode(imageNamed: "triangle")
triangle.name = "shape"
triangle.position = CGPoint(x: scene.size.width * 0.75, y: scene.size.height * 0.5)

scene.addChild(square)
scene.addChild(circle)
scene.addChild(triangle)


Related Topics



Leave a reply



Submit