Ways to achieve multithreading and benefits:
1,Nsthread
Lightweight is the lightest, but requires itself to manage the thread's lifecycle and thread synchronization. Thread synchronization locks the data to a certain amount of system consumption.
2.Cocoa nsopertion (nsopertion and Nsopertionquene)
Cocoa Nsopertion does not need to care about thread management and data synchronization. The related classes are nsopertion and Nsopertionquene. Startup Nsopertion is an abstract class that uses its subclasses to implement it or the two subclasses it defines: Nsinvocationopertion and nsblockoperation. Create an object of the Nsopertion subclass and add the object to the Nsopertionquene queue to execute
3.Grand Central Dispatch (GCD)
GCD is a multi-core programming solution developed by Apple. The Mac OS x10.6 Snow Leopard was first launched and then introduced into the ios4.0. GCD is an efficient and powerful technology to replace Nsthread, Nsoperationqueue, nsinvocationoperation and other technologies. It is the most abstract and simple to use, and is the most recommended for Apple.
---well, the crap is finally done.
1. Nsthread Example
1.1 via Integrated Nsthread
main.swift// swift_lession1//// Created by shaoyongyang on 15/5/24.// copyright (c) 2015 year shaoyongyang. All rights reserved.//import foundationclass bigdog:nsthread { func hello () { // Nsthread.detachnewthreadselector ("Otherthread", totarget: self, withobject: nil) var mythread:nsthread = nsthread (Target: self, selector: "Otherthread", object: nil) Mythread.start () println (Nsthread.currentthread ()) } func otherthread () { println (Nsthread.currentthread()) println ("Hello! i am other thread ...") }}var dog = bigdog () println (Dog.hello ()) for var i=0;i< 100000;i++ { }
Nsthread.detachnewthreadselector ("Otherthread", Totarget:self, Withobject:nil)//Do not need start, directly execute function
2. Cocoa Nsoperation Example
3. Grand Central Dispatch Example
Swift Basic syntax-multithreading