Search code examples
iosswift2ios9alamofireswifty-json

Swift 2.0 + Alamofire 3.0 - slow loading of JSONs


I have a project for showing healthy recipes. That project returning JSON object from Alamofire to Arraylist and then to Table Cell in Tableview. When I am loading all the objects to tableview it loads ...well slow. 8+ seconds. But my target is 3 seconds. I know the problem is around alamofire cause I check print(timestamp). Anything i am doing wrong?

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)
            }

    }
}
}

Solution

  • So ... The answer is simple, the data I wanted to load was too much and I did (even the unused data). The ideal way to solve this is to only GET what you need and then become very specific.

    EXAMPLE:** I have a Recipe Book. I want to get all the recipes and watch them. SOLUTION: so get only ID and name of all recipes and store them in an array, then of you need specific recipe to be shown, write an Alamofire method to get that specific recipe data and call it with the ID of that recipe parameter.