Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 确定urlsession.shared和Json解析何时完成_Ios_Json_Swift_Urlsession - Fatal编程技术网

Ios 确定urlsession.shared和Json解析何时完成

Ios 确定urlsession.shared和Json解析何时完成,ios,json,swift,urlsession,Ios,Json,Swift,Urlsession,我正在下载并读取一个json文件。此json包含文件列表及其在服务器上的地址 一切正常,但我想得到所有文件的大小下载 但是我在设置一个completionblock时遇到了一些麻烦,它会指示所有的事情都完成了 这是代码 jsonAnalysis { self.sum = self.sizeArray.reduce(0, +) print(self.sum) } here func jsonAnalysis(completion: @escapin

我正在下载并读取一个json文件。此json包含文件列表及其在服务器上的地址

一切正常,但我想得到所有文件的大小下载

但是我在设置一个completionblock时遇到了一些麻烦,它会指示所有的事情都完成了

这是代码

   jsonAnalysis {
        self.sum = self.sizeArray.reduce(0, +)
        print(self.sum)
    } here

func jsonAnalysis(completion:  @escaping () -> ()) {


    let urlString = "xxxxxxxxxxxxxxxxxxxxx"
    let url = URL(string: urlString)
    URLSession.shared.dataTask(with:url!) { (data, response, error) in
        if error != nil {
            print("error")

        } else {
            do {

                let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? [String: Any]
                self.i = -1
                guard let array = json?["Document"] as? [Any] else { return }

                for documents in array {

                    self.i = self.i + 1
                    guard let VersionDictionary = documents as? [String: Any] else { return }
                    guard let DocumentName = VersionDictionary["documentname"] as? String else { return }
                    guard let AddressServer = VersionDictionary["addressserver"] as? String else { return }

                    self.resultAddressServer.append(AddressServer)
                    self.addressServer = self.resultAddressServer[self.i]
                    self.resultDocumentName.append(DocumentName)
                    self.documentName = self.resultDocumentName[self.i]

                    let url1 = NSURL(string: AddressServer)
                    self.getDownloadSize(url: url1! as URL, completion: { (size, error) in
                        if error != nil {
                            print("An error occurred when retrieving the download size: \(String(describing: error?.localizedDescription))")
                        } else {
                            self.sizeArray.append(size)
                            print(DocumentName)
                            print("The download size is \(size).")

                        }
                    })

                }

            } catch {
                print("error")
            }

        }
            completion()

        }   .resume()

}

func getDownloadSize(url: URL, completion: @escaping (Int64, Error?) -> Void) {
    let timeoutInterval = 5.0
    var request = URLRequest(url: url,
                             cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
                             timeoutInterval: timeoutInterval)
    request.httpMethod = "HEAD"
    URLSession.shared.dataTask(with: request) { (data, response, error) in
        let contentLength = response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
        completion(contentLength, error)
        }.resume()
}
我希望在完成所有操作后,在最后得到数组的和,此时print(self.sum)正在运行,并显示0


我不熟悉完成情况,我确信我做的每件事都是错的。

您需要
DispatchGroup

在调用内部异步任务
之前,在内部异步任务
的完成块中输入
,离开该组。
最后,当组
通知
时,调用
完成

let group = DispatchGroup()
for documents in array {
    ...

    let url1 = URL(string: AddressServer) // no NSURL !!!
    group.enter()
    self.getDownloadSize(url: url1!, completion: { (size, error) in
         if error != nil {
            print("An error occurred when retrieving the download size: \(String(describing: error?.localizedDescription))")
         } else {
            self.sizeArray.append(size)
            print(DocumentName)
            print("The download size is \(size).")
         }
         group.leave()
     })
}
group.notify(queue: DispatchQueue.main) {
    completion()
}

您需要
DispatchGroup

在调用内部异步任务
之前,在内部异步任务
的完成块中输入
,离开该组。
最后,当组
通知
时,调用
完成

let group = DispatchGroup()
for documents in array {
    ...

    let url1 = URL(string: AddressServer) // no NSURL !!!
    group.enter()
    self.getDownloadSize(url: url1!, completion: { (size, error) in
         if error != nil {
            print("An error occurred when retrieving the download size: \(String(describing: error?.localizedDescription))")
         } else {
            self.sizeArray.append(size)
            print(DocumentName)
            print("The download size is \(size).")
         }
         group.leave()
     })
}
group.notify(queue: DispatchQueue.main) {
    completion()
}

它的工作非常完美,并给我的总尺寸在年底预期谢谢!!您总是可以将一个异步调用包装到另一个调用中,然后在触发时立即使用一个回调,在完成时使用另一个回调。类似于UIAnimation使用两个块的方式。看到它完美的工作,并给我的总尺寸在年底预期谢谢!!您总是可以将一个异步调用包装到另一个调用中,然后在触发时立即使用一个回调,在完成时使用另一个回调。类似于UIAnimation使用两个块的方式。看见