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

介紹:swift

  NSOperation是基於GCD實現,封裝了一些更爲簡單實用的功能,由於GCD的線程生命週期是自動管理,因此NSOperation也是自動管理。NSOperation配合NSOperationQueue也能夠實現多線程。多線程

 

實現步驟併發

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

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

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

 

具體實現線程

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

  1.NSBlockOperation對象

  2.自定義子類繼承NSOperationblog

 

目錄:

  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.在設置操做依賴的時候不能設置循環依賴。

 

完!

相關文章
相關標籤/搜索