【发布时间】:2016-12-19 17:49:45
【问题描述】:
我使用this 指南让我的应用程序使用 NSOperationQueue 和 NSOperations。但是我的代码仍然在主线程上运行,不管:S?我必须说我是 NSOperation 的新手,我认为这是我错过的一件小事。
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 Preloader[1807:31357] 主线程?是的。
这是怎么回事?感谢您的帮助;
【问题讨论】:
-
您可能已经从here 获得了
NSOperation子类,但我已经对其进行了更新以同步executing和finishedgetter 和setter。请参阅 Multicore Considerations 讨论,其中讨论了同步这些的重要性。 -
另外,由于您使用的是
responseJSON,因此无需致电NSJSONSerialization。这已经为你完成了。 (这就是我们使用responseJSON的原因,所以我们不必担心自己解析它。)只需使用response.result.value即可。
标签: swift multithreading nsoperation nsoperationqueue