- 3
- 0
- 约1.61万字
- 约 28页
- 2021-04-13 发布于天津
- 举报
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
您可能关注的文档
- DSW18型数控钢筋弯箍机说明书(改)要点.docx
- ERP系统实施规划解决方案.docx
- ERTU2000C_用户手册(硬件版本2)A1要点.docx
- EWI5000电吹管常用指法.docx
- Excel-使用技巧集锦——-163种使用技巧大全(超全).docx
- Excel中添加标签或文本框的操作方法.docx
- Excel完整教程—200种超全使用技巧.docx
- FFT的计算机实现.docx
- Fireye火检检测器要点.docx
- FIR数字滤波器设计与使用.docx
- 2025-2026学年天津市和平区高三(上)期末数学试卷(含解析).pdf
- 2025-2026学年云南省楚雄州高三(上)期末数学试卷(含答案).pdf
- 2025-2026学年甘肃省天水市张家川实验中学高三(上)期末数学试卷(含答案).docx
- 2025-2026学年福建省厦门市松柏中学高二(上)期末数学试卷(含答案).docx
- 2025-2026学年广西钦州市高一(上)期末物理试卷(含答案).docx
- 2025-2026学年河北省邯郸市临漳县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省石家庄二十三中七年级(上)期末历史试卷(含答案).docx
- 2025-2026学年海南省五指山市九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省唐山市玉田县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省邢台市市区九年级(上)期末化学试卷(含答案).docx
原创力文档

文档评论(0)