由于从解析查询

时间:2017-05-04 07:37:16

标签: ios parse-platform swift3 uicollectionview

我正在开发一个Parse as Backend的iOS应用程序。 我有这个查询从服务器检索一些数据并填充集合视图

let getAncmntsQuery = PFQuery(className: "Announcement")
    getAncmntsQuery.whereKey("categories", equalTo: self.category.objectId!)
    getAncmntsQuery.includeKey("user")
    getAncmntsQuery.order(byDescending: "startDate")
    if let currentUser = PFUser.current() {
        getAncmntsQuery.whereKey("user", notEqualTo: currentUser)
    }
    getAncmntsQuery.findObjectsInBackground { (result, error) in
        if error == nil{
            self.collectionView.backgroundView = nil
            self.ancmnts = result as? [Announcement]
            self.indicator.stopAnimating()
            self.collectionView.reloadData()
       }
   }

这是cellForItemAt

cell.phoneNumber = ancmnts[indexPath.row].phoneNumber
    cell.title = ancmnts[indexPath.row].title
    cell.price = "\(ancmnts[indexPath.row].price)"
    cell.objectId = ancmnts[indexPath.row].objectId
    cell.ancmntImage.contentMode = .scaleAspectFill
    cell.videoMark.isHidden = true
    cell.ancmntTitle.text = ancmnts[indexPath.row].title
    cell.ancmntPrice.text = "\(ancmnts[indexPath.row].price) TND"
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    let dateString = dateFormatter.string(from: ancmnts[indexPath.row].startDate as Date)
    cell.ancmntDate.text = dateString
    if !ancmnts[indexPath.row].videoUrl.isEmpty {
        cell.ancmntImage.sd_setImage(with: URL(string: ancmnts[indexPath.row].thumbnailUrl), placeholderImage: UIImage(named:"nophoto"), options: SDWebImageOptions.progressiveDownload, completed: { (image, error, cacheType, url) in
            if image != nil {
                cell.ancmntImage.image = imageByScalingAndCroppingForSize(originalImage: image!, size: cell.ancmntImage.frame.size)
                cell.videoMark.isHidden = false
            }
        })

    }else if ancmnts[indexPath.row].picturesUrl.count > 0 {

        cell.ancmntImage.sd_setImage(with: URL(string: ancmnts[indexPath.row].picturesUrl[0]), placeholderImage: UIImage(named:"nophoto"), options: SDWebImageOptions.progressiveDownload, completed: { (image, error, cacheType, url) in
            cell.ancmntImage.image = imageByScalingAndCroppingForSize(originalImage: image!, size: cell.ancmntImage.frame.size)
        })
    }else{
        cell.ancmntImage.image = UIImage(named: "nophoto")
        cell.ancmntImage.contentMode = .scaleAspectFit
        cell.videoMark.isHidden = true
    }
    if self.ancmnts?[indexPath.row].user["avatarUrl"] != nil {
        cell.ownerPhoto.sd_setImage(with: URL(string: self.ancmnts?[indexPath.row].user["avatarUrl"] as! String), placeholderImage: UIImage(named:"avatar"), options: SDWebImageOptions.progressiveDownload, completed: { (image, error, cacheType, url) in
            cell.ownerPhoto.image = imageByScalingAndCroppingForSize(originalImage: image!, size: cell.ownerPhoto.frame.size)
        })
    }else{
        cell.ownerPhoto.image = UIImage(named: "avatar")
    }

    cell.ownerName.text = self.ancmnts?[indexPath.row].user["username"] as? String

问题是当我在与查询中包含的“user”相关的cellForItemAt中注释代码部分时,集合视图的滚动滞后

getAncmntsQuery.includeKey("user")

uicollectionview的滚动返回到正常状态,因此包含的“user”阻止了滚动,我正在尝试遍历解析查询的“result”数组并准备另一个包含所有用户的PFUser数组在“结果”但仍然存在同样的问题。我的问题是如何解决这个问题?这是否与主线程相关,我阻止它,如果是这样,解决方案是什么?在cellForItemAt中使用后台线程?

0 个答案:

没有答案