gpt4 book ai didi

ios - swift 3 : DispatchQueue Timing over URLSession

转载 作者:行者123 更新时间:2023-11-28 15:42:56 25 4
gpt4 key购买 nike

我正在尝试创建一个用于将图像上传到我的服务器的进度指示器!我的目标是让包含 UIProgressViewUILabelUIView 显示正在上传的照片的当前状态。

每次我的代码向我的服务器发送图像时,它应该循环到下一张图片,同时更新 UILabel(引用 lblImagesRemaining)和 UIProgressView(引用 progressImages)。

我能够让 UIView(给定引用 viewUploadBox)出现并稍微更新进度,但我似乎无法将其发送给 完全工作。 viewUploadBox 将出现,但 lblImagesRemainingprogressImages 都不会更新,直到上传过程完成约 70%。不过,一旦它确实达到了 ~70% 的完成度,它就会在每次发送图像时进行更新。

这是触发 Upload Test 按钮之前我的 UIViewController: enter image description here

这是我的 UIViewController 一旦首次触发 Upload Test 按钮: enter image description here

上传过程完成约 70% 后,这是我的 UIViewController: enter image description here

这是我的代码:

@IBAction func uploadTest(_ sender: Any) {
let imageCount = collectImages() // Collect images also prepares the tests with dictionaries for images to upload.
DispatchQueue.main.async {
self.viewUploadBox.isHidden = false
self.lblImagesRemaining.text = "Images Remaining: " + String(imageCount)
self.view.alpha = 0.5
self.viewUploadBox.alpha = 1.5
}
let time = DispatchTime.now() + 0.01
DispatchQueue.main.asyncAfter(deadline: time) { // The asyncAfter was used to ensure that the viewUploadBox appeared.
self.uploadTest(imageCount: imageCount)
}
}


func uploadTest(imageCount: Int) {
let progressFactor = 1.0 / Double(imageCount)
var i = 1
var imagesRemaining = imageCount
while i < 8 {
let imageDictionary = projectHandler.testHandler.getImageDictionary(i)
let test = imageDictionary["test"] as! String
for (key, value) in imageDictionary {
if key == "test" || key == "count" {
continue
}
let url = NSURL(string: "http://IMAGEUPLOADLOCATION.PHP")
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let boundary = generateBoundaryString()
//define the multipart request type
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let image_data = UIImageJPEGRepresentation((value as! UIImage), 0.2)
let body = NSMutableData()
let constructedName = userdata.valueForKey("name_full") + "(ID-" + userdata.valueForKey("id_ACCOUNT") + ")"
let parameters = [
"name" : constructedName,
"project" : projectHandler.projectName,
"picturefile" : key + ".jpeg",
"testname" : test,
"projectid" : projectHandler.idPROJECT,
"accountid" : userdata.valueForKey("id_ACCOUNT"),
"brazerid" : projectHandler.idBrazer]
//define the data post parameter
for (key, value) in parameters {
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(value)\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
}
let fname = "image"
let mimetype = "image/jpeg"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname); \"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = body as Data
let session = URLSession.shared
session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
} else {
do {
let dataString = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
print("Response: \(response!)")
print("Data: \(data!)")
print(dataString)
} catch {
print(error)
}
}
DispatchQueue.main.async {
self.progressImages.progress += Float(progressFactor)
imagesRemaining -= 1
self.lblImagesRemaining.text = "Images Remaining: " + String(imagesRemaining)
if imagesRemaining == 0 {
self.viewUploadBox.isHidden = true
self.btnEmail.isEnabled = true
self.view.alpha = 1.0
}
}
}).resume()
}
i += 1
}
}

最佳答案

这似乎是一个竞争条件。尝试下一个片段。

import PlaygroundSupport
import Dispatch
PlaygroundPage.current.needsIndefiniteExecution = true

let q = DispatchQueue(label: "test") // serial queue

var j = 0

DispatchQueue.concurrentPerform(iterations: 10) { i in
q.async {
j += 1
DispatchQueue.main.async {
print(i, j)
}
}
}

print("total",j)

检查代码,试着想象代码将打印出什么,并将结果与​​您的预期进行比较:-)

它打印

total 6
0 10
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10

这是你期望的吗?

让我们尝试一些更高级的东西:-)

import PlaygroundSupport
import Dispatch
PlaygroundPage.current.needsIndefiniteExecution = true

let q = DispatchQueue(label: "test") // serial queue

class S {
let q = DispatchQueue(label: "sync", attributes: .concurrent)
private var v: Int = 0
func add(i: Int) {
q.async(flags: .barrier) {
self.v += i
}
}
var i: Int {
get {
return q.sync {
return v
}
}
}
}

let s = S()

DispatchQueue.concurrentPerform(iterations: 10) { i in
q.async {
s.add(i: 1)
let k = s.i
DispatchQueue.main.async {
print(i, s.i, k)
}
}
}

看起来好多了,但再次检查结果。

0 4 1
1 4 2
2 4 3
3 4 4
4 6 5
5 6 6
6 8 7
7 8 8
8 10 9
9 10 10

查看第二和第三“列”中的值之间的差异。第二个看起来像你的进度条,第三个是你想要的。

嗯,但是我们使用从并发调度的串行队列...让我们删除它

import Dispatch
PlaygroundPage.current.needsIndefiniteExecution = true


class S {
let q = DispatchQueue(label: "sync", attributes: .concurrent)
private var v: Int = 0
func add(i: Int) {
q.async(flags: .barrier) {
self.v += i
}
}
var i: Int {
get {
return q.sync {
return v
}
}
}
}

let s = S()

DispatchQueue.concurrentPerform(iterations: 10) { i in
s.add(i: 1)
let k = s.i
DispatchQueue.main.async {
print(i, s.i, k)
}
}

我们又回到了起点......

1 10 4
0 10 4
3 10 4
2 10 5
5 10 7
4 10 7
6 10 7
7 10 10
8 10 10
9 10 10

解决方案

import PlaygroundSupport
import Dispatch
PlaygroundPage.current.needsIndefiniteExecution = true

class S {
let q = DispatchQueue(label: "sync", attributes: .concurrent)
private var v: Int = 0
func add(i: Int) {
q.async(flags: .barrier) {
self.v += i
let k = self.v
DispatchQueue.main.async {
print("update progress", k)
}
}
}
var i: Int {
get {
return q.sync {
return v
}
}
}
}

let s = S()

DispatchQueue.concurrentPerform(iterations: 10) { i in
s.add(i: 1)
print(i, s.i)
}

打印

0 3
1 5
2 5
3 5
4 7
5 9
8 9
7 9
6 9
9 10
update progress 1
update progress 2
update progress 3
update progress 4
update progress 5
update progress 6
update progress 7
update progress 8
update progress 9
update progress 10

无法保证并发任务的完成顺序,但会正确指示我们的进度。

关于ios - swift 3 : DispatchQueue Timing over URLSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43507202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com