2

I'm not sure whether this is reproducible...

My swift iOS project is taking a long time to build. Actually, it does not build, the progress bar just stays there for about 15 minutes now:

enter image description here

This is really weird.

This is what I have done before building it:

  • Changed the version and build number
  • Archive project
  • Wrote some code while the archive is being uploaded to app store
  • The Archive has an error after uploading so I cleaned the project
  • Archive again
  • I noticed that I forgot to add a pod to the Linked Binaries section in the project settings so I added it
  • Archived again
  • Uploaded to app store
  • Asked this question (I don't think this matters)

And now, the progress bar is staying there.

What can I do to make it faster?

P.S.

Yesterday it didn't take long to build. I only added a few lines of code today so I don't think the problem it is because of my project is too big. Maybe a bug in Xcode.

Fun fact: the progress bar is still there when I finish writing the question.

UPDATE

After checking out this post, I found out that compilation is always stuck at this file:

class UserSettings {
    static var valueOfPi: Double {
        let π = NSUserDefaults.standardUserDefaults().doubleForKey("valueOfPi")
        return π == 0.0 ? M_PI : π
    }

    static var usePiLiteral: Bool {
        return NSUserDefaults.standardUserDefaults().boolForKey("usePiLiteral")
    }

    enum SigFigOptions{
        case No
        case Yes(Int)

        func correctTo (i: Double) -> Double {
            switch self {
            case .No:
                return i
            case let .Yes(sigFig):
                if i == 0 {
                    return 0
                }
                let d = ceil(log10(i < 0 ? -i : i))
                let power = sigFig - Int(d)
                let magnitude = pow(10.0, Double(power))
                let shifted = round(i * magnitude)
                return shifted / magnitude
            }
        }
    }

    enum AngleMeasures : String {
        case Degrees = "Degrees"
        case Radians = "Radians"
        case Gradians = "Gradians"
    }

    static var sigFigOption: SigFigOptions {
        let userPref = NSUserDefaults.standardUserDefaults().integerForKey("sigFigOption")
        return userPref == 0 ? .No : .Yes(userPref)
    }

    static var angleMeasure: AngleMeasures {
        let measureId = NSUserDefaults.standardUserDefaults().integerForKey("angleMeasure")
        switch measureId {
        case 0:
            return .Radians
        case 1:
            return .Degrees
        case 2:
            return .Gradians
        default:
            return .Radians
        }
    }

    static func convertToPref (angleRadians: Double) -> Double {
        switch angleMeasure {
        case .Radians:
            return angleRadians
        case .Degrees:
            return 180 * angleRadians / UserSettings.valueOfPi
        case .Gradians:
            return 180 * angleRadians / UserSettings.valueOfPi * (10.0 / 9.0)
        }
    }

    static func convertFromPref (prefAngle: Double) -> Double {
        switch angleMeasure {
        case .Radians:
            return prefAngle
        case .Degrees:
            return prefAngle * UserSettings.valueOfPi / 180.0
        case .Gradians:
            return prefAngle / (10 / 9) * UserSettings.valueOfPi / 180.0
        }
    }

    static var pref90Degrees: Double {
        return convertToPref(valueOfPi / 2)
    }

    static var pref180Degrees: Double {
        return pref90Degrees * 2
    }

    static var prefHideWarning: Bool {
        get { return NSUserDefaults.standardUserDefaults().boolForKey("hideWarning") }
        set { NSUserDefaults.standardUserDefaults().setBool(newValue, forKey: "hideWarning") }
    }

    static var aValue: Double {
        get { return NSUserDefaults.standardUserDefaults().doubleForKey("aValue") }
        set { NSUserDefaults.standardUserDefaults().setDouble(newValue, forKey: "aValue") }
    }

    static var bValue: Double {
        get { return NSUserDefaults.standardUserDefaults().doubleForKey("bValue") }
        set { NSUserDefaults.standardUserDefaults().setDouble(newValue, forKey: "bValue")}
    }

    static var cValue: Double {
        get { return NSUserDefaults.standardUserDefaults().doubleForKey("cValue") }
        set { NSUserDefaults.standardUserDefaults().setDouble(newValue, forKey: "cValue")}
    }
}

What piece of code in that file can possibly cause this? I think it's related to NSUserDefaults, right?

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Is there a literal dictionary/array defined somewhere in your code? If this object is kinda big or complex enough to choke the type inference mechanism it could be the cause - in this case the solution is to either declare the object type explicitly or to use an external source for the object (plist or json file, etc). – Eric Aya Jun 03 '16 at 08:27
  • @EricD I have a `[(String, [OneOfMyClasses])]` object stored in my VC. Originally I did have a lot of items in the array but it compiled just fine back then. Today I only added one item into the array, you mean this new item is causing the problem? – Sweeper Jun 03 '16 at 08:33
  • It could... I already encountered this issue many times, for example I had [this](https://github.com/ericdke/PokerHands/blob/master/SwiftyPokerHands/byteRanks/flushes_bytes.json) in the source code and the compiler choked, once moved in a json file it was ok again. – Eric Aya Jun 03 '16 at 08:36
  • @EricD I commented all the items in the array so now the array is empty. Then I cleaned the project and build. However, the progress bar still stays there... :( – Sweeper Jun 03 '16 at 08:52
  • What's the "few lines of code" that you added today? Does the project build fine if you remove them? If any of the lines are particularly complicated (i.e rely heavily on type inference), then that could easily be the source of the problem. A usual suspect is complex functional programming one-liners (i.e a complicated `reduce` operation), or large/complex collections as Eric has said. – Hamish Jun 03 '16 at 08:54
  • Useful related Q&A: http://stackoverflow.com/questions/25537614/why-is-swift-compile-time-so-slow – Eric Aya Jun 03 '16 at 08:54
  • @originaluser2 No I don't think so. I only added some code to loop through a dictionary: `for entry in someDict.enumerate()` and the other code is just a class implementing `CustomStringConvertible`. It shouldn't be too bad, right? – Sweeper Jun 03 '16 at 08:58
  • @EricD I found out the file that the compiler is stuck on. But I don't know exactly where in the file is causing the problem. Maybe it's related to `NSUserDefaults`? – Sweeper Jun 03 '16 at 09:09
  • Without seeing your file contents, I can't guess, no, unfortunately. Usual culprits have been listed by originaluser2 and me and the Q&A I've linked: literal dictionaries/arrays, one-liners with type inference and/or complex reduce operations, ambiguous unwrapping results, etc. Any of those in your file? – Eric Aya Jun 03 '16 at 09:29
  • Why don't you try to encoding your vars into NSData before saving?(NSKeyedArchiver.archivedDataWithRootObject) – Alessandro Ornano Jun 03 '16 at 09:36
  • Guess what? Now the build has succeeded... somehow. I guess I can only solve this by trial and error now... – Sweeper Jun 03 '16 at 09:42

1 Answers1

1

I've tried various techniques, launch many commands but finally I've noticed there are two command lines that actually increased the speed in compiling, maybe they can be useful for you case (you must close and reopen Xcode to makes active these settings):

defaults write com.apple.Xcode PBXNumberOfParallelBuildSubtasks <1|2|4|8|16|..>

(the number of threads Xcode will use to perform tasks defaults to the same number of cores your CPU has -> default value = CPU cores)

defaults write com.apple.Xcode XCMaxNumberOfDistributedTasks <desired-number>

(The user default XCMaxNumberOfDistributedTasks in the com.apple.Xcode domain controls how many concurrent compile jobs Xcode will send to the DNB system at once ; 25 by default)

enter image description here

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133