PFQueryTableViewController

PFQueryTableViewController

我有一个pfquery tableview控制器,该类内部有许多pfobject,这些对象正在pfquerytableviewcontroller中加载。我有问题。刷新表视图时,在调试器中出现此错误“警告:主线程上正在执行长时间运行的操作。
 断开warnBlockingOperationOnMainThread()以进行调试。有人可以告诉我如何解决这两个问题。非常感谢。谢谢

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

   }
  }

最佳答案

为什么不使用普通的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
}

关于ios - 主线程pfquerytableviewcontroller,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44339788/

10-09 00:24