Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Beta hangs often / SourceKitService high cpu

Tags:

xcode

swift

In some of my new projects, I have the problem, that Xcode Beta5 (I had this problem with earlier betas too) really often hangs ('beachball'). In the activity monitor I can see, that SourceKitService needs 100 % of the CPU. Xcode than hangs for at least 10-15 seconds.

I don't know, if it's a problem with my code. In the Console Utility I can see a lot of this error:

sourcekit-serv[63558]: [1:getBufferStamp:17199:1776.3650] failed to stat file: <imports> (No such file or directory)

UPDATE:

I found out, that working with the following is the problem: [[String: AnyObject]], just like:

var myArray: [[String: AnyObject]]
myArray = [
    ["name": "item1", "children": [
        "name": "child1", "children": [],
        "name": "child2", "children": []]],
    ["name": "item2", "children": [
        "name": "child1", "children": [],
        "name": "child2", "children": []]]  
]

But CAUTION: This code completely blocks Xcode after a few seconds!

like image 639
Lupurus Avatar asked Aug 09 '14 13:08

Lupurus


1 Answers

After struggling with this for a while and reading many posts by people struggling with the same issue, I realized that the meta theme is: Swift struggles with "complicated" arrays and dictionaries. In order to help Xcode I explicitly defined the type of variable for all of my arrays and dictionaries instead of letting Xcode figure it out. This made my problems go away.

Instead of letting Swift figure out the type of variable by writing the following line of code:

var myDictionary1 = ["Item":[1,2,3],"Thing":[4,5,6],"Weight":[7,8,9]]

I rewrote my variable declarations as follows:

var myDictionary2: [String:[Int]] = ["Item":[1,2,3],"Thing":[4,5,6],"Weight":[7,8,9]]

As a side note, Xcode's autocompletion shows myDictionary1 as [String:ArrayInt] and myDictionary2 as [String:[Int]].

like image 139
StemOner Avatar answered Jan 09 '23 12:01

StemOner