Swift Xcode Index Freezing or Slow
Asked Answered
W

25

105

Maybe this is just me experiencing such an annoying "feature":

After upgrading from Xcode 6.0.1 to Xcode 6.1, things changed. Xcode 6.1 is forever indexing the project or compiling source files. The project is not a huge one. It just contains a bunch of Swift files and AWS SDK 2.0 Cocoapods in the workspace. I don't think it should prevent the whole to index and compile smoothly. I tried with some aws-sdk-ios-samples, just to see how Xcode 6.1 works on them, and it ended up in the same forever waiting.

What solutions I have tried so far:

  1. Deleting "Derived Data" in the Organizer, and re-open and workspace. (fail to fix)
  2. "Show Package Contents" on the .xcodeproj file and deleting .xcworkspace as in (Xcode 4 - slow performance)

None of them worked, unfortunately.

P.S. maybe I should try re-creating the project? My computer settings: MacBook Pro (Retina, 13-inch, Mid 2014), Memory 8 GB 1600 MHz DDR3, with Yosemite. (I think this is enough for running this small project.)

Witenagemot answered 21/10, 2014 at 18:49 Comment(3)
Similar expierences? Yes: Xcode beta 3 Swift indexing foreverStyle
I won't be of much help but what I do is... I just let xcode 6.x take it's time. If i fight it, it screws me over big time so I just wait till xcode 6 starts. I let it run it's stupid indexing. I then start the iOS simulator separately and wait till it's stupid springboard shows up. Finally when I run a project, I overlook the fact that many times the Indexing says "Paused" and I pray to <#godObject#>, drink coffee and try to enter zen mode. But *sigh*, it's rough :/ and I had really liked xcode 5.1.1 :|Threadbare
This answer worked for me: https://mcmap.net/q/204344/-swift-xcode-index-freezing-or-slowPeriotic
A
72

I tried many of the suggestions above including splitting files, installing Xcode 6.2 beta, and breaking string concatenation statements. What finally did it for me was splitting an array of dictionaries literal declaration I was using for test data into multiple .append statements.

// This causes indexing/building to hang...
var test = [ [ "a": false, "b": "c" ],
             [ "a": false, "b": "c" ],
             [ "a": false, "b": "c" ],
             [ "a": false, "b": "c" ],
             [ "a": false, "b": "c" ],
             [ "a": false, "b": "c" ] ]

// This works fine.
var test = [ [ "a": false, "b": "c" ] ]
test.append([ "a": false, "b": "c" ])
test.append([ "a": false, "b": "c" ])
test.append([ "a": false, "b": "c" ])
test.append([ "a": false, "b": "c" ])
test.append([ "a": false, "b": "c" ])

Also, for what it's worth, the 6th entry in this array is what causes the issue for me; five works just fine.

Africanize answered 17/12, 2014 at 17:29 Comment(13)
Same solution worked for me with a dictionary. Initialising it directly caused a forever running indexing. Initialising it in init() step by step does work perfectly. Worth to mention that I had to delete the .xcworkspace file too.Vigilance
Fixed it for me with no dictionaries involved. Just an array of tuples which was fine with 8 elements in the declaration. Had to revert to appends with 11 elements. Didn't bother to find the exact breaking point but builds 10x faster. Xcode version 6.1.1 (6A2008a) on Mavericks.Mescaline
I hit the issue with exactly 6 elements as well. 5 was fine.Kelley
Is this an XCode problem or a compiler problem?Tare
Thanks for the sleuthing here. It's a pretty frustrating bug and it persists well into production builds. I'm using Xcode 6.3.1 on Yosemite.Bolick
This makes me wonder whether static constants/vars/methods generally make Xcode slower to index? I have several in my current project and indexing is very slow.Cloyd
When my xcode started to index forever, I have no idea it is due to my own code until I found this... thank you!! It also heats up my Macbook until I have to shut it down, scary stuffFrigate
there are only a few things i really get pissed off at apple for, and this is oneColumba
Still persists on Xcode 6.4. This solution worked but required restarting xcode a few times. Possibly deleting the .xcworspace file as jboi suggested may have done the trick quicker.Kedge
Worked for me on XCode 7. Deleting DerivedData folder was also required.Boyar
On Xcode 7.2 the line that was causing me trouble was this class func downloadCulture(culture: Culture, progress: ((done: Int, total: Int)->(Void))?, completion: (result: Int)->(Void)) Simply the declaration of the functionFictitious
I experienced this in Xcode 7.3 with an array literal of 229 items. Instead of breaking it up, I added a type declaration instead of letting Swift infer the type. After I did that, it worked.Cartridge
Did you open a radar for this? if so whats the #?Ulrika
L
37

