【问题标题】:main thread pfquerytableviewcontroller主线程 pfquerytableviewcontroller
【发布时间】:2017-11-04 12:08:47
【问题描述】:

我有一个 pfquery tableview 控制器,它在我的 pfquerytableviewcontroller 中加载的类中有许多 pfobjects。我有问题。当我刷新表视图时,我在调试器中收到此错误“警告:正在主线程上执行长时间运行的操作。 中断 warnBlockingOperationOnMainThread() 以进行调试。”。当我尝试在我的 tableview 中滑动时,tableview 滞后并且滑动非常缓慢。我认为这是因为数据正在主线程上加载。这是我的代码。谁能告诉我我能做些什么来解决这两个问题。非常感谢。谢谢

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

   }
  }

【问题讨论】:

  • 我曾经涉足Android。当开发人员将重要任务放在 UI 线程中时,移动操作系统框架讨厌它。在 Android 中,我们实现 AsynchTask 以在另一个线程上工作。防止您挂断 UI。不过,我不知道这是什么混蛋 Swift 版本。
  • 我不知道该做什么。我不认为 parse 会为 pfquerytableviewcontroller 创建框架,并且肯定总会有一个操作并在主线程上崩溃。有一个我还不知道的解决方案。
  • 试试看docs.parseplatform.org/ios/guide 我刚刚做了,他们有本指南中提到的 PFQuery 的后台查询功能。

标签: ios swift xcode parse-platform swift3


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多