在iOS应用程序中限制调度\u异步任务时间
我使用dispatch\u async进行一些服务器调用。 有时,由于互联网连接速度较慢,此呼叫需要花费大量时间在iOS应用程序中限制调度\u异步任务时间,ios,swift,grand-central-dispatch,Ios,Swift,Grand Central Dispatch,我使用dispatch\u async进行一些服务器调用。 有时,由于互联网连接速度较慢,此呼叫需要花费大量时间 如何设置以秒为单位的限制?例如:任务还有3秒要完成,否则它将停止。如果您从服务器请求数据,您可能应该使用类似的方法。设置会话时,您可以设置所需的超时。如果您从服务器请求数据,您可能应该使用类似的方法。设置会话时,您可以设置所需的超时。因此,您确实应该为此使用NSURLSession。 let queue = dispatch_queue_create("serialQueue", D
如何设置以秒为单位的限制?例如:任务还有3秒要完成,否则它将停止。如果您从服务器请求数据,您可能应该使用类似的方法。设置会话时,您可以设置所需的超时。如果您从服务器请求数据,您可能应该使用类似的方法。设置会话时,您可以设置所需的超时。因此,您确实应该为此使用
NSURLSession
。
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.doNetworkTask()
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
self.doNetworkTaskWithCompletion({
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
然而,如果你仍然在寻找GCD的答案,这里是
您可以轻松地使用信号量来实现请求的超时。 您只需创建一个初始值为
0
的信号量,然后使用dispatch\u semaphore\u wait
等待网络任务完成时发出信号量,或等待请求超时
因此,如果您的网络任务是同步的,您将希望执行以下操作…
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.doNetworkTask()
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
self.doNetworkTaskWithCompletion({
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
或者如果您的网络任务是异步的…
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.doNetworkTask()
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
self.doNetworkTaskWithCompletion({
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
As,您真的应该为此使用NSURLSession
。
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.doNetworkTask()
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
self.doNetworkTaskWithCompletion({
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
然而,如果你仍然在寻找GCD的答案,这里是
您可以轻松地使用信号量来实现请求的超时。 您只需创建一个初始值为
0
的信号量,然后使用dispatch\u semaphore\u wait
等待网络任务完成时发出信号量,或等待请求超时
因此,如果您的网络任务是同步的,您将希望执行以下操作…
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.doNetworkTask()
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
self.doNetworkTaskWithCompletion({
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
或者如果您的网络任务是异步的…
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
self.doNetworkTask()
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
let queue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL) // your GCD queue
...
dispatch_async(queue, {
let semaphore = dispatch_semaphore_create(0) // create semaphore
self.doNetworkTaskWithCompletion({
dispatch_semaphore_signal(semaphore); // signal semaphore on completion
})
let timeout = dispatch_time(DISPATCH_TIME_NOW, Int64(3.0 * Double(NSEC_PER_SEC))) // 3 second time-out
let returnValue = dispatch_semaphore_wait(semaphore, timeout)
guard returnValue == 0 else { // return value is non-zero if the request timed out
// request timed out, cancel the request here.
self.stopNetworkTask()
return // prevent further execution if network task was unsucessful
}
dispatch_async(dispatch_get_main_queue(), {
// do processing after request success
})
})
那么你基本上是在寻找一个暂停时间吗?如果是这样,请尝试在n/w调用中设置超时。那么您基本上是在寻找超时吗?如果是这样,请尝试在n/w调用中设置超时。