The only working solution for me is to delete all the derived data (not only for the current project, just clean up the whole folder) and then restart Xcode.

  1. Open File / Preferences in Xcode

  2. Click on Locations on the far right of the pop up window

  3. Click on the little arrow icon next to "/Users/Mac/Library/Developer/Xcode/DerivedData"....it takes you to an Xcode folder that contains a DerivedData folder (that contains all of the derived data from your previous projects.)

  4. DELETE the DerivedData folder

Levania answered 20/2, 2015 at 15:17 Comment(4)
@TravisM. Did you follow the steps? If you go to Window, Organizer, Projects, Delete Derived Data, it won't work. You need to go through Preferences.Garboil
@Garboil Yeah, but it didn't work. I did fix my issue, though, it was related to me initializing a dictionary with hardcoded data in one shot. When I moved the code to add the hardcoded data one row at a time, the freezing / slow issues disappeared immediately.Villus
Folder at ~/Library/Developer/Xcode/DerivedData/ on my machine. You can also find it by going to Window > Projects in Xcode.Peculium
Thanks. This helps. The "forever indexing" problem starts right after i updated my cocoaPod version and executed a pod install.Massa
C
11

Are you using CocoaPods? I ran across the same issue earlier today. (Using xCode 6.1.1)

To fix the issue, I deleted everything in ~/Library/Developer/Xcode/DerivedData, the Pods folder in my project directory, and <project>.xcworkspace.

I then opened terminal, navigated to my project directory, and ran pod install again.

Ciliolate answered 5/1, 2015 at 15:57 Comment(3)
Yes I do. For now the Xcode is behaving ok. But it still from time to time gives sourcekit crash error. My solution is to delete the derived data folder.Witenagemot
Glad to see that this worked for someone else too :). Seems like xCode is trying to index CocoaPod Libraries and gets stuck somewhere.Ciliolate
Deleting the Pods folder and <project>.xcworkspace file, and then running pod install again worked.Cheney
C
10

Had the same issue today. Xcode 6.3.2, medium-sized Swift project. At one point it started indexing and it would never finish indexing. The code that caused this was a dictionary of type [String:[String]], so a string-key dict with string arrays as values. I had two of these with keys from A to Z and each of these 26 entries contain a string array with 5 to 10 strings.

Clearing derived data didn't help. Only commenting out those dicts made it go again.

Honestly, this is ridiculous! Apple needs to fix Xcode! It's already horribly slow when compiling Swift projects but bugs like this are a showstopper. I can't do my job properly with this!

Cloyd answered 27/5, 2015 at 3:32 Comment(0)
C
6

For those still having this issue, this is a workaround I've come to enjoy which prevents you from having to enter the objects one by one:

// instead of this, which freezes indexing
let keys = [keyQ, keyW, keyE, keyR, keyT, keyY, ... keyM]    

// and instead of this, which is ugly & lengthy
var keys = [KeyboardKey]()
keys.append(keyQ)
keys.append(keyW)
...
keys.append(keyM)

// use this:
var keys = [KeyboardKey]()
keys.appendContentsOf([keyQ, keyW, keyE, keyR, keyT, keyY, ... keyM])
Crassus answered 28/10, 2015 at 15:59 Comment(1)
Wow... I had the exact same issue. Thanks! I reported it as a bug to Apple. Doing what you said didn't work for me. I had to break up my long array in to smaller arrays each with five or less elements.Periotic
A
5

