ios - How to query an PFFile By Creation Date -
i using code retrieve images parse data...
if let userpicture = object.valueforkey("image") as? pffile { userpicture.getdatainbackgroundwithblock({ (imagedata: nsdata?, error: nserror?) -> void in if (error == nil) { let image = uiimage(data:imagedata!) self.imagearray.insert(image!, atindex: 0) } else { self.alert("error: \(error!) \(error!.userinfo!)", message: "make sure have secure internet connection") } dispatch_async(dispatch_get_main_queue()) { println("finished pictures") } }) }
and using code retrieve string parse:
var stuffarray = [string]() var query = pfquery(classname:"classname") query.findobjectsinbackgroundwithblock { (objects: [anyobject]?, error: nserror?) -> void in if error == nil { // find succeeded. println("successfully retrieved \(objects!.count) scores.") // found objects if let objects = objects as? [pfobject] { object in objects { stuffarray.append(object.valueforkey("column")! as! string) } } else { // log details of failure println("error: \(error!) \(error!.userinfo!)") } dispatch_async(dispatch_get_main_queue()) { self.alert("\(stuffarray)", message: "") } } }
i know how query second example adding following code:
query.orderbyascending("createdat")
my question is, how query first example(image) same way did in second example? tried using following code, error:
userpicture.orderbyascending("createdat")
i tried using following code, text being returned correctly, images still returned in random order...
var query = pfquery(classname:"featured") query.orderbydescending("createdat") query.findobjectsinbackgroundwithblock { (objects: [anyobject]?, error: nserror?) -> void in if error == nil { // find succeeded. println("successfully retrieved \(objects!.count) items.") // found objects if let objects = objects as? [pfobject] { object in objects { self.namearray.insert(object.valueforkey("text")! as! string, atindex: 0) self.itemnamearray.insert(object.valueforkey("itemname")! as! string, atindex: 0) let userimagefile = object["image"] as! pffile userimagefile.getdatainbackgroundwithblock { (imagedata: nsdata?, error: nserror?) -> void in if error == nil { if let imagedata = imagedata { let image = uiimage(data:imagedata) self.imagearray.insert(image!, atindex: 0) } } dispatch_async(dispatch_get_main_queue()) { self.loading.hidden = true self.collectionview.reloaddata() } } } } } else { // log details of failure println("error: \(error!) \(error!.userinfo!)") } }
in response to, "i changed 'append', , arrays images still in random order. 2 other string items work perfectly. why images. please give answer in swift.":
the reason why images still in random order because downloading asynchronously server. pffile getdatainbackgroundwithblock
runs in background , smaller files may complete faster larger files may have started earlier.
you should not downloading images until ready display them in uicollectionview or uitableview. when render cell, call pffile getdatainbackgroundwithblock
, then.
Comments
Post a Comment