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

时间:2016-03-02 06:41:17

标签: swift parse-platform

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

我当前的代码也给了我这个错误:打破warnBlockingOperationOnMainThread()来调试。 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 个答案:

答案 0 :(得分:1)

尝试添加检查以确保在向单元格分配数据时阵列不会为空。