For me, I tried all the above with no success; but all I had to do was to delete the derived data folder, then open up another random project, wait for it to index and now my original (malfunctioning) project works!

Do the development world a favour apple and make your swift compilers open source- so we are not all thwarted by your incompetence.

Agraphia answered 7/4, 2015 at 10:57 Comment(0)
D
3

I've had this issue as well and solved it by removing/changing expressions with the "+" operator.

I changed this:

var mainArray = arrayOne + arrayTwo + arrayThree + arrayFour + arrayFive

To this:

var mainArray = arrayOne
mainArray += arrayTwo
mainArray += arrayThree
mainArray += arrayFour
mainArray += arrayFive

It solved the problem.

My machine is a maxed out MBP late 2013

Dissepiment answered 12/11, 2014 at 19:9 Comment(1)
Exactly my experience. It hangs with perfectly fine Swift code, and the only way to fix it is to change the code.Babin
T
3

I am using Xcode Version 7.3 (7D175)

I think I might have figured out an underlying problem. There where two instances where I got stuck in the indexing phase:

  • I created a closure that I assigned to a variable and omitted the type signature. I think xcode had issues with that type inference step. If I remember correctly one of the arguments was a CGPoint, which has an overloaded constructor. My hypothesis is that there where too many possibilities of what my closure might accept as arguments.

  • I refactored a factory method such that instead of returning instances of one type, it could return instances of many types with a common base class. It appears that wherever I used the factory method, I had to cast the resulting object to a specific type (either with as? or by assigning it to a variable that accepts a specific type) Again the type inference step seems to be broken.

It seems like the same is going on with the dictionary declarations mentioned by earlier individuals. I filed a bug report with apple.

Thomasinethomason answered 2/4, 2016 at 17:5 Comment(0)
E
3

I had expressions like:

let x = (value as? Int) ?? someDefault

also

guard let x = (dateFormatter().string(from: Date()) + msg + "\n").addingPercentEncoding(...) else { ... }

So the point is to rewrite your file to contain only kindergarten level expressions and the indexing problem will go away.

Esme answered 13/2, 2020 at 13:37 Comment(1)
I believe this is exactly what people are talking about when people complain about Xcode being slow while writing Swift code. Many "solutions" are assuming some kind of bug, misconfiguration, or cache issue, but the actual answer is that the Swift analyzer is just REALLY slow. The only workaround is to write code that's very simple and easy for the analyzer. This means writing more type annotations than necessary and breaking up compound statements/expressions. Unfortunately, I don't think following this advice will make a huge difference, generally. Still worth it, IMO.Bezonian
P
2

I experienced this same issue after upgrading to 6.1. Xcode would get stuck compiling or indexing without generating a specific error message.

The issue was finally resolved by breaking some of the longer expressions in the swift files down into multiple shorter expressions. Part of my program combines many different string variables to form a longer string. Attempts to combine them in a single expression and using the addition assignment operator both failed. I was able to make it work by doing something similar to the following (simplified):

var a = "Hello"
var b = " "
var c = "World"
var d = "!"
var partA = a + b
var partB = c + d
var result = partA + partB

I got this idea from receiving the following error many times in the previous Xcode version "Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions."

Hope this helps

Pollute answered 23/10, 2014 at 3:56 Comment(2)
Yes thanks, I actually took this kind of suggestions. And I also shrank the length of one of my files from 1500 lines to about 1200. And of course I created a new project and imported all the files one by one to it. Then finally it gets back on. Not really sure which one is the ultimate saviour.Witenagemot
Splitting up a long expression in Swift did the trick for me.Weightless
L
2

I've struggled with the same problem. I've tried the two solutions mentioned ( deleting derived data and deleting .xcworkspace ) with no success. I also tried slowly commenting out most of the code bit by bit and removing the files until there was almost nothing left and the indexing was still stuck.

