IOS multithreading and other supplements 02, ios multithreading supplements 02
NSOperation
- NSOperation is an abstract class and does not have the ability to encapsulate operations. It must be used as a subclass.
NSInvocationOperation
- NSBlockOperation
- Custom Operation
- NSOperationQueue
Comparison between GCD queue and NSOperationQueue
- GCD
- Serial: self-created, main queue
- Concurrency: self-created, global
NSOperationQueue
- Self-created: alloc/init
- Main queue: mainQueue
NSOperationQueue features
- Add task
Create by yourself
New threads are enabled in the queue.
- Concurrency by default: maxConcurrentOperationCount-1
- Serial: maxConcurrentOperationCount = 1
- Add task
mainQueue
New threads are not enabled in the queue
Invocation
- Block
-
// 1. create a queue NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // 2. encapsulation task NSBlockOperation * op1 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "1 = % @", [NSThread currentThread]);}]; // 3. add the task to the queue [queue addOperation: op1]; // 1. create a queue NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // The addOperationWithBlock method will do two things // 1. create an NSBlockOperation object based on the input block // 2. add the NSBlockOperation object created internally to the queue. // 2. add the task to the queue [queue addOperationWithBlock: ^ {NSLog (@ "1 =%@", [NSThread currentThread]) ;}]; [queue addOperationWithBlock: ^ {NSLog (@ "2 = % @", [NSThread currentThread]);}];
- Custom
- Pause-resume
- Cancel
- Inter-thread Communication
- Dependency and listener
- Image download
- Directory structure
Documents
-
- Files or data generated by the application itself need to be saved, such as drawing of game progress and graffiti software.
- Files in the directory will be automatically stored in the iCloud
- Note: Do not save the files downloaded from the network. Otherwise, they cannot be mounted!
-
Caches
- Save temporary files and "need to be used later", such as cache images and offline data (map data)
- The system does not clear files in the cache directory.
- When the program is required to be developed, "the cache directory cleaning solution must be provided"
Preferences
- User preference: Use NSUserDefault to directly read and write!
- If you want to write data to the disk in time, you also need to call a synchronization method.
Tmp
- Save the temporary file, "No need to use later"
- Files in the tmp directory are automatically cleared.
- Restart the phone, and the tmp directory will be cleared.
- When the system disk space is insufficient, the system automatically clears the disk space.
Encapsulation Method for obtaining the file path
-(NSString *) cacheDir {// 1. obtain the cache directory NSString * dir = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; return [dir stringByAppendingPathComponent: [self lastPathComponent *) documentDir {NSString * dir = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; return [dir stringByAppendingPathComponent: [self lastPathComponent];}-(NSString *) tmpDir {NSString * dir = NSTemporaryDirectory (); return [dir stringByAppendingPathComponent: [self lastPathComponent];}
-
- SDWebImage Architecture
SDWebImageManager
- SDImageCache
- SDWebImageDownloader
- SDWebImageDownloaderOperation
SDWebImage FAQ
Default Cache Time
Cache address
- NSString * fullNamespace = [@ "com. hackemist. SDWebImageCache." stringByAppendingString: ns];
How does cleanDisk clear expired images?
- Delete objects earlier than the expiration date
- Save file attributes to calculate disk cache space occupied
- If the remaining disk cache space exceeds the maximum limit, perform the cleanup operation again to delete the earliest files.
How to clear a clearDisk
- Delete cache directory
- Create cache directory
How to play images with SDWebImage
- Each frame in the gif is taken and an animated image is generated.
How does SDWebImage determine the image type?
- Determine the first 8 bytes of the Image Binary
- KPNGSignatureBytes [8] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
CocoaPods Installation
- Update gem
- Sudo gem update -- system
- Update ruby software sources
- Gem sources -- remove https://rubygems.org/
- Gem sources-a http://ruby.taobao.org/
- Gem sources-l
- Install CocoaPods
- Sudo gem install cocoapods
- Replace CocoaPods image Index
- Pod repo remove master
- Pod repo add master http://git.oschina.net/akuandev/Specs.git
- Pod repo add master https://gitcafe.com/akuandev/Specs.git
- Pod repo update
- Set pod Repository
- Test
Uninstall CocoaPods
- Sudo gem uninstall cocoapods
CocoaPods usage:
- Create a new file named Podfile
- List the dependent library names in the file in sequence
platform :iospod'AFNetworking'
- Comments
- 1. After using the CocoPods Management Library, open the project with xxxx. xcworkspace later, instead of the previous. xcodeproj file.
- 2. Every time you change the Podfile file, you need to re-execute the pod update command.
- 3. When executing pod install and pod update, CocoaPods will update the CocoPods spec repository index by default. You can use the -- no-repo-update parameter to disable INDEX update.
pod install --no-repo-updatepod update --no-repo-update