iOS开发-多线程

多线程-NSOperation

用线程是为了什么

  • 完成一个费事的任务,且不影响UI响应
    • 不如封装一下,省得反复写例行代码
      • NSOperation:封装任务部分
        • 用selector编写任务:NSInvocationOperation
        • 用block编写任务:NSBlockOperation
        • 任务可以用依赖关系穿起来
      • NSOperationQueue:封装线程管理部分

NSOperation 抽象基类

  • 执行
    • -start 启动
      • 设置状态然后调用-main
    • cancel 取消
    • -waitUntilFinished
  • 任务定义
    • main
      • 默认啥也不干
      • 会在NSOperation提供的AutoreleasePool理智型
      • 定义NSOperation子类时并覆盖之

NSInvocationOperation

  • 创建
    • -initWithTarget:(id) selector:(SEL) boject:(id)
    • -initWithInvocation:(NSInvocation *)

NSBlockOperation

  • 创建
      • blcokOperationWithBlock:(void (^)(void))
  • 追加
    • -(void)addExecutionBlock:(void (^)(void))
  • Operation 会copy参数block

NSOperation 依赖关系

  • -(void)addDependency:(NSOperation *)dep
    • dep 完成后,本operation才会开始执行
    • 注意不要写出唤醒依赖!!
    • 依赖链中的operation即时已经完成,也不会从连中移除,除非调用-removeDependency:

NSOperation 状态 (iOS8+)

  • .name
  • .ready: 依赖都满足了没
  • .executing,.finished,.cancelled
  • .asynchronous
    • 默认都是NO,制度,只有子类能改变之
    • synchronized, in calling thread
  • -setCompletionBlock:(^void))/*copy*/
    • iOS4+,但iOS8+block开始执行后会置nil

NSOperationQueue

  • Queue operations
    • add
      • [q addOperation:anOp];
      • [q addOperations:anArrayOfOps waitUntilFinished:NO];
      • [q addOperationWithBlock:^{...}];
    • exec control
      • run once added
      • -setMaxConcurrentOperationCount;
      • operation -addDependency;
      • queue -cancelAllOperations
      • queue -setSuspended:(BOOL)
    • waitUntilAllOperationsAreFinished

Dispatch Queue

  • GCD Dispatch Queue is C API!
  • Queue Type :Serial,Concurrent,Main
  • Usage
    • get a queue
    • dispatch a block using the queue

在Objective-C里,dispatch objects 也是ObjC对象,支持ARC

Predefined Queue

  • Global Concurrent Queue
    • dispatch_get_global_queue(id,0)
      • returns dispatch_queue_t,falgs alwasy 0
      • id
        • QOS_CLASS_USER_INTERACTIVE/INITIATED/UNILITY/BACKGROUND
        • DISPATCH_QUEUE_PRIORITY_HIGH/DEFAULT/LOW/BACKGROUND
    • Main queue(serial)
      • dispatch_get_main_queue();

Create Queue

tips: 只能创建串行queue

  • dispatch_queue_create("com.my.q",NULL);
  • QueueOP
    • dispatch_suspend()/dispatch_resume()
    • on queue cleanup:dispatch_set_finalizer_f(func)

Queue the Tasks

  • dispatch_sysc(q,block) : don't do this on q's thread
  • dispatch_asysnc(q,block)
    • on task complete:dispatch a block at end of task
      • dispatch inside
        • dispatch_retain(q) before queue the task
        • dispatch_release(q) before queue complete block
    • get queue in block: dispatch_get_current_queue()

Special Tasks

  • singleton
    • dispatch_once(dispatch_once_t *,block);
  • timer
    • dispatch_after(dispatch_time_t,q,block);
  • for-loop
    • dispatch_apply(count,q,^(size_t i){....})

Dispatch Group

  • how to wait a group of tasks to finished
    • dispatch_group_t group = dispatch_group_create();
    • dispatch_group_async(g,q,^{});
    • dispatch_group_wait(g)

同步

  • dispatch_semaphore_t
    • dispatch_semaphore_create(long)
    • long dispatch_semaphore_wait(sema,timeout);
    • dispatch_semaphore_signal(sema)
  • barrier
    • dispatch_barrier_async(q,block)
    • dispatch_barrier_sync(q,block)

Dispatch Source

  • 用以接受各类事件
    • Timer
    • UNIX signal/FD/Process/Mach port
    • Custom
  • 用法
    • 创建
      • -dispatch_source_create(type,handle,mask,q)
    • 配置
      • -dispatch_source_set_event_handler(src,block)
        • dispatch_source_get_data/handle/mask(src)
      • dispatch_source_set_cancel_handler(src,block)
    • 启动:dispatch_resume(src)
    • 撤销:dispatch_source_cancel(src)

数据存储

数据持久化

  • 本地
    • 文件 NSHomeDirectiory()/Documents
    • 数据库
  • 云端
    • iCloud
      • 文件
      • key-value数据库
    • 第三方

你可能感兴趣的:(iOS开发-多线程)