Swift 在主线程上运行的操作
我已使用指南使我的应用程序使用NSOperationQueue和NSOperations。但是我的代码仍然在主线程上运行:S?我必须说我是新来的,我想这是我错过的一件小事Swift 在主线程上运行的操作,swift,multithreading,nsoperation,nsoperationqueue,Swift,Multithreading,Nsoperation,Nsoperationqueue,我已使用指南使我的应用程序使用NSOperationQueue和NSOperations。但是我的代码仍然在主线程上运行:S?我必须说我是新来的,我想这是我错过的一件小事 class CountryFetcher: NSObject{ var operation: NSOperation? var alamoFireQueue: NSOperationQueue{ let val = NSOperationQueue() val.maxConcur
class CountryFetcher: NSObject{
var operation: NSOperation?
var alamoFireQueue: NSOperationQueue{
let val = NSOperationQueue()
val.maxConcurrentOperationCount = 10
val.name = "Alamofire Downloading Queue"
return val
}
func getCountries(){
operation = CountryProcessor(URLString: (BASE_URL + "countries/"+CAT_STAMPS))
alamoFireQueue.addOperation(operation!)
}
}
class CountryProcessor : ConcurrentOperation {
let URLString: String
weak var request: Alamofire.Request?
init(URLString: String) {
self.URLString = URLString
super.init()
}
override func main() {
request = Alamofire.request(.GET, URLString).responseJSON { response in
if let dataResponse = response.data{
var test: NSArray?
do{
test = try NSJSONSerialization.JSONObjectWithData(dataResponse, options: NSJSONReadingOptions()) as! NSArray
}catch let error as NSError{
print(error.localizedDescription)
}
for _ in 1...100{
NSLog("main thread? %@", NSThread.isMainThread() ? "YES" : "NO");
}
}
self.completeOperation()
}
}
override func cancel() {
request?.cancel()
super.cancel()
}
}
这是ConcurrentOperation类。我从上面链接的帖子中复制了它
class ConcurrentOperation : NSOperation {
override var asynchronous: Bool {
return true
}
override var concurrent: Bool{
return true
}
private var _executing: Bool = false
override var executing: Bool {
get {
return _executing
}
set {
if (_executing != newValue) {
self.willChangeValueForKey("isExecuting")
_executing = newValue
self.didChangeValueForKey("isExecuting")
}
}
}
private var _finished: Bool = false;
override var finished: Bool {
get {
return _finished
}
set {
if (_finished != newValue) {
self.willChangeValueForKey("isFinished")
_finished = newValue
self.didChangeValueForKey("isFinished")
}
}
}
/// Complete the operation
///
/// This will result in the appropriate KVN of isFinished and isExecuting
func completeOperation() {
executing = false
finished = true
}
override func start() {
if (cancelled) {
finished = true
return
}
executing = true
main()
}
}
当我执行这段代码时,它一直说它在主线程上运行:2016-08-12 18:25:45.799 Stamp Catalague Preload[1807:31357]主线程?对
这是怎么发生的?谢谢你的帮助 问题在于Alamofire将其完成处理程序分派到主队列(理论上,它们希望让您更容易更新UI和/或模型) 如果要为完成处理程序使用不同的队列,请将队列参数提供给
response
或responseJSON
方法
例如,具有某些属性:
var completionHandlerQueue = dispatch_queue_create("com.domain.completionhandler", nil)
然后,当您执行请求时:
Alamofire.request(.GET, URLString).responseJSON(queue: completionHandlerQueue) { response in
guard let test = response.result.value else {
print("no data")
print(response.result.error)
return
}
// do something with `test` here
print(test)
NSLog("main thread? %@", NSThread.isMainThread() ? "YES" : "NO");
}
这就提出了一个问题:您是否真的关心是否在主队列上调用完成处理程序。当然,如果你正在做一些计算密集型的事情,那么,无论如何,指定一个不同的队列。但是操作没有理由不能在主队列上执行某些任务。事实上,有时只在主队列上运行完成处理程序非常有用,这样您就不需要同步模型更新和/或将UI更新发送到主队列
为了更进一步,我们必须询问使用
NSOperation
的目的是什么。如果目标仅仅是享受异步处理请求的乐趣,那么就不需要操作。但是如果(a)您正在使用NSOperation
管理不同操作的依赖关系;(b)您没有使用后台会话,那么,无论如何,请将您的请求包装到操作中。您可能从中获得了NSOperation
子类,但我已对其进行了更新,以同步执行的和完成的getter和setter。请参阅有关同步这些的重要性的讨论。此外,由于您使用的是responseJSON
,因此无需调用NSJSONSerialization
。这已经为你做了。(这就是为什么我们使用responseJSON
,所以我们不必担心自己解析它。)只需使用response.result.value
。