aiQG's Blog

Happy hacking. Happy developing.

aiQG's GitHub ⛓Links P
Other CTF Swift Developing&Reversing

Swift 学习笔记(八)

24 February 2019

by aiQG_

ios swift 多线程(队列) //这篇笔记是看了这篇文章后的总结



//课堂草稿⬇️

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
//=======================================================
	
	
	
	override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
		

        //simpleQueues()
        
        //queuesWithQoS()
        
		
//         concurrentQueues()
//		//用于激活.initiallyInactive 不活动的队列
//         if let queue = inactiveQueue {
//            queue.activate()
//         }
		
        
		//queueWithDelay()
        
        //fetchImage()
        
        //useWorkItem()
    }
    
    
    
    func simpleQueues() {
		let queue = DispatchQueue(label: "com.aiqg")
		queue.async {
			for i in 10..<20 {
				print("\(i)")
			}
		}
		
		//主队列
		for i in 0..<10 {
			print("\(i)")
		}
    }
    
    
    func queuesWithQoS() {
		//user为高优先级 ///主线程最高
		let queue0 = DispatchQueue(label: "com.tryQoS0", qos:DispatchQoS.background)//低优先级
		let queue1 = DispatchQueue(label: "com.tryQoS1", qos:DispatchQoS.utility)
		queue0.async {
			for i in 0..<10 {
				print("🔵\(i)")
			}
		}
		queue1.async {
			for i in 10..<20 {
				print("🔴\(i)")
			}
			
		}
		for i in 1000..<1010 {
			print("Ⓜ️", i)
		}
    }
    
    //并行队列
    var inactiveQueue: DispatchQueue! //不活动的队列
    func concurrentQueues() {
        //let anotherQueue = DispatchQueue(label: "com.another", qos: .utility)
		//let anotherQueue = DispatchQueue(label: "com.another", qos: .utility, attributes: .concurrent)
		let anotherQueue = DispatchQueue(label: "com.another", qos: .utility, attributes: [.initiallyInactive, .concurrent])//不活动+并行
		inactiveQueue = anotherQueue
		anotherQueue.async {
			for i in 0..<10 {
				print("⚪️\(i)")
			}
		}
		anotherQueue.async {
			for i in 100..<110 {
				print("🔴\(i)")
			}
		}
		anotherQueue.async {
			for i in 1000..<1010 {
				print("🔵\(i)")
			}
		}
    }
    
    
    func queueWithDelay() {
		let delayQueue = DispatchQueue(label: "com.delay", qos: .userInitiated)
		print(Date())
		let additionalTime: DispatchTimeInterval = .seconds(2)
		//启动延迟队列
		delayQueue.asyncAfter(deadline: .now() + additionalTime) {
			print(Date())
		}
    }
    
    
    func fetchImage() {
//		let globalQueue = DispatchQueue.global(qos: .background) //全局队列
//		globalQueue.async {
//			for i in 0..<10 {
//				print("🔴\(i)")
//			}
//		}
		
		let imageURL: URL = URL(string: "http://www.appcoda.com/wp-content/uploads/2015/12/blog-logo-dark-400.png")!
		(URLSession(configuration: URLSessionConfiguration.default)).dataTask(with: imageURL, completionHandler: {(imageData, response, error) in
			if let data = imageData {
				print("完成")
				DispatchQueue.main.async {
					self.imageView.image = UIImage(data: data) //主队列中更新UI
				}
			}
		}).resume()
    }
	
    
    func useWorkItem() {
		var value = 10
		let workItem = DispatchWorkItem {
			value += 5
		}
		//在全局队列中运行
		workItem.perform() //在主队列上运行 =15
		let queue = DispatchQueue.global(qos: .utility)
		queue.async(execute: workItem) //在全局队列运行 =20
		workItem.notify(queue: DispatchQueue.main) {
			print("value = \(value)")
		}
    }
}


Swift

return

email:nrxxmzlrovqw4z3fgeztcncaozuxaltroexgg33n

桂ICP备18011144号-1 (点击访问安全的工信部网站)