Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用解析进行异步处理时出现问题_Ios_Swift_Parse Platform - Fatal编程技术网

Ios 使用解析进行异步处理时出现问题

Ios 使用解析进行异步处理时出现问题,ios,swift,parse-platform,Ios,Swift,Parse Platform,我有一个解析数据库,其中包含由缩略图的PFFile项组成的记录(以及其他项)。数据库是只读的,已使用其他程序成功创建。我确认所有的缩略图都在解析中。当我尝试使用下面所示的函数检索缩略图时,我会在处理所有图像之前收到通知,根据通知后处理的时间,会导致偶尔的故障。如何确保处理所有记录 func convertPFilesToImages () { println("convertPFilesToImages") let notification = NSNotificatio

我有一个解析数据库,其中包含由缩略图的PFFile项组成的记录(以及其他项)。数据库是只读的,已使用其他程序成功创建。我确认所有的缩略图都在解析中。当我尝试使用下面所示的函数检索缩略图时,我会在处理所有图像之前收到通知,根据通知后处理的时间,会导致偶尔的故障。如何确保处理所有记录

    func convertPFilesToImages () {
    println("convertPFilesToImages")
    let notification = NSNotification(name: "imagesLoaded", object: self)
    for i in 0 ..< records.count {
        let userImageFile = records[i].icon
        records[i].image = UIImage()
        println("name: \(self.records[i].name) image: \(self.records[i].image)")
        userImageFile.getDataInBackgroundWithBlock {
            (imageData: NSData?, error: NSError?) -> Void in
            if error == nil {
                //println("no error")
                if let imageData = imageData {
                    let image = UIImage(data:imageData)!

                    self.records[i].image = image
                    // println below shows that not all images are converted
                    println("name: \(self.records[i].name) image: \(self.records[i].image)")
                }
            } else {
                println("ParseProcessing-convertToImage: error = \(error)")
            }
            if (i == self.records.count-1) {
                println("All images processed")
                // loop is done before all images are processed
                NSNotificationCenter.defaultCenter().postNotification(notification)
            }

        }
    }

}
func convertPFilesToImages(){
println(“convertPFilesToImages”)
let notification=NSNotification(名称:“imagesLoaded”,对象:self)
对于0中的i..在中无效
如果错误==nil{
//println(“无错误”)
如果让imageData=imageData{
让image=UIImage(数据:imageData)!
self.records[i].image=image
//下面的println显示并非所有图像都被转换
println(“名称:\(self.records[i].name)图像:\(self.records[i].image)”)
}
}否则{
println(“解析处理转换图像:错误=\(错误)”)
}
if(i==self.records.count-1){
println(“已处理的所有图像”)
//循环在处理所有图像之前完成
NSNotificationCenter.defaultCenter().postNotification(通知)
}
}
}
}
您的for()循环执行异步代码,您的问题在于
getDataInBackground

i==self.records.count-1
时,并不保证所有调用都已完成

getDataInBackround
应转换为
getData
(或者以同步调用命名),整个方法应在后台线程中调用


您还可以使用来同时加载所有内容,然后调用
[queue waitUntilFinished]

谢谢你的及时回复。我错误地认为GetDataInBackgroundithBlock可以帮助我。我将尝试一种在后台线程中使用同步调用的方法。