Search code examples
iosswiftevent-dispatch-threadparse-server

Long-Running Operation is being executed on main thread


I've seen this question posted a few times but still haven't found a solution. I'm using a parse hosted sdk @ back4app.com

Receiving this error :

Warning: A long-running operation is being executed on the main thread. Break on warnBlockingOperationOnMainThread() to debug.

In my home.swift file the code is :

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()
    }}
}

-

The data loads, and the app doesn't crash but the error is driving me nuts. I also realised there is a slight lag when I'm scrolling sometimes even for a second or two. If anyone can help that'll be great, thanks.


Solution

  • As per our discussion you can change your method with the given methods :

    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
        }