解析getDataInBackgroundWithBlock没有按顺序获取

时间:2015-02-21 16:37:34

标签: ios swift parse-platform pfquery

我从Parse类中获取字符串,NSDate和PFFile以填充集合视图单元格

所有单元格都正确加载图像,日期,信息。信息和日期是正确排序的(按升序日期)。但是我不时会在构建一些图像时将它们放在不同的单元格中。我真的为此感到头疼。我猜测它与我如何调用mixPhoto.getDataInBackgroundWithBlock({

我确实尝试过使用dispatch_async(dispatch_get_main_queue())

仍然没有运气......继承我的代码,有人有任何想法吗?

@IBOutlet weak var collectionView1: UICollectionView!


var mixPhotoArray : Array<UIImage> = []
var mixInfoArray: Array <String> = []
var mixDateArray: Array <NSDate> = []

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView1.delegate = self;
    collectionView1.dataSource = self;

    self.queryParseMethod()
    self.getImageData()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Do any additional setup after loading the view.
}


func getImageData() {
    var query = PFQuery(className: "musicMixes")
    query.orderByAscending("date")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
    for object in objects {


    let mixPhoto = object["mixPhoto"] as PFFile

        mixPhoto.getDataInBackgroundWithBlock({
            (imageData: NSData!, error: NSError!) -> Void in
            if (error == nil) {
                dispatch_async(dispatch_get_main_queue()) {
                    let image = UIImage(data:imageData)
                    //image object implementation
                    self.mixPhotoArray.append(image!)
                    println(self.mixPhotoArray[0])
                    self.collectionView1.reloadData()

                }
            }
            else {
                println("error!!")
            }

        })//getDataInBackgroundWithBlock - end

        }



    }//for - end

}

    func queryParseMethod() {

    var query = PFQuery(className: "musicMixes")
    query.orderByAscending("date")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {
            for object in objects {


    let mixPhoto = object["mixPhoto"] as PFFile




        let mixInfo = object["info"] as String
        let dateForText = object["date"] as NSDate


        //self.collectionView1.reloadData()

            self.mixDateArray.append(dateForText)
            self.mixInfoArray.append(mixInfo)
            self.collectionView1.reloadData()


                }//for - end
        }
        }
} // end of queryParseMethod


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()




        }



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

// MARK: UICollectionViewDataSource

 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 1
}


 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
     println("I have \(mixPhotoArray.count) Images")
    return mixInfoArray.count


}

  //func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell:StreamCollectionViewCell = collectionView1.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as StreamCollectionViewCell

   cell.mixImage.image = mixPhotoArray[indexPath.item]
    cell.infoLabel.text = mixInfoArray[indexPath.item]

    // NSDate array into cell
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
   cell.mixDateLabel.text = dateFormatter.stringFromDate(mixDateArray[indexPath.item])






     return cell
}

2 个答案:

答案 0 :(得分:3)

就像Wain所说,我认为主要的问题是,由于您的图像以不同的速度下载,因此它们不一定按顺序附加到您的阵列。虽然不是建议您使用字典,但我仍然建议在使用数组时避开该问题:

// Declare your mixPhotoArray such that it can store optionals
var mixPhotoArray : Array<UIImage?> = []

func getImageData() {

    var query = PFQuery(className: "musicMixes")
    query.orderByAscending("date")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
        // Initialize your array to contain all nil objects as
        // placeholders for your images
        self.mixPhotoArray = [UIImage?](count: objects.count, repeatedValue: nil)
        for i in 0...objects.count - 1 {

            let object: AnyObject = objects[i]
            let mixPhoto = object["mixPhoto"] as PFFile

            mixPhoto.getDataInBackgroundWithBlock({
                (imageData: NSData!, error: NSError!) -> Void in
                if (error == nil) {
                    dispatch_async(dispatch_get_main_queue()) {
                        let image = UIImage(data:imageData)
                        // Replace the image with its nil placeholder
                        // and do so using the loop's current index
                        self.mixPhotoArray[i] = image
                        println(self.mixPhotoArray[i])
                        self.collectionView1.reloadData()
                    }
                }
                else {
                    println("error!!")
                }

            })

        }

    }

}

然后在collectionView:cellForItemAtIndexPath范围内,您可以有条件地设置图像,使其只有在准备好后才会显示:

if mixPhotoArray[indexPath.item] != nil {
    cell.mixImage.image = mixPhotoArray[indexPath.item]
}

答案 1 :(得分:1)

您将数据存储在2个数组mixPhotoArraymixInfoArray中,但您无法保证它们的数据顺序相同。图像大小不同,因此可以以不同的速度下载。您也不应该一次尝试下载超过4个,因此您当前的方案并不是很好。

相反,您应该拥有一个字典或自定义类的数组,其中包含所有详细信息,并在下载每个图像时使用该图像进行更新。

显然,这意味着您需要知道哪一个与您刚刚下载的图像相关联,因此您需要在块中捕获此词典/实例,以便您可以更新它。

你可以按原样在2个数组中完成,只要你捕获一个索引,其中图像应该是在正确的位置将图像插入数组。