IOS Multithreading and other additions

Source: Internet
Author: User
Tags gcd delete cache install cocoapods

  • Nsoperation

    • Nsoperation is an abstract class and does not have the ability to encapsulate operations, it must use its subclasses
  • Nsinvocationoperation

    • If you perform the actions in Nsinvocationoperation directly, the default is to execute the in the main thread
    • Nsinvocationoperation *OP1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (demo) Object:nil]; [OP1 start];

  • Nsblockoperation
    • If only one operation is encapsulated, the default is to execute it in the main thread
    • If a number of actions are encapsulated, other actions are performed in the child thread in addition to the first operation
    • Nsblockoperation *OP1 = [nsblockoperation blockoperationwithblock:^{        NSLog (@ "1-%@", [Nsthread CurrentThread]);    } ]; [OP1 start];

  • Custom operation
      • @implementation xmgoperation-(void) main{    NSLog (@ "%s,%@", __func__,[nsthread CurrentThread]);} @end

  • Nsoperationqueue
  • GCD Queue and Nsoperationqueue comparison

    • GCD
      • Serial: Create yourself, Home row
      • Concurrency: Create yourself, Global
      Nsoperationqueue
      • Create yourself: Alloc/init
      • Main queue: Mainqueue
  • Nsoperationqueue Features

    • A task is added to the 自己创建 queue to open a new thread
      • Default is concurrency: maxConcurrentOperationCount-1
      • Serial: Maxconcurrentoperationcount = 1
    • Task added to mainQueue queue does not open new thread
  • Invocation

      • //1. Create Queue Nsoperationque    UE *queue = [[Nsoperationqueue alloc] init]; 2. Package task Nsinvocationoperation *OP1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (Demo) Obje    Ct:nil]; 3. Add a task to the queue Addoperation:op1 [queue]; 

         

  • block
      • //1. Create Queue Nsoperationqueue *queue =    [[Nsoperationqueue alloc] init]; 2. Package task Nsblockoperation *OP1 = [nsblockoperation blockoperationwithblock:^{NSLog (@ "1 =%@", [Nsthread Currentt     Hread]);     }];    3. Add tasks to the queue [queue ADDOPERATION:OP1];    1. Create queue Nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; The Addoperationwithblock method will do two things//1. Create a Nsblockoperation object based on the incoming block//2. Add the internally created Nsblockoperation object to the queue/    /2. Add the task to the queue [addoperationwithblock:^{NSLog (@ "1 =%@", [Nsthread CurrentThread]);    }];    [Queue addoperationwithblock:^{NSLog (@ "2 =%@", [Nsthread CurrentThread]); }];

         

  • Custom
      • 1. Create Queue    Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];    2. Package task    Jxoperation *op1 = [[Jxoperation alloc] init];    3. Add tasks to the queue    [queue ADDOPERATION:OP1];

  • Pause-Resume
      • The currently executing task is not paused
      • Resumes execution from the first non-executed task
      • If yes, represents the need to pause//if it is no, on behalf of recovery execution self.queue.suspended = YES;

  • Cancel
      • The currently executing task is not canceled
      • Task cannot be resumed after canceling
      • Time-consuming operation should not perform a single judgment
      • The Cancel method that invokes all tasks internally [Self.queue cancelalloperations];

  • Inter-thread communication
      • Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//open sub-thread [queue addoperationwithblock:^{        //back to main thread        [[ Nsoperationqueue Mainqueue] addoperationwithblock:^{        }];}];

  • Dependency and monitoring
      • The current task will only be executed if the dependent task is completed
      • Can be cross-queue dependent
      • [Operationb Adddependency:operationa]; Operation B depends on operation a    op1.completionblock = ^{        NSLog (@ "First picture downloaded");    };    Op2.completionblock = ^{        NSLog (@ "The second picture has been downloaded")    ;

  • Image download
        • Duplicate download problem
          • definition dictionary save downloaded Pictures
        • disk cache issues
          • memory not trying to get from disk
        • Blocking main thread problem
          • new nsoperationqueue download picture
        • Repeating setup issues
          • reloadrowsatindexpaths
      逻辑1 - 从来没下载过 1.查看内存缓存是否有图片 2.查看磁盘缓存是否有图片 3.查看时候有任务正在下载当前图片 4.开启任务下载图片 5.写入磁盘 6.缓存到内存 7.移除下载操作 8.显示图片 逻辑2 - 已经下载过 1.查看内存缓存是否有图片 2.查看磁盘缓存是否有图片 3.使用磁盘缓存 4.将图片缓存到内存中 5.更新UI 逻辑3 - 已经下载过, 并且不是重新启动 1.查看内存缓存是否有图片 2.更新UI
  • Directory structure
  • Documents

      • You need to save files or data generated by the "application itself", such as: game progress, drawing of graffiti software
      • Files in the directory are automatically saved in ICloud
      • Note: Do not save files downloaded from the network, otherwise you will not be able to shelves!
      • Caches

        • Save temporary files, "need to use later", for example: Cached pictures, offline data (map data)
        • The system does not clean up files in the cache directory
        • "Must provide a cleanup solution for the cache directory" when the program is being developed
      • Preferences

        • User preferences, use Nsuserdefault Direct Read and write!
        • If you want data to be written to disk in a timely manner, you also need to call a synchronization method
      • Tmp

        • Save temporary files, "Follow up no need to use"
        • Files in the TMP directory, the system will automatically clean up
        • Restart your phone and the TMP directory will be emptied
        • System also cleans up automatically when the system is running out of disk space
      • Encapsulation get File path method

      • -(NSString *) cachedir{//1. Get the cache directory NSString *dir = [Nssearchpath    Fordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; return [dir stringbyappendingpathcomponent:[self lastpathcomponent];} -(NSString *) 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 Common face questions

      • What is the default cache time?

        • A week
      • The cached address

        • NSString *fullnamespace = [@ "Com.hackemist.SDWebImageCache." Stringbyappendingstring:ns];
      • Cleandisk How to clean up outdated images

        • Delete files older than the expiration date
        • Save file properties to calculate disk cache footprint
        • If the remaining disk cache space exceeds the maximum limit, perform a cleanup operation again, deleting the oldest file
      • Cleardisk How to clean up a disk

        • Delete Cache Directory
        • New Cache Directory
      • Sdwebimage How to play a picture

        • Remove each frame in the GIF to create an animated picture
      • Sdwebimage How to determine the type of picture

        • Determine the first 8 bytes of a picture binary
        • Kpngsignaturebytes[8] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
      What is
      • CocoaPods?

        The
        • CocoaPods is a dependency management tool for developing a third-party library of OS X and IOS applications. With CocoaPods, you can define your own dependencies (called pods), and it's easy to manage the versioning of third-party libraries over time and throughout the development environment
      • CocoaPods The concept behind is mainly embodied in two aspects

        • introducing third-party code into a project involves a lot of content. For OBJECTIVE-C junior developers, the configuration of the project file can be frustrating
        • in configuring buildphases and linker flags, there are many human error
        • CocoaPods that simplify this
      • Cocoapods The principle of

        • It is to put all dependent libraries in another named Pods project, and then let the main project rely on the PODS project, so that the source management work is Moved from the main project to the PODS project
        • 1, the PODS project is eventually compiled into a file named LIBPODS.A, and the main project only needs to rely on this. a file.
        • 2, for resource files, Cocoapods provides a bash script called pods-resources.sh, which executes every time the project is compiled and copies the various resource files from the third-party library to the target directory.
        • 3, cocoapods a file named Pods.xcconfig to set all dependencies and parameters at compile time.
      • Cocoapods installation

        • update gem
          • sudo gem update--system
        • Update R Uby software source
          • gem sources--remove https://rubygems.org/
          • gem Sources-a http://ruby.ta obao.org/
          • gem sources-l
        • install cocoapods
          • sudo gem install cocoapods
        • replace mirror index of cocoapods
          • pod repo remove master
          • pod repo add master&nb Sp;http://git.oschina.net/akuandev/specs.git
          • pod repo add master https://gitcafe.com/akuandev/ Specs.git
          • pod repo update
        • set pod warehouse
          • pod Setup
        • test
          • pod--version
      • Uninstall cocoapods

        • sudo g EM uninstall cocoapods
      • Cocoapods use:

        • use to create a new file named Podfile
        • will depend on the name of the library The second column is in the file
    platform :iospod‘AFNetworking‘
      • Comment Items
        • 1. After using Cocopods to manage the library, open the project later with Xxxx.xcworkspace instead of the previous. xcodeproj file
        • 2. Each time you change the Podfile file, you need to re-execute the pod update command.
        • 3.CocoaPods when performing pod install and pod Update, the Cocopods Spec warehouse index is updated first. Use the--no-repo-update parameter to disable index update operations
    pod install --no-repo-updatepod update --no-repo-update

IOS Multithreading and other additions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.