从Parse sdk检索的Swift图像 - 崩溃 [英] Swift Image retrieving from Parse sdk - getting crashed

查看:122
本文介绍了从Parse sdk检索的Swift图像 - 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Parse中检索一组图像,使用以下代码使用Swift。

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


}

My Parse Class detail

My Parse Class detail

class name - 图片

class name - Images

当我运行这个函数时,它在控制台中没有消息就崩溃了。

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() 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()进行调试。

Warning: A long-running operation is being executed on the main thread. Break on warnBlockingOperationOnMainThread() to debug.

所以,我还原为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图像 - 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