ios - Loading TableViewDataSource after a method finish -
i want call tableviewdata sources method seeting ui after has been fethced parse . able fetch
func loadimages() { var query = pfquery(classname: "testclass") query.orderbydescending("objectid") query.findobjectsinbackgroundwithblock ({(objects:[anyobject]!, error: nserror!) in if(error == nil){ self.getimagedata(objects [pfobject]) } else{ println("error in retrieving \(error)") } })//findobjectsinbackgroundwithblock - end } func getimagedata(objects: [pfobject]) { object in objects { let thumbnail = object["image"] pffile println(thumbnail) thumbnail.getdatainbackgroundwithblock({ (imagedata: nsdata!, error: nserror!) -> void in if (error == nil) { var imagedic = nsmutablearray() self.image1 = uiimage(data:imagedata) //image object implementation self.imageresources.append(self.image1!) println(self.image1) println(self.imageresources.count) } }, progressblock: {(percentdone: cint )-> void in })//getdatainbackgroundwithblock - end }//for - end self.tableview.reloaddata()
but not able populate these fetched data tableview this
func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { println("in table view") println(self.imageresources.count) return imageresources.count+1; } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell:customtableviewcell = tableview.dequeuereusablecellwithidentifier("customcell") customtableviewcell var (title, image) = items[indexpath.row] cell.loaditem(title: title, image: image) println("message : going upto line") println(self.imageresources.count) var (image1) = imageresources[indexpath.row] cell.loaditem1(image1: image1) return cell }
then on loaditem trying show images , have writen own array populate image array geeting 0 value when populating not able set
any appreciated!
you have several problems, related concurrency - load occurring in background , in parallel.
the first problem use of self.image1
temporary variable in loading process - variable may accessed concurrently multiple threads. should use local variable purpose.
second, appending self.imageresources
multiple threads, swift arrays not thread safe.
third, need call reload on tableview after have finished loading of data, isn't happening because call while background operations still taking place.
finally, getimagedata
function executing on background queue, , must perform ui operations (such reloading table) on main queue.
the simplest option change thumbnail loading synchronous calls - means thumbnails load sequentially , may take bit longer multiple parallel tasks easier manage -
func getimagedata(objects: [pfobject]) { object in objects { let thumbnail = object["image"] pffile println(thumbnail) let imagedata? = thumbnail.getdata if (imagedata != nil) { let image1 = uiimage(data:imagedata!) //image object implementation self.imageresources.append(image1!) println(self.imageresources.count) } }//for - end dispatch_async(dispatch_get_main_queue(), { self.tableview.reloaddata() }) }
a more sophisticated approach use dispatch group , keep parallel image loading. in order need guard access shared array
Comments
Post a Comment