iOS多线程应用开发中使用NSOperation类的基本方法概要.docxVIP

  • 3
  • 0
  • 约1.61万字
  • 约 28页
  • 2021-04-13 发布于天津
  • 举报

iOS多线程应用开发中使用NSOperation类的基本方法概要.docx

iOS 多线程应用开发中使用 NSOperation 类的基本方法 这篇文章主要介绍了 iOS多线程应用开发中使用 NSOperation类的基本方法,代码基于传统的 Objective-C,需要的朋友可以参考下 一、NSOperation简介 简单说明 NSOperation的作?:配合使用 NSOperation和NSOperationQueue也能实现多线程编程 NSOperation 和 NSOperationQueue 实现多线程的具体步骤: (1) 先将需要执行的操作封装到一个 NSOperation对象中 (2) 然后将 NSOperation 对象添加到 NSOperationQueue 中 (3) 系统会?动将 NSOperationQueue 中的 NSOperation取岀来 (4) 将取岀的NSOperation封装的操作放到?条新线程中执? NSOperation 的子类 NSOperation是个抽象类,并不具备封装操作的能力必须使?它的子类 使用 NSOperation? 类的方式有 3种: ( 1 ) NSInvocationOperation ( 2) NSBlockOperation (3)自定义子类继承NSOperation,实现内部相应的?法 具体说明 NS In vocati onO peration 子类 创建对象和执行操作: 复制代码代码如下: //创建操作对象,封装要执行的任务 //NSInvocatio“Operation 圭寸装操作 selector:@selectorNSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector (test) object: nil]; //执行操作 [operation start]; 说明:一旦执?操作,就会调用target的test方法 代码示例: 复制代码代码如下: // // YYViewController.m // 01-NSOperation 基本 1 // // Created by 孔医己 on 14-6-25. // Copyright (c) 2014 年 itcast. All rights reserved. // #import YYViewController.h @i nterface YYViewCo ntroller () @end 复制代码代码如下: @implementation YYViewController -(void)viewDidLoad { [super viewDidLoad]; //NSOperatio n:抽象类,不具备封装功能 //创建操作对象,封装要执行的任务 //NSInvocatio“Operation 圭寸装操作 selector:@selectorNSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector (test) object: nil]; //执行操作 [operation start]; } -(void)test { NSLog(@--test--%@--,[NSThreadcurre ntThread]); } @end 打印查看: (null) t nur2014-66-25 21:13:23.294 OL-NS Ope ration 基本 1 [1492: Mb (null) t nur 注意:操作对象默认在主线程中执行,只有添加到队列中才会开启新的线程。即默认情况下,如果操 作没有放到队列中 queue中,都是同步执行。只有将 NSOperation放到一个 NSOperationQueue中,才 会异步执行操作 NSBIockOperatio n 子类 创建对象和添加操作: 复制代码代码如下: //创建NSBlockOperation操作对象 NSBIockOperation *operation=[NSBIockOperation blockOperationWithBIock:A{ //…… }]; //添加操作 [operation addExecutionBlock:A{ 〃.… }]; 代码示例: 代码1: 复制代码代码如下: // // YYViewController.m // 02-NSTherad基本 2 // // Created by 孔医己 on 14-6-25. // Copyright (c) 2014 年 All rights res

文档评论(0)

1亿VIP精品文档

相关文档