Reputation: 1334
I am having a strange problem. I am using a 3rd party web API, which is supposed to return a JSON object, that wasn't set up correctly. As a result, I have to reformat the "JSON" in order for Swift to recognize it as a JSON object. On the iOS simulator, the process of reformatting the JSON string takes roughly 2 seconds, but when I test it on my physical iPhone, it takes a whopping 25 seconds. Why is this? How can I fix it (make the JSON formatter faster on the physical device)?
The flawed JSON could be seen here: http://godaven.com/db/davenapi.aspx?zip=48075&results=20. Before running my formatting function, Swift doesn't register it as a JSON object.
Here is the code that I use for reformatting (I found a javascript version, and ported it over to swift):
func formatJSON(json: String) -> String
{
var i = 0, il = 0, tab: Character = "\t", newJson = "", indentLevel: Double = 0, inString = false
var newString = json.stringByReplacingOccurrencesOfString("davenresults = ", withString: "", options: .allZeros, range: nil)
var jsonArray = Array(newString)
for(i = 0; i < jsonArray.count; i++)
{
var currentChar = jsonArray[i]
switch(currentChar)
{
case "{":
if (!inString)
{
newJson += "\(currentChar)\n\(tab * (indentLevel + 1))"
indentLevel++
}
else
{
newJson += String(currentChar)
}
case "[":
if (!inString)
{
newJson += "\(currentChar)\n\(tab * (indentLevel + 1))"
indentLevel++
}
else
{
newJson += String(currentChar)
}
case "}":
if (!inString)
{
indentLevel--
newJson += "\n\(tab * indentLevel)\(currentChar)"
}
else
{
newJson += String(currentChar)
}
case "]":
if (!inString)
{
indentLevel--
newJson += "\n\(tab * indentLevel)\(currentChar)"
}
else
{
newJson += String(currentChar)
}
case ",":
if (!inString)
{
newJson += "\(currentChar)\n\(tab * indentLevel)"
}
else
{
newJson += String(currentChar)
}
case ":":
if (!inString)
{
newJson += ": "
}
else
{
newJson += String(currentChar)
}
case " ":
if (inString)
{
newJson += String(currentChar)
}
case "\n":
if (inString)
{
newJson += String(currentChar)
}
case "\t":
if(inString)
{
newJson += String(currentChar)
}
case "\"":
if (i > 0 && jsonArray[i - 1] != "\\")
{
inString = !inString
}
newJson += String(currentChar)
default:
newJson += String(currentChar)
}
}
return newJson
}
Upvotes: 0
Views: 86
Reputation: 41226
A much easier solution will be to just remove the "davenResults =" at the beginning, that's what makes it invalid JSON, which must contain a single JSON object or array.
json = result.stringByReplacingOccurrencesOfString("davenresults =", withString:"")
All the rest of the function is doing is pretty-printing and indenting the JSON, which has no effect on it's parsability.
The processing you're doing is quite complex, and will function very inefficiently due to repeatedly appending to a string that will ultimately be many k in length.
As for why the processing time is so different between the two, the simulator runs at desktop speeds and with desktop processing power, the real device doesn't. It's one of the reasons it's a "simulator" and not a cycle-accurate "emulator"
Upvotes: 4
Reputation: 64
I think you can't. because the simulator has a higher processing capacity compared to your actual device. so it will take the time to finish the process in the mobile compare to the your mac system.
Upvotes: 2