繁体   English   中英

在TableView中使用parse.com图像时出错

[英]Error when using parse.com image in tableview

我正在运行此代码以基于解析查询创建表视图。 它有效,问题是出现以下错误:

2014-09-24 01:09:32.187 inventario [253:20065]警告:正在主线程上执行长时间运行的Parse操作。 中断warnParseOperationOnMainThread()进行调试。

当使用“ var datta = image?.getData() ”将图像放置到位时,我得到了这个。 有任何想法吗?

{       
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("ModeloEquipoInventarioCell", forIndexPath: indexPath) as UITableViewCell;

            let sweet:PFObject = self.timelineData.objectAtIndex(indexPath.row) as PFObject

            cell.textLabel?.text = sweet.objectForKey("Modelo") as? String
            cell.detailTextLabel?.text = sweet.objectForKey("Marca") as? String

            // This part is the problem
            var image = sweet.objectForKey("Foto3") as? PFFile
            var datta = image?.getData()
            cell.imageView?.image = UIImage(data: datta!)

            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

            return cell
        }

查询的方法是:

{@IBAction func loadData(){

        var findTimelineData:PFQuery = PFQuery(className: "InventarioListado")
        findTimelineData.whereKey("Categoria", equalTo: toPassInventario)
        findTimelineData.whereKey("Descripcion", equalTo: toPassModeloEquipoInventario)
        //findTimelineData.orderByAscending("Descripcion")
        findTimelineData.limit = 500

        findTimelineData.findObjectsInBackgroundWithBlock{
            (objects:[AnyObject]!, error:NSError!)->Void in

            if error == nil{
                for object in objects{
                    let sweet:PFObject = object as PFObject

                    let sweeter:NSString! = sweet.objectForKey("Modelo") as? NSString
                    var filtro = self.categoriasFiltradasDeInventario.containsObject(sweeter!)
                    if (filtro == false) {
                        self.categoriasFiltradasDeInventario.addObject(sweeter)
                        self.timelineData.addObject(sweet)
                    }
                    self.tableView.reloadData()

                }
            }

        }
    }

}

问题在于getData()函数通过网络连接加载数据,并且下载图像可能需要一些时间。 在此期间,您的主线程将被阻止,因此强烈建议在后台运行它。 您可以使用getDataInBackgroundWithBlock()轻松做到这一点。

let image = sweet["Foto3"] as PFFile
image.getDataInBackgroundWithBlock {
    (imageData: NSData!, error: NSError!) -> Void in
    if error == nil {
       cell.imageView?.image = UIImage(data:imageData)
    }
}
  • 编程中有一个基本概念, Run the UI code on UI thread and Non-UI code in Non-UI thread.

  • 运行诸如网络调用,I / O调用等CPU密集型代码应该在Non-UIthread/Background thread.

  • 解析查询以获取图像是要在后台线程上进行的网络调用,以免UI被触及

  • 使用shared NSOperationQueue拍摄查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM