iOS開發:Swift多線程NSOperation的使用


介紹:

  NSOperation是基於GCD實現,封裝了一些更為簡單實用的功能,因為GCD的線程生命周期是自動管理,所以NSOperation也是自動管理。NSOperation配合NSOperationQueue也可以實現多線程。

 

實現步驟

  第1步:將一個操作封裝到NSOperation對象中

  第2步:將NSOperation對象放入NSOperationQueue隊列

  第3步:NSOperationQueue自動取出隊列中的NSOperation對象放到一條線程中執行

 

具體實現

  在swift中的實現方式分2種(oc還多了一個NSInvocationOperation,並且在oc中NSOperation是個抽象類):

  1.NSBlockOperation

  2.自定義子類繼承NSOperation

 

目錄:

  1. NSOperation常用操作
  2. NSOperation操作依賴
  3. NSOperation操作監聽
  4. NSOperation線程通信
  5. 注意

 

1.NSOoperation常用操作,創建隊列,設置最大並發數。

//創建隊列
        let queue = NSOperationQueue()
        //設置最大並發數
        queue.maxConcurrentOperationCount=2
        
        //創建operation
        let operation = NSBlockOperation { () -> Void in
            print("doSomething1 \(NSThread.currentThread())")
        }
        
        //當operation有多個任務的時候會自動分配多個線程並發執行,
        //如果只有一個任務,會自動在主線程同步執行
        //operation.start()
        
        operation.addExecutionBlock { () -> Void in
            print("doSomething2 \(NSThread.currentThread())")
        }
        
        operation.addExecutionBlock { () -> Void in
            print("doSomething3 \(NSThread.currentThread())")
        }
        
        let operation2=NSBlockOperation { () -> Void in
            print("doSomething4 \(NSThread.currentThread())")
        }
        
        //添加到隊列中的operation將自動異步執行
        queue.addOperation(operation)
        queue.addOperation(operation2)
        
        //還有一種方式,直接將operation的blcok直接加入到隊列
        queue.addOperationWithBlock { () -> Void in
            print("doSomething5 block \(NSThread.currentThread())")
        }
        queue.addOperationWithBlock { () -> Void in
            print("doSomething6 block \(NSThread.currentThread())")
        }
        queue.addOperationWithBlock { () -> Void in
            print("doSomething7 block \(NSThread.currentThread())")
        }
        queue.addOperationWithBlock { () -> Void in
            print("doSomething8 block \(NSThread.currentThread())")
        } 

 

2.NSOperation操作依賴,可設置一個操作在另一個操作完成后在執行

 //創建隊列
        let queue = NSOperationQueue()
        
        let operationA = NSBlockOperation { () -> Void in
            print("print A")
        }
        let operationB = NSBlockOperation { () -> Void in
            print("print B")
        }
        let operationC = NSBlockOperation { () -> Void in
            print("print C")
        }
        
        //B等A執行完才執行
        operationB.addDependency(operationA)
        //C等B執行完才執行
        operationC.addDependency(operationB)
        
        
        queue.addOperation(operationA)
        queue.addOperation(operationB)
        queue.addOperation(operationC)

 

 

3.NSOperation操作監聽,一個操作完成后調用另一個操作:

   func operationCompletion(){
        //創建隊列
        let queue = NSOperationQueue() 
        let operation = NSBlockOperation { () -> Void in
            print("print A")
        } 
        operation.completionBlock = doSomething
        queue.addOperation(operation)
    }
    func doSomething(){
        print("doSomething")
    }

 

4.NSOperation線程通信,NSOperationQueue.mainQueue。

    //創建隊列
        let queue = NSOperationQueue()
        queue.addOperationWithBlock { () -> Void in
            print("子線程  \(NSThread.currentThread())")
            NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                print("主線程  \(NSThread.currentThread())")
            })
        }

 

注意

  1.在使用隊列任務的時候,內存警告的時候可使用隊列的cancelAllOperations函數取消所有操作,注意一旦取消不可恢復。亦可設置隊列的suspended屬性暫停和恢復隊列。

  2.在設置操作依賴的時候不能設置循環依賴。

 

完!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM