简体   繁体   English

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

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

I am running this code for creating a tableview based on a parse query. 我正在运行此代码以基于解析查询创建表视图。 It works, problem is I get the following error: 它有效,问题是出现以下错误:

2014-09-24 01:09:32.187 inventario[253:20065] Warning: A long-running Parse operation is being executed on the main thread. 2014-09-24 01:09:32.187 inventario [253:20065]警告:正在主线程上执行长时间运行的Parse操作。 Break on warnParseOperationOnMainThread() to debug. 中断warnParseOperationOnMainThread()进行调试。

I get this when using " var datta = image?.getData() " for getting the image in place. 当使用“ var datta = image?.getData() ”将图像放置到位时,我得到了这个。 Any ideas? 有任何想法吗?

{       
        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
        }

The method for the query was: 查询的方法是:

{ @IBAction func loadData(){ {@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()

                }
            }

        }
    }

} }

The problem is that the getData() function loads data via network connection and it can take some time to download an image. 问题在于getData()函数通过网络连接加载数据,并且下载图像可能需要一些时间。 Your main thread would be blocked during this time so it's highly recommended to run it in the background. 在此期间,您的主线程将被阻止,因此强烈建议在后台运行它。 You can use getDataInBackgroundWithBlock() to do that easily. 您可以使用getDataInBackgroundWithBlock()轻松做到这一点。

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

  • Running the CPU intensive code like Network calls, I/O calls etc should be on Non-UIthread/Background thread. 运行诸如网络调用,I / O调用等CPU密集型代码应该在Non-UIthread/Background thread.

  • Parse query to fetch image is a network call that is to be made on a background thread so the UI don't get strucked . 解析查询以获取图像是要在后台线程上进行的网络调用,以免UI被触及

  • Use shared NSOperationQueue to shoot the query. 使用shared NSOperationQueue拍摄查询。

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

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