正在主线程上执行长时间运行操作

时间:2016-06-01 08:36:16

标签: ios swift event-dispatch-thread parse-server

我已经看过几次这个问题,但仍未找到解决方案。 我使用解析托管的sdk @ back4app.com

收到此错误:

  

警告:正在执行长时间运行的操作   线。打破warnBlockingOperationOnMainThread()进行调试。

在我的home.swift文件中,代码为:

func queryPosts(text:String) {
    showHUD()

    let query = PFQuery(className: POSTS_CLASS_NAME)

    if text != "" {
        let keywords = text.componentsSeparatedByString(" ") as [String]
        query.whereKey(POSTS_QUOTE, containsString: "\(keywords[0])")
    }

    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error:NSError?)-> Void in
        if error == nil {
            self.postsArray = objects!
            self.postsTableView.reloadData()
            self.hideHUD()

        } else {
            self.simpleAlert("\(error!.localizedDescription)")
            self.hideHUD()
    }}
}

-

数据加载,应用程序没有崩溃,但错误让我疯狂。我也意识到当我有时滚动一两秒时会有一点延迟。如果有人能提供帮助,那就太棒了,谢谢。

1 个答案:

答案 0 :(得分:1)

根据我们的讨论,您可以使用给定的方法更改您的方法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! PostCell

    // Resize quote label
    cell.quoteView.frame = CGRectMake(30, 30, view.frame.size.width - 30*2, view.frame.size.width - 30*2)


    let postRecord = postsArray[indexPath.row]

    // Get User Pointer
    let userPointer = postRecord[POSTS_USER_POINTER] as! PFUser

    userPointer.fetchIfNeededInBackgroundWithBlock { (result, error) -> Void in
        cell.avatarImage.image = UIImage(named: "logo")
        let imageFile = userPointer[USER_AVATAR] as? PFFile
        imageFile?.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
            if error == nil {
                if let imageData = imageData {
                    cell.avatarImage.image = UIImage(data:imageData)
                }}})
        cell.avatarImage.layer.cornerRadius = cell.avatarImage.bounds.size.width/2


        cell.usernameLabel.text = "\(userPointer[USER_FULLNAME]!)"


        cell.quoteLabel.text = "\(postRecord[POSTS_QUOTE]!)"
        let quoteColor = postRecord[POSTS_COLOR] as! Int
        cell.backgroundColor = colors[quoteColor]
        cell.quoteView.backgroundColor = colors[quoteColor]
    }


    // Assign tags to buttons in the cell
    cell.likeOutlet.tag = indexPath.row
    cell.reportOutlet.tag = indexPath.row
    cell.shareOutlet.tag = indexPath.row
    cell.avatarOutlet.tag = indexPath.row
    cell.commentOutlet.tag = indexPath.row

    return cell
    }