2017-06-03 85 views
0

我有一個pfquery tableview控制器,在我的pfquerytableviewcontroller中正在加載的類中有許多pfobjects。我有問題。當我刷新我的表格視圖時,在調試器中出現此錯誤「警告:正在主線程上執行長時間運行的操作。 斷開warnBlockingOperationOnMainThread()以進行調試。」當我嘗試在桌面視圖中滑動時,桌面視圖會非常緩慢地滑動並滑動。我認爲這是因爲數據正在主線程中加載而發生的。這是我的代碼。有人能告訴我我能做些什麼來解決這兩個問題。非常感激。謝謝主線程pfquerytableviewcontroller

class ProductTableViewController: PFQueryTableViewController{ 
override func queryForTable() -> PFQuery<PFObject> { 
    var query : PFQuery<PFObject>! 


     query = PFQuery(className: "Products") 
     query.includeKey("User") 
     query.limit = 25 
     query.skip = 25 

       query.order(byAscending: "createdAt") 

} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    let nib = UINib(nibName: "ProductTableViewCell", bundle: nil) 
    tableView.register(nib, forCellReuseIdentifier: reuseIdentifier) 
} 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, object: PFObject?) -> PFTableViewCell? { 
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! ProductTableViewCell 

    if let files : [PFFile] = object?["Images"] as? [PFFile] { 
     cell.ProductImage.file = files.first 
     cell.ProductImage.loadInBackground() 
    } 

    } 
    } 
+0

我曾經涉足Android。當開發人員將重要任務放在UI線程中時,移動操作系統框架會討厭它。在Android中,我們實現AsynchTask在另一個線程上工作。讓你免於掛斷用戶界面。我不知道這是什麼混雜的Swift版本。 –

+0

我迷失在我應該做的事情上。我不認爲解析會爲pfquerytableview控制器創建框架,並肯定會在主線程上出現操作和崩潰。有一種解決方案我還不知道。 – ahmed

+0

試着看http://docs.parseplatform.org/ios/guide/ 我剛剛做了,他們在本指南中提到了PFQuery中提到的後臺查詢功能。 –

回答

0

爲什麼不使用普通的tableViewController?

var show = 0 
let maxload = 250 
var objects = [PFObject]() 

func loaddata() { 
    self.objects.removeAll(keepingCapacity: false) 
    self.show = 25 

    let query = PFQuery(className:"Ledger") 
    query.limit = self.maxload 
    query.findObjectsInBackground { 
     (objects: [PFObject]?, error: Error?) -> Void in 
     if error == nil { 
      if let objects = objects as [PFObject]! { 
       for object in objects { 
        self.objects.append(object) 
       } 
       //reload TableView 
       self.tableView.reloadData() 
       print("retrieved \(self.objects.count)") 

      } 
     } 
    } 
} 

@IBAction func refreshButton(_ sender: AnyObject) { 
    loaddata() 
    self.tableView.reloadData() 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    if self.objects.count <= 0 { 
     return 0 
    } else { 
     if show > self.objects.count { 
      return self.objects.count 
     } else if show > maxload { 
      return maxload 
     } else { 
      return show 
     } 
    } 
} 


func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    if (indexPath as NSIndexPath).section == self.show - 1 { 
     self.show += 25 
     print("showing more data") 
    } 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewController 

    if self.objects.count - 1 > (indexPath as NSIndexPath).section { 
     let objects = self.objects[(indexPath as NSIndexPath).section] 
    } 

    return cell 
}