从 Parse 获取 PFFile 时数组超出索引

Posted

技术标签:

【中文标题】从 Parse 获取 PFFile 时数组超出索引【英文标题】:Array out of index when getting PFFile from Parse 【发布时间】:2016-03-02 06:41:17 【问题描述】:

我的应用程序可以很好地获取图像,但如果我上下滚动一段时间然后拉动刷新,它会崩溃并显示数组超出索引。这并非每次都会发生。我相信这与我没有使用 getdatainbackground 的事实有关,但是当我使用它而不是 getData() 时,它的循环速度比它实际检索文件的速度要快,而且它们是无序的。

我当前的代码也给了我这个错误: Break on warnBlockingOperationOnMainThread() to debug。 2016-03-02 00:35:48.630 应用程序名称 [1137:96655] 警告:正在主线程上执行长时间运行的操作。

if Reachability.isConnectedToNetwork() 
        self.skip = 0
        //--RESETTING THE ARRAYS FOR DATA--\\
        self.contactText.removeAll(keepCapacity: true)
        self.names.removeAll(keepCapacity: true)
        self.images.removeAll(keepCapacity: true)
        self.prices.removeAll(keepCapacity: true)
        self.sizes.removeAll(keepCapacity: true)
        self.conditions.removeAll(keepCapacity: true)
        self.dates.removeAll(keepCapacity: true)
        self.ids.removeAll(keepCapacity: true)
        self.createdBy.removeAll(keepCapacity: true)


        //--RESET THE USERS LOCATION WHEN HE REFRESHES IN CASE OF A DARASTIC MOVE IN LOCATION--\\
        let userGeoPoint = PFUser.currentUser()!["location"] as! PFGeoPoint

        //--GETTING ALL OF THE OBJECTS WITHIN 60 MILES OF USERS CURRENT LOCATION--\\
        let query = PFQuery(className:"Shoes")
        query.whereKey("Location", nearGeoPoint: userGeoPoint, withinMiles: 60)
        let user = PFUser.currentUser() as PFUser!
        let array: AnyObject? = user["blockedUsers"]
        if(array !=  nil)
            query.whereKey("createdBy", notContainedIn: array! as! [AnyObject])
        
        query.limit = 50
        query.orderByDescending("createdAt")
        query.skip = self.skip

        query.findObjectsInBackgroundWithBlock 
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil 
                print("Successfully retrieved \(objects!.count) scores.")
                for object in objects! 

                    if let dateCreated = object.createdAt as NSDate? 
                        self.dates.append(dateCreated)
                    

                    self.contactText.append(object["Contact"] as! String)
                    self.descriptions.append(object["Description"] as! String)
                    self.names.append(object["Name"] as! String)


                    if object["price"] as! String == "" || object["price"] == nil
                       self.prices.append("Negotiable")
                    else
                        self.prices.append(object["price"] as! String)
                    

                    if object["size"] as! String == "" || object["size"] == nil
                        self.sizes.append("N/A")
                    else
                        self.sizes.append(object["size"] as! String)
                    

                    if object["conditionType"] as! String == "" || object["conditionType"] == nil
                        self.conditions.append("N/A")
                    else
                        self.conditions.append(object["conditionType"] as! String)
                    

                    self.ids.append(object.valueForKey("objectId") as! String)
                    self.createdBy.append(object["createdBy"] as! String)

                    let imageFile = object["imageFile"] as! PFFile
                    let imageData = imageFile.getData()

                    if (imageData != nil) 
                        let image = UIImage(data:imageData!)
                        self.images.append(image!)

                    


                
                dispatch_async(dispatch_get_main_queue(), 
                    self.tableView.reloadData()
                )

            
            else 
                print(error)
            

            self.refresher.endRefreshing()
        

        self.skip+=50

     else 
        print("Internet connection not available")
        self.refresher.endRefreshing()
        let alert = UIAlertView(title: "No Internet connection", message: "Please ensure you are connected to the Internet", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
    

【问题讨论】:

【参考方案1】:

尝试添加检查以确保在将数据分配给单元格时数组不为空。

【讨论】:

以上是关于从 Parse 获取 PFFile 时数组超出索引的主要内容,如果未能解决你的问题,请参考以下文章

ParseServer PFFile 扭曲链接

刷新时数组索引超出范围

线程 1:致命错误:索引超出范围。没有快速从数组中获取值,控制台显示它们不是空数组

如何限制 PFFile 的后台下载 - Parse Framework

使用 Parse 从预定义的 User 类中查询 PFFile

离线 Parse.com 时如何在 localdatastore 中保存图像 PFFile?