Zac Powell Zac Powell - 11 days ago 4
Swift Question

Code causes Swift indexing to consume masses of memory

I have the below spinnet of code, which when xcode tried to index causes SourceKitService to consume masses of memory and will continue to do so unless the process is killed (hitting over 20GB before I killed it).

let key = ""
let register = "";


var vpn:String?
var password:String?

var taxDueString:String?
var motDueString:String?
var makeString:String?
var dateString:String?
var manufactureString:String?
var ccString:String?
var co2String:String?
var fuelString:String?
var exportString:String?
var statusString:String?
var colourString:String?
var approvalString:String?
var wheelplanString:String?
var revenueString:String?

var parkingIsOn:Bool?
var motIsOn:Bool?
var tollsIsOn:Bool?
var insuranceIsOn:Bool?
var fuelIsOn:Bool?
var partsIsOn:Bool?
var trafficIsOn:Bool?

@IBOutlet weak var firstNameText: UITextField!
@IBOutlet weak var lastNameText: UITextField!
@IBOutlet weak var mobileText: UITextField!
@IBOutlet weak var emailText: UITextField!
@IBOutlet weak var confirmEmailText: UITextField!
...
Just.post(
register,
data: ["key": key, "vrn": vpn, "password": password, "taxDue": taxDueString, "motDue": motDueString, "make": makeString, "date": dateString, "manufacture": manufactureString, "cc": ccString, "co2": co2String, "fuel": fuelString, "export": exportString, "status": statusString, "colour": colourString, "approval": approvalString, "wheelplan": wheelplanString, "revenue": revenueString, "featureParking": parkingIsOn, "featureMot": motIsOn, "featureTolls": tollsIsOn, "featureInsurance": insuranceIsOn, "featureFuel": fuelIsOn, "featureParts": partsIsOn, "featureTraffic": trafficIsOn, "firstname": firstNameText.text, "lastname": lastNameText.text, "mobile": mobileText.text, "email": emailText.text ]
) { r in
if r.ok {
let json = JSON(data: r.content!);
let success = json["success"].intValue

print(json)

if (success == 1){
let defaults = UserDefaults.standard
defaults.setValue(true, forKey: "logged")
defaults.setValue(json["ID"].intValue, forKey: "ID")
defaults.synchronize()

DispatchQueue.main.async { [unowned self] in
self.performSegue(withIdentifier: "registerToDash", sender: nil)
}

}else{
DispatchQueue.main.async { [unowned self] in
self.showError(error: json["error"].stringValue)
}
return
}

}
}


I can see that its the data line that is causing the issue because when I comment it out everything index's fine.

data: ["key": key, "vrn": vpn, "password": password, "taxDue": taxDueString, "motDue": motDueString, "make": makeString, "date": dateString, "manufacture": manufactureString, "cc": ccString, "co2": co2String, "fuel": fuelString, "export": exportString, "status": statusString, "colour": colourString, "approval": approvalString, "wheelplan": wheelplanString, "revenue": revenueString, "featureParking": parkingIsOn, "featureMot": motIsOn, "featureTolls": tollsIsOn, "featureInsurance": insuranceIsOn, "featureFuel": fuelIsOn, "featureParts": partsIsOn, "featureTraffic": trafficIsOn, "firstname": firstNameText.text, "lastname": lastNameText.text, "mobile": mobileText.text, "email": emailText.text ]


I can't seem to see any issue with the above code but something within it is causing the indexing to crash/act strangly

Answer

My guess is it's having trouble inferring the type of such a large array literal. Try defining it to a typed constant and then passing it into the function.

Comments