iOS开发—线程的模拟暂停和继续操作
2017-11-11 20:16
337 查看
表视图开启线程下载远程的网络界面,滚动页面时势必会有影响,降低用户的体验。针对这种情况,当用户滚动屏幕的时候,暂停队列;用户停止滚动的时候,继续恢复队列。接下来通过一个案例,演示如何暂停和继续操作,具体内容如下:
(1)新建一个SingleViewApplication工程,命名为“13-SuspendAndContinue”;
(2)进入Main.StoryBoard,从对象库中拖拽3个Button到程序界面,分别设置Title为“添加”,“暂停”和“继续”,并且用拖拽的方式给这3个控件进行单击响应的声明,分别对应这添加操作、暂停操作、继续操作。界面如下:
(3)进入ViewController.m文件,在单击“添加”按钮后激发的方法中,首先设置操作的最大并发操作数为1,向创建的队列中添加20个操作,然后为线程设置休眠时间为1.0s ,相当于GCD的异步串行操作;
(4)当队列中的操作正在排队时,则将调用setSuspended: 方法传入YES参数将其挂起;当队列中的操作被挂起的时候,则调用setSuspended: 方法传入NO参数让它们继续排队,代码如下:
从图中可以看出,当单击“暂停”按钮后,有一个线程还要继续并执行完毕,这是因为当队列执行暂停的时候,这个线程仍在运行,只有其余排队的线程被挂起。
(1)新建一个SingleViewApplication工程,命名为“13-SuspendAndContinue”;
(2)进入Main.StoryBoard,从对象库中拖拽3个Button到程序界面,分别设置Title为“添加”,“暂停”和“继续”,并且用拖拽的方式给这3个控件进行单击响应的声明,分别对应这添加操作、暂停操作、继续操作。界面如下:
(3)进入ViewController.m文件,在单击“添加”按钮后激发的方法中,首先设置操作的最大并发操作数为1,向创建的队列中添加20个操作,然后为线程设置休眠时间为1.0s ,相当于GCD的异步串行操作;
(4)当队列中的操作正在排队时,则将调用setSuspended: 方法传入YES参数将其挂起;当队列中的操作被挂起的时候,则调用setSuspended: 方法传入NO参数让它们继续排队,代码如下:
#import "ViewController.h" @interface ViewController () @property (nonatomic,strong)NSOperationQueue *queue; - (IBAction)addOperation:(id)sender; - (IBAction)pause:(id)sender; - (IBAction)resume:(id)sender; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.queue=[[NSOperationQueue alloc] init]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //添加operation - (IBAction)addOperation:(id)sender { //设置操作的最大并发操作数 self.queue.maxConcurrentOperationCount=1; for(int i=0;i<20;i++) { [self.queue addOperationWithBlock:^{ //模拟休眠 [NSThread sleepForTimeInterval:1.0f]; NSLog(@"正在下载%@%d",[NSThread currentThread],i); }]; } } //暂停 - (IBAction)pause:(id)sender { //判断队列中是否有操作 if(self.queue.operationCount==0){ NSLog(@"没有操作"); return; } //如果没有被挂起,才需要暂停 if(!self.queue.isSuspended){ NSLo 997b g(@"暂停"); [self.queue setSuspended:YES]; }else{ NSLog(@"已经暂停"); } } //继续 - (IBAction)resume:(id)sender { //判断队列中是否有操作 if(self.queue.operationCount==0){ NSLog(@"没有操作" ); return; } //如果没有被挂起,才需要暂停 if(self.queue.isSuspended){ NSLog(@"继续"); [self.queue setSuspended:NO]; }else{ NSLog(@"正在执行"); } } @end运行程序,程序的运行结果如下:
从图中可以看出,当单击“暂停”按钮后,有一个线程还要继续并执行完毕,这是因为当队列执行暂停的时候,这个线程仍在运行,只有其余排队的线程被挂起。
相关文章推荐
- iOS边练边学--多线程NSOperation介绍,子类实现多线程的介绍(任务和队列),队列的取消、暂停(挂起)和恢复,操作依赖与线程间的通信
- iOS开发 实现动画的暂停和继续播放
- iOS 开发 多线程详解之NSThread线程通信的操作与控制
- (iOS开发) 录屏功能的实现~包括暂停和继续功能
- iOS 开发,用 NSInvocationOperation 和 OperationQueue 做多线程操作
- 【不全,仅参考】iOS开发数据库操作之SQLite及Mysql操作实例
- 如何解决iOS界面操作导致导致NSTimer暂停计时的问题?
- Java 线程暂停与继续
- IOS开发 沙盒中的文件操作
- ios助手开发系列(四):文件操作系列1 - 获取设备文件信息 AFCFileInfoOpen函数和AFCKeyValueRead函数
- IOS 开发学习26 NSDictionary的一些操作
- iOS开发多线程-线程间通讯
- IOS学习 NSOperation 并发数量、暂停/继续、取消、等待、依赖 与GCD区别
- 安卓TV开发(九) Android模拟事件 遥控器变身成鼠标来操作TV
- [iOS开发]沙盒中的文件操作
- [iOS开发项目-3] 按钮控件的移动,放大缩小,左右旋转操作
- iOS开发-AFNetworking 怎样上传数据? 怎样上传模拟表单
- IOS开发之了解线程锁 《转载自Mr.魏》
- iOS开发问题---xcode9 iphone7真机模拟出现 iPhone7 is busy:Preparing debugger support for ....
- iOS 开发 多线程详解之NSThread异步线程下载网络图片