swift - Why is retrieving data from JSON taking a lot of time? -


it takes @ least 15-35 second iphone go through these loops. learned json on stackoverflow , way people this. these arrays have 3 elements inside text , 1 small image per element

if let parsejson = json{     let succes = parsejson["data"]     let item = self.success["catalogue_products"] as! [[string: anyobject]]  if item.isempty == false{     in item {         var categoryname = i["category_name"] as! string         if self.category == nil{             self.category = categoryname             self.categories.append(self.category)             self.categorycount = 1         }          if self.category != categoryname{             self.categorycount += 1             self.category = categoryname             self.categories.append(self.category)         }          var deep = i["products"] as! [[string: anyobject]]         in deep{             var product = productcatalogue()             product.categoryname = categoryname             product.id =  i["id"]             println(product.id)             product.name = i["name"]             product.imageurl = i["image"]             product.value = i["value"]             var volumes = (i["volumes"] as? [anyobject])!              var check = true             in volumes{                 if check == true {                     product.volumemin =                     check = false                 } else {                     product.volumemax =                     check = true                 }             }             product.colors = i["colors"] as! [[string: anyobject]]             in product.colors{                 let temp: anyobject? = i["code"]                 product.colorcode.append(temp!)                 let url2: anyobject? = i["image"]                 product.colorimageurl.append(url2!)             }              let url = nsurl(string: string(stringinterpolationsegment: product.imageurl))             let data = nsdata(contentsofurl: url!)             product.image = uiimage(data: data!)              if product.colorimageurl.isempty == false {                 in 0...(product.colorimageurl.count - 1) {                     let url1 = nsurl(string: string(stringinterpolationsegment: product.colorimageurl[i]))                     let data1 = nsdata(contentsofurl: url1!)                      switch {                     case 0: product.color1 = uiimage(data: data1!)                     case 1: product.color2 = uiimage(data: data1!)                     case 2: product.color3 = uiimage(data: data1!)                     case 3: product.color4 = uiimage(data: data1!)                     case 4: product.color5 = uiimage(data: data1!)                     case 5: product.color6 = uiimage(data: data1!)                     case 6: product.color7 = uiimage(data: data1!)                     default: println("")                 }             }         }         self.array.append(product)     } }  self.sortinout() self.loadscreen() 

its because making many http requests. first in 1 loop not idea @ , second time in nested loop, in idea bad mistake. http requests not efficient , fast when dealing them this.

if willing bunch of data, make json in rest api , once.

if don't have rest api , you're getting images web, make efficient possible making fewest requests possible. also, try getting images asynchronously, calling gcd async function.

dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{      // download image }); 

hope helps


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -