[英]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.