问题描述
我正在使用 Swift 使用以下代码从 Parse 检索图像集.
I'm retrieving set of images from Parse, using the following code using Swift.
var imageResources : Array<UIImage> = []
override func viewDidLoad(){
super.viewDidLoad()
self.loadImages()
}
func loadImages(){
var query = PFQuery(className: "Images")
query.orderByDescending("objectId")
query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
for object : PFObject! in objects as [PFObject] {
var thumbNail = PFFile()
thumbNail = object["image"] as PFFile
println("thumNail (thumbNail)")
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) in
if (error == nil) {
let image : UIImage = UIImage(data:imageData)
//image object implementation
self.imageResources.append(image)
}
})//getDataInBackgroundWithBlock - end
}//for - end
}
else{
println("Error in retrieving (error)")
}
})//findObjectsInBackgroundWithblock - end
}
我的解析类详细信息
类名 - 图片
当我运行这个函数时,它在控制台中没有任何消息的情况下崩溃了.
When I run this function, it's getting crashed without a message in the console.
注意:我可以在回调中获取 PFFile 对象的集合.
Note: I'm able to get the collection of PFFile objects in the callback.
我已经换了"thumbNail.getDataInBackgroundWithBlock({...." 块与同步函数调用thumbNail.getData() 类似"var imageData=thumbNail.getData()var image = UIImage(data:imageData)"
I've replaced "thumbNail.getDataInBackgroundWithBlock({...." block with the synchronous function call thumbNail.getData() like "var imageData= thumbNail.getData() var image = UIImage(data:imageData)"
然后错误说
警告:正在主线程上执行长时间运行的操作.中断warnBlockingOperationOnMainThread() 进行调试.
所以,我回到了 thumbNail.getDataInBackGroundWithBloack({...但是现在,控制台中没有错误显示,就像之前发生的那样.我的方法有什么问题吗,请告诉我.任何帮助将不胜感激...!
So, I reverted to thumbNail.getDataInBackGroundWithBloack({... But now, there is no error display in the console, as it happens before. Is there anything wrong in my approach please let me know.Any help would be appreciated...!
推荐答案
我设法重现了错误,这似乎是 PFObject 上的某种内存泄漏/僵尸.我不确定确切的原因,但以下列方式重构您的代码消除了我的情况下的错误:
I managed to recreate the error, which seems to be some kind of memory leak / zombie on a PFObject. I'm not sure exactly why, but refactoring your code in the following manner got rid of the error in my case:
func loadImages() {
var query = PFQuery(className: "Images")
query.orderByDescending("objectId")
query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
self.getImageData(objects as [PFObject])
}
else{
println("Error in retrieving (error)")
}
})//findObjectsInBackgroundWithblock - end
}
func getImageData(objects: [PFObject]) {
for object in objects {
let thumbNail = object["image"] as PFFile
println(thumbNail)
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
//image object implementation
self.imageResources.append(image)
println(image)
}
})//getDataInBackgroundWithBlock - end
}//for - end
}
顺便说一句,这也有效:
Incidentally, this also works:
func loadImages() {
var query = PFQuery(className: "Images")
query.orderByDescending("objectId")
query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
let imageObjects = objects as [PFObject]
for object in objects {
let thumbNail = object["image"] as PFFile
thumbNail.getDataInBackgroundWithBlock({
(imageData: NSData!, error: NSError!) -> Void in
if (error == nil) {
let image = UIImage(data:imageData)
//image object implementation
self.imageResources.append(image)
println(image)
}
})//getDataInBackgroundWithBlock - end
}//for - end
}
else{
println("Error in retrieving (error)")
}
})//findObjectsInBackgroundWithblock - end
}
这表明错误是由以下行引起的:
This would indicate that the error was due to the following line:
for object : PFObject! in objects as [PFObject] {
将该行重写如下:
for object : PFObject in objects as [PFObject] {
也消除了错误.所以这个错误的原因似乎是你告诉程序解开一些不是可选的东西.
Also removes the error. So the reason for this error seems to be that that you told the program to unwrap something that wasn't an optional.
这篇关于从 Parse sdk 检索 Swift Image - 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!