multithreading - How do I know when multiple threads finished (Swift) -
i use parse fetch rows , each row contains pffile. want store fetched data model, in kinda way:
class mymodel { var name: string var image: uiimage init(dataobject: pfobject) { self.name=dataobject["name"] as! string let imagefile = dataobject["image"] as! pffile imagefile.getdatainbackgroundwithblock() { (imagedata: nsdata?,error: nserror?) -> void in self.image=uiimage(imagedata) } } } func fetchdata() { var mycollection = array<mymodel>() parsequery.findobjectsinbackgroundwithblock { (objects: [anyobject]?,error: nserror) -> void in if let parseobjects = objects as? [pfobject] { parseobject in parseobjects { mycollection.append(mymodel(parseobject)) } } } println("fetching finished , data loaded") } well 'fetching finished' correct , data still not loaded because of image fetching, how can tell in case if fetching , mycollection object loaded?
you can use nsoperation + nsoperationqueue, this:
import foundation let queue = nsoperationqueue() queue.maxconcurrentoperationcount = 1 _ in 0..<10 { var op = nsblockoperation( block: { println("do something") } ) queue.addoperation( op ) } queue.suspended = false queue.waituntilalloperationsarefinished() println("done!")
Comments
Post a Comment