I did find a solution that worked for me, I've opened the project with an older Xcode Version 6.1 (6A1030) which had no problem indexing then got back to the latest Xcode Version 6.1 (6A1052d) which I was using before and indexing was fixed and continued to work well.

My conclusion is that this is a bug with Xcode Version 6.1 (6A1052d) which I hope will improve with future releases.

The problem does come back once in a while, the same fix works each time. I guess another solution would be to just stick with the older Xcode Version 6.1 (6A1030) but it won't work with devices running iOS 8.1 and it won't have the latest bug fixes.

Letsou answered 4/11, 2014 at 9:34 Comment(2)
I have tried most of the suggestions around for solving the perpetual indexing issue and this was the only one that worked for me. I didn't have an older Xcode 6 but I had Xcode 5. It would not build, but it did index and after that Xcode 6.1 indexed successfully.Alvin
I forget to mention that this was only a temporary solution and I had to do it again a few hours later.Alvin
P
2

On my Xcode the solution was to close all redundant windows. For some reason many open windows make Xcode very slow.

Pishogue answered 12/1, 2015 at 10:32 Comment(1)
for me this was the solution as well, surprising as it worked well for a long time before with the same number of open windowsLatarsha
B
2

I have tried this with Xcode 8.3.3. Here are my results:

You can write perfectly fine Swift code that will cause indexing to hang.

Once indexing hangs, it hangs. Changing the Swift code back to something that wouldn't cause indexing to hang doesn't help, it still hangs.

Closing the project and reopening doesn't help in that situation.

