Swift 2.0 + Alamofire 3.0 - JSON 加载缓慢
Swift 2.0 + Alamofire 3.0 - slow loading of JSONs
我有一个展示健康食谱的项目。该项目将 JSON 对象从 Alamofire 返回到 Arraylist,然后返回到 Table 视图中的 Table Cell。当我将所有对象加载到 tableview 时,它加载......很慢。 8 秒以上。但我的目标是 3 秒。我知道问题出在 alamofire 附近,因为我检查了 print(timestamp)。我做错了什么吗?
func getCookData(urlString: String , completionHandler: (String, String, String, String, String, String, String, String, String, String, String, String, String, String, String) -> ()) -> (){
Alamofire.request(.GET, urlString).responseJSON() {
response in
// var cookArray:[CookData] = []
if response.result.isSuccess {
let data = response.result.value
let cookJson = JSON(data!)
for (var i = 0; i < cookJson["data"].count; i++){
let category = cookJson["data"][i]["kategorie"].stringValue
let nameOfRecipe = cookJson["data"][i]["nazev"].stringValue
let preparationTime = cookJson["data"][i]["priprava"].stringValue
let cookingTime = cookJson["data"][i]["vareni"].stringValue
let dificulty = cookJson["data"][i]["obtiznost"].stringValue
let count = cookJson["data"][i]["pocet"].stringValue
var ingredience = String()
for var j = 0; j < cookJson["data"][i]["ingredience"].count; j++ {
let ingredienceX = cookJson["data"][i]["ingredience"][j].stringValue
ingredience = ingredience + "\n" + "- " + ingredienceX
}
let nutriInfo = cookJson["data"][i]["nutricni_informace"].stringValue
let kcal = cookJson["data"][i]["nutricni_informace"]["Kcal"].stringValue
let bilkoviny = cookJson["data"][i]["nutricni_informace"]["Bílkoviny"].stringValue
let sacharidy = cookJson["data"][i]["nutricni_informace"]["Sacharidy"].stringValue
let tukyy = cookJson["data"][i]["nutricni_informace"]["Tuky"].stringValue
let preparationMekanism = cookJson["data"][i]["postup_pripravy"].stringValue
let photo = cookJson["data"][i]["photos"][0].stringValue
let thumb = cookJson["data"][i]["thumb"].stringValue
self.printTimestamp()
completionHandler(category, nameOfRecipe, preparationTime, cookingTime, dificulty, count, ingredience, nutriInfo, preparationMekanism, photo, thumb, kcal, bilkoviny, sacharidy, tukyy)
}
}
}
}
所以...答案很简单,我想加载的数据太多了,我做了(甚至是未使用的数据)。解决这个问题的理想方法是只获取您需要的内容,然后变得非常具体。
示例:**我有一本食谱书。我想得到所有的食谱并观看它们。
解决方案:因此只获取所有食谱的 ID 和名称并将它们存储在 数组 中,然后如果您需要显示特定的食谱,请编写一个 Alamofire
方法来获取特定的配方数据并使用该配方参数的 ID 调用它。
我有一个展示健康食谱的项目。该项目将 JSON 对象从 Alamofire 返回到 Arraylist,然后返回到 Table 视图中的 Table Cell。当我将所有对象加载到 tableview 时,它加载......很慢。 8 秒以上。但我的目标是 3 秒。我知道问题出在 alamofire 附近,因为我检查了 print(timestamp)。我做错了什么吗?
func getCookData(urlString: String , completionHandler: (String, String, String, String, String, String, String, String, String, String, String, String, String, String, String) -> ()) -> (){
Alamofire.request(.GET, urlString).responseJSON() {
response in
// var cookArray:[CookData] = []
if response.result.isSuccess {
let data = response.result.value
let cookJson = JSON(data!)
for (var i = 0; i < cookJson["data"].count; i++){
let category = cookJson["data"][i]["kategorie"].stringValue
let nameOfRecipe = cookJson["data"][i]["nazev"].stringValue
let preparationTime = cookJson["data"][i]["priprava"].stringValue
let cookingTime = cookJson["data"][i]["vareni"].stringValue
let dificulty = cookJson["data"][i]["obtiznost"].stringValue
let count = cookJson["data"][i]["pocet"].stringValue
var ingredience = String()
for var j = 0; j < cookJson["data"][i]["ingredience"].count; j++ {
let ingredienceX = cookJson["data"][i]["ingredience"][j].stringValue
ingredience = ingredience + "\n" + "- " + ingredienceX
}
let nutriInfo = cookJson["data"][i]["nutricni_informace"].stringValue
let kcal = cookJson["data"][i]["nutricni_informace"]["Kcal"].stringValue
let bilkoviny = cookJson["data"][i]["nutricni_informace"]["Bílkoviny"].stringValue
let sacharidy = cookJson["data"][i]["nutricni_informace"]["Sacharidy"].stringValue
let tukyy = cookJson["data"][i]["nutricni_informace"]["Tuky"].stringValue
let preparationMekanism = cookJson["data"][i]["postup_pripravy"].stringValue
let photo = cookJson["data"][i]["photos"][0].stringValue
let thumb = cookJson["data"][i]["thumb"].stringValue
self.printTimestamp()
completionHandler(category, nameOfRecipe, preparationTime, cookingTime, dificulty, count, ingredience, nutriInfo, preparationMekanism, photo, thumb, kcal, bilkoviny, sacharidy, tukyy)
}
}
}
}
所以...答案很简单,我想加载的数据太多了,我做了(甚至是未使用的数据)。解决这个问题的理想方法是只获取您需要的内容,然后变得非常具体。
示例:**我有一本食谱书。我想得到所有的食谱并观看它们。
解决方案:因此只获取所有食谱的 ID 和名称并将它们存储在 数组 中,然后如果您需要显示特定的食谱,请编写一个 Alamofire
方法来获取特定的配方数据并使用该配方参数的 ID 调用它。