我的函数快速返回并解析了空数组 [英] My function returns empty array in swift and parse

查看:62
本文介绍了我的函数快速返回并解析了空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,可以从我的解析服务器中快速获取数据,一切正常,并且数据可以很好地读取.但是,当我尝试返回数组时,它会返回一个空数组.我还在获取后台数据"中添加了打印件.那里的阵列已经满了.所以问题不在于数据.

HI there I am writing a function to get data from my parse server in swift everything is working alright and the data is getting read well. but when I try to return the array it returns me an empty array. I also added a print in the "Get data in background" and there, the array was getting full.So the problem is not getting the data.

public func getthearray() -> Array<UIImage>
{
    let user = PFUser.current()
    let array = user?["Photos"] as! Array<PFFileObject>
    var imagearray = [UIImage]()

    for x in array
    {
        x.getDataInBackground
        { (dataa, error) in

            var img = UIImage(data: dataa!)
            imagearray.append(img!)
        }
    }

    return imagearray
}

推荐答案

该函数返回空数组的原因是 x.getDataInBackground 完成中的代码被异步调用,并且在此之前任何图像都将添加到数组中,派系将已经返回.

The reason the function returns an empty array is the fact that the code in the x.getDataInBackground completion gets called asynchronously, and before any image will get added to the array, the faction will already return.

以下是解决方案(2个版本),即您的函数经过了重构,并使用完成处理程序和Grand Central Dispatch进行了重写

Here is the solution (in 2 versions), which is your function slightly refactored and rewritten with completion handler and Grand Central Dispatch

public func getTheArray(_ completion: @escaping ([UIImage]) -> Void) {
    let user: PFUser = .current()
    let array = user?["Photos"] as? [PFFileObject] ?? []
    var result = [UIImage]()

    let semaphore = DispatchSemaphore(value: 0)
    // dispatch to a separate thread which we can safely occupy and block
    // while waiting for the results from async calls.
    DispatchQueue.global().async {
        for file in array {
            file.getDataInBackground { (data, error) in
                if let data = data, let image = UIImage(data: data) {
                    result.append(image)
                }
                // the result have arrived, signal that we are ready
                // to move on to another x
                semaphore.signal()
            }

            // wait until the `signal()` is called, thread is blocked
            semaphore.wait()
        }
        // dispatch to main queue where we are allowed to perform UI updates
        DispatchQueue.main.async {
            // call the completion, but we are doing it only once,
            // when the function is finished with its work
            completion(result)
        }
    }
}

方法2.派遣组

public func getTheArray(_ completion: @escaping ([UIImage]) -> Void) {
    let user: PFUser = .current()
    let array = user?["Photos"] as? [PFFileObject] ?? []
    var result = [UIImage]()

    let group = DispatchGroup()
    for file in array {
        group.enter()
        file.getDataInBackground { (data, error) in
            if let data = data, let image = UIImage(data: data) {
                result.append(image)
            }
            group.leave()
        }
    }

    // notify once all task finish with `leave()` call.
    group.notify(queue: .main) {
        // call the completion, but we are doing it only once,
        // when the function is finished with its work
        completion(result)
    }
}

用法

您会这样称呼它

getTheArray { result in
    // do what you want with result which is your array of UIImages
}

文档:

相关博客文章:

  • Grand Central Dispatch by John Sundell
  • A deep dive into Grand Central Dispatch in Swift by John Sundell

这篇关于我的函数快速返回并解析了空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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