Quitting Xcode and restarting it helps. Indexing will not hang anymore (that is if you changed the code back to something that doesn't make it hang).

Restarting your Mac helps as well, although it isn't needed.

The hanging is caused by perfectly fine Swift code. An example that I had looked like

if let date = function1()
?? function2()
?? function3()
?? function4()
?? function5()
?? function6()
?? function7()
?? function8()
?? function9()
?? function10() {
    return date
}

Indexing would hang. I commented out most of the "??" lines and it was fine (after quitting and relaunching Xcode). Uncommented one line after the other. With some number of lines it was fine, then uncommenting the next line would make it hang.

The only thing that helps apparently is changing your code.

Babin answered 3/7, 2017 at 13:16 Comment(0)
R
2

Xcode 11.5 (11E608c) and still the same issues, 6 years after the original question. I wish i could "mark" apple in this question so they can see this mess. This is a large project( >1000 files) and i was under the clock so when i notice the freezing index i was with more than 100 files changed and can't go back.

I've tried everything:

  1. clear Derived data and build
  2. Restart xcode , restart mac
  3. remove and add source
  4. Searched for dictionaries literal's etc etc

The problem was an array creation:

private var overlayColors: [UIColor] = [UIColor(hex: "#b71c1c"), UIColor(hex: "#4a148c"),
                                        UIColor(hex: "#880e4f"), UIColor(hex: "#1de9b6"),
                                        UIColor(hex: "#f50057"), UIColor(hex: "#311b92"),
                                        UIColor(hex: "#f44336"), UIColor(hex: "#651fff"),
                                        UIColor(hex: "#d500f9"), UIColor(hex: "#3d5afe"),
                                        UIColor(hex: "#bf360c"), UIColor(hex: "#0d47a1"),
                                        UIColor(hex: "#006064"), UIColor(hex: "#2979ff"),
                                        UIColor(hex: "#ff6f00"), UIColor(hex: "#1a237e"),
                                        UIColor(hex: "#795548"), UIColor(hex: "#004d40"),
                                        UIColor(hex: "#00e676"), UIColor(hex: "#01579b"),
                                        UIColor(hex: "#33691e"), UIColor(hex: "#827717"),
                                        UIColor(hex: "#76ff03"), UIColor(hex: "#ffc400"),
                                        UIColor(hex: "#e65100"), UIColor(hex: "#00b0ff"),
                                        UIColor(hex: "#ff3d00"), UIColor(hex: "#616161"),
                                        UIColor(hex: "#263238"), UIColor(hex: "#ff1744")]

What helped me to discover the bad swift file was when xcode freezed indexing i did the following steps

  1. open activity monitor -> "swift" process ->show process info -> open Files and Ports. This will give you a list of what files this process is running drilling down your list of possible bad files
  2. Other handy tool is this script SOURCEKIT_LOGGING=3 /Applications/Xcode.app/Contents/MacOS/Xcode &> ~/Documents/xcode.log this will start Xcode with level 3 verbose and start logging in the log file.
  3. Search in the log file the last entries for your swift files ex: "my_project/Source/App/"

It's not a full solution but it's helpful to drill down and know where to look.

open activity monitor -> "swift" process ->show process info -> open Files and Ports.

Recruit answered 11/7, 2020 at 11:39 Comment(0)
W
1

Finally, I "solved" the issue, though it is just a workaround.

I created another project and added files one by one to it. Then I spotted a "very long" viewcontroller.swift file. Then I broke its codes into modules and made those repeatedly used codes into functions in another swift file. And also, I took the suggestion online that long expression should be broken into shorter ones. Then, the indexing works and the compiling works.

So for now, I have it "solved".

BUT, I don't think this is right. Xcode IDE should be more than capable of handling my "very long" swift file, only 1500 lines. I believe this is definitely a bug (existing for a long time), although Xcode 6.1 is already an upgrade from Xcode 6.0.1.

Witenagemot answered 23/10, 2014 at 12:45 Comment(0)
E
1

For me, I deleted the Xcode app and downloaded it again and install it. That solved the issue, at least over now.

Eleanoreleanora answered 12/11, 2014 at 23:55 Comment(0)
D
1

Xcode indexing usually for your code for suggestions and auto complete among other things like helping you in story boards and vice versa. But to make faster your xcode project you can turn it off/on via terminal

Turn off indexing
defaults write com.apple.dt.XCode IDEIndexDisable 1 Turn on indexing defaults write com.apple.dt.XCode IDEIndexDisable 0

But Better approach to use a speedy mac with good RAM.

Dutch answered 17/11, 2016 at 5:40 Comment(2)
Turning off indexing severely cripples Xcode and should not be done. Telling somebody to purchase a new Mac should never be the answer, a 2 year old laptop should be able to handle the latest Xcode.Bromism
macOS 10.13 Beta 7 introduced something that crashes both Xcode 9 Beta 6 and Xcode 8 as soon as the indexer runs. Turning it off was the only way to continue using Xcode, even with a crippled version.Tortilla
B
0

The Xcode 6.2 beta resolved the issue for me. Not lightning-fast, but at least it isn't indexing forever. The beta does not install over the top of your regular Xcode installation, so if you don't like the beta, you can just delete it.

Various Xcode downloads including the beta >

Badtempered answered 26/11, 2014 at 16:44 Comment(0)
L
0

You may wish to update to Xcode 6.1.1

It has been officially released and resolved for us the indexing problem. In the update description, it says that they have applied stability fixes so it is very likely that it will behave in a more stable fashion.

Latria answered 3/12, 2014 at 14:18 Comment(2)
Interesting. For me, 6.1.1 introduced the endless indexing problem, with a project that was opening fine in 6.1. signPatience
I see. In our case we also simplified expressions and reduced class sizes using extension classes. Which is pretty much ridiculous that we had to do it but it is what we had to do. And it seems to be doing the trick for now. Also by commenting out lines we narrowed what piece of the code actually is "overly complex" as per what Swift compiler thinks and reduced complexity as much as possible. Hope they are going to fix this soon as it is pretty annoying.Latria
G
0

If you don't mind reverting back to 6.0.1 until they figure it out, that's what worked for me. I was having the same issue with both 6.1 and 6.1.1. Now I'm good. I'll try 6.2 when it comes out.

You can find previous versions of apple software on their official dev site, here: https://developer.apple.com/downloads/index.action

If you do this, make sure to delete your current copy of Xcode first.

Gmur answered 12/12, 2014 at 17:25 Comment(0)
T
0

I am using Xcode 6.1.1 with swift files on the same exact MacBook Pro.

As I kept adding rows into a 3D string array, Xcode all of sudden became unusable and now I can't do anything.

Will try to revert to 6.1 and hopefully the problem will go away.

Typal answered 7/1, 2015 at 20:58 Comment(2)
I have the same issue. After adding an array of dictionaries xcode became unusable ... Did you find solution?Sappington
I went back to Xcode 6.1 which worked better. It didn't get stuck on indexing forever, but sometimes i would type and get the beach ball for 4 seconds. Overall I was able to code enough to get stuff done. I didn't change any of my code. Though on Xcode 6.1 the battery of macbook pro didnt seem too affected, but on my macbook air mid 2013 running 6.1.1, the battery life is absolutely shot. It goes from ~12 hours to 2 hours remain once i start Xcode.Typal
D
0

I am seeing this in Xcode 6.3.2. I had really hoped that a year after release, they would have the compiler working, but alas.

If none of the above solutions work for, try checking your code for syntactic errors. In the process of refactoring, I extracted a closure but forgot to qualify the parameters:

    let hangsInsteadOfError = { l, r in
        return l.nameFirst < r.nameFirst
        || l.nameFirst == r.nameFirst && l.nameLast < r.nameLast }

    let fixingErrorAvoidsHang = { (l:User, r:User) -> Bool in
        return l.nameFirst < r.nameFirst
            || l.nameFirst == r.nameFirst && l.nameLast < r.nameLast }

If I have learned anything from working in Swift, it is to work incrementally, to avoid having to backtrack too much to find the offending code.

Declare answered 1/6, 2015 at 18:35 Comment(0)
L
0
  1. Is your indexing status an "Indicator circle" or "Progress bar"?
  2. If it is an "Indicator circle", that means it already stuck at the beginning.
  3. Open and check with your other projects, if they are all the same, that means it's a system issue.
  4. Just restart your computer and everything will be fine.
Leverrier answered 1/10, 2016 at 2:8 Comment(0)
A
0

I use Xcode 8.2 and also ended in this problem. It started after I defined a complex tuple variable -- an array of tuple with subarray of tuple. Things get really slow when the subarray of tuple has a property that is programmatically calculated.

As some other answers noted, the indexing takes forever, and I believe it is trying to infer the types the variable.

I solved the problem first by clearly define the variable with types included. When updating the property, I calculate it first then assign it to the tuple, instead of calculating in defining the variable.

Here is an example code.

var sectionTuples: [(section: String, rows: [(name: String, subtitle: String)])] = []
let subtitle1: String = "" // something calculated dynamically
let subtitle2: String = "" // something calculated dynamically
sectionTuples = [(
section: "Section 1", rows: [
(name: "name1", subtitle: subtitle1),
(name: "name2", subtitle: subtitle2)
])]

The bottom line is that don't let Xcode infer complex structures.

Apraxia answered 8/2, 2017 at 22:35 Comment(0)
P
0

I was having the same issue. My Xcode is 8.2.1. But in my case, I wanted to create an array of dictionary with 33 key-value pairs. I was doing in the following way which was stuck in indexing:

var parameter = [String : AnyObject]()
var finalArray = [parameter]

for item in listArray
{
    parameter = ["A": item.a as AnyObject, "B": item.b as AnyObject, "C": item.c as AnyObject, ... , "Z": item.z as AnyObject]

    finalArray.append(parameter)
}

Following worked for me:

var parameter = [String: AnyObject]()
var finalArray = [parameter]

for item in listArray
{
    parameter["A"] = listArray.a as AnyObject
    parameter["B"] = listArray.b as AnyObject
    parameter["C"] = listArray.c as AnyObject
    parameter["D"] = listArray.d as AnyObject 
    .
    .
    .
    parameter["Z"] = listArray.z as AnyObject 
    finalArray.append(parameter)
}
Pooh answered 17/3, 2017 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.