I have the following method, which, if I comment it, there is no delay when building on the simulator or on a regular iPhone. If I use this method, it delays a lot of time before it runs.
Here's the code
func createFirstRowView() {
let b = SKSpriteNode(imageNamed: "obj_bg_card1")
b.xScale = 0.8
b.yScale = 0.8
r = SKShapeNode(rectOf: CGSize(width: b.size.width*4, height: b.size.height))
r.position = CGPoint(x: frame.midX, y: frame.midY)
//r.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
self.addChild(r)
for i in 0..<4{
let bg = SKSpriteNode(imageNamed: "obj_bg_card1")
bg.xScale = 0.8
bg.yScale = 0.8
let lel = 0.5 + (Double(i)*1)
bg.position = CGPoint(x: -bg.size.width*2 + bg.size.width*CGFloat(lel), y: 0)
r.addChild(bg)
}
r.position = CGPoint(x: frame.midX, y: frame.midY*1.4)
let q = SKSpriteNode(imageNamed: "obj_bg_card1")
q.xScale = 0.8
q.yScale = 0.8
t = SKShapeNode(rectOf: CGSize(width: q.size.width*4, height: q.size.height))
t.position = CGPoint(x: frame.midX, y: frame.midY)
//t.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
self.addChild(t)
for k in 0..<4{
let bg = SKSpriteNode(imageNamed: "obj_bg_card1")
bg.xScale = 0.8
bg.yScale = 0.8
let lel = 0.5 + (Double(k)*1)
bg.position = CGPoint(x: -bg.size.width*2 + bg.size.width*CGFloat(lel), y: 0)
t.addChild(bg)
}
t.position = CGPoint(x: frame.midX, y: frame.midY*0.6)
}
I read that this is caused due to code inefficiency... Can anyone spot my mistake?
My suggestion is to separate this method to several ones and look at compilation time of each one, spotting the wrong place
For this turn on the debug-time-function-bodies
option for the compiler. In your project in Xcode, go to Build Settings and set Other Swift Flags to -Xfrontend -debug-time-function-bodies.
Then jump over to the Build Report navigator with ⌘-8 where you’ll see the most recent build (and possibly some others).
Next, right-click on the build log for the target you built and select Expand All Transcripts to show the detailed build log.
Finally, you should see a series of green boxes, each representing a file or step in the compilation process. The text inside these boxes may take a moment (or a click) to load properly. If you correctly set up the build flags to show function compilation times, you should see a line of build times along the left.
Taken from here