2016-06-01 168 views
1

我已經看過這個問題發佈了幾次,但還沒有找到解決方案。 我使用解析託管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

你需要做的主隊列外的裝載和當它完成更新從主隊列中的UI。 – DMH

+0

你的問題是什麼?你想知道如何在後臺線程上運行任務嗎? – Feroz

+0

@feroz對不起,我的問題是真的阻止我的錯誤的任何方式,我在這裏讀取PFObject更改爲AnyObject,並沒有工作,所以是啊我怎麼能在後臺線程上運行它?我可以試試這個。 – OnlyDanWilliams

回答

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 
    }