`
janedoneway
  • 浏览: 568909 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

ios 任务、线程、定时器

 
阅读更多

From : http://o0o0o0o.iteye.com/blog/1326772

一:operations(任务)

 

cocoa提供了三种不同的operations

 

1:Block operations(NSBlockOperation
These f acilitate the execution of one or more block objects.

 

 

C代码  收藏代码
  1. #import <UIKit/UIKit.h>   
  2. @inter<span>f</span>ace OperationsAppDelegate : NSObject <UIApplicationDelegate> {  
  3.     UIWindow *window;  
  4.     NSBlockOperation *simpleOperation;  
  5. }  
  6.   
  7. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  8. @property (nonatomic, retain) NSBlockOperation *simpleOperation;  
  9.   
  10. @end  

 

C代码  收藏代码
  1. #import "OperationsAppDelegate.h"   
  2.   
  3. @implementation OperationsAppDelegate  
  4. @synthesize window;  
  5. @synthesize simpleOperation;  
  6.   
  7. - (BOOL ) application:(UIApplication *)application did<span>F</span>inishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  8.     /* Here is our block */   
  9.     NSBlockOperation *newBlockOperation = [NSBlockOperation blockOperationWithBlock:^{  
  10.                             NSLog(@"Main Thread = %@" , [NSThread mainThread]);  
  11.                             NSLog(@"Current Thread = %@" , [NSThread currentThread]);  
  12.                             NSUInteger counter = 0;  
  13.                             for (counter = 0;counter < 1000;counter++){  
  14.                                 NSLog(@"Count = %lu" , (unsigned  long )counter);  
  15.                             }  
  16.                                 }];  
  17.   
  18.     /* Make sure we keep the reference somewhere */   
  19.     self.simpleOperation = newBlockOperation;  
  20.       
  21.     /* Start the operation */   
  22.     [self.simpleOperation start];  
  23.   
  24.     /* Print something out just to test i<span>f</span> we have to wait  
  25.     <span>f</span>or the block to execute its code or not */   
  26.       
  27.     NSLog(@"Main thread is here" );  
  28.     [window makeKeyAndVisible];  
  29.     return  YES;  
  30. }  
  31.   
  32.   
  33. - (void )dealloc {  
  34.     [simpleOperation release];  
  35.     [window release];  
  36.     [super dealloc];  
  37. }  
  38.   
  39. @end  

 

 


2:Invocation operations(NSInvocationOperation
These allow you to invoke a method in another, currently existing object.

 

NSNumber *simpleObject = [NSNumber numberWithInteger:123];

NSInvocationOperation *newOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(simpleOperationEntry:) object:simpleObject];

[newOperation  start];

 

 

调用start方法执行改任务

 

C代码  收藏代码
  1. #import <UIKit/UIKit.h>   
  2. @interface OperationsAppDelegate : NSObject <UIApplicationDelegate> {  
  3.     UIWindow *window;  
  4.     NSInvocationOperation *simpleOperation;  
  5. }  
  6.   
  7. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  8. @property (nonatomic, retain) NSInvocationOperation *simpleOperation;  
  9.   
  10. @end  

 

C代码  收藏代码
  1. - ( void ) simpleOperationEntry:(id)paramObject{  
  2.   
  3.     NSLog(@"Parameter Object = %@" , paramObject);  
  4.     NSLog(@"Main Thread = %@" , [NSThread mainThread]);  
  5.     NSLog(@"Current Thread = %@" , [NSThread currentThread]);  
  6. }  
  7.   
  8. - (BOOL ) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  9.   
  10.     NSNumber *simpleObject = [NSNumber numberWithInteger:123];  
  11.     NSInvocationOperation *newOperation =[[NSInvocationOperation alloc] initWithTarget:self    selector:@selector(simpleOperationEntry:) object:simpleObject];  
  12.     self.simpleOperation = newOperation;  
  13.     [newOperation release];  
  14.   
  15.     [self.simpleOperation start];  
  16.     [window makeKeyAndVisible];  
  17.     return  YES;  
  18. }  
  19.   
  20. - (void )dealloc {  
  21.   
  22.     [simpleOperation release];  
  23.     [window release];  
  24.     [super dealloc];  
  25. }  

 

 

 

 

 

 


3:Plain operations(简单的任务)NSOperation的子类


These are plain operation classes that need to be subclassed. The code to be executed
will be written inside the main method of the operation object.

 

 

C代码  收藏代码
  1. @interface MyTask : NSOperation {   
  2.     int  operationId;   
  3. }  
  4.   
  5. @property int  operationId;  
  6.   
  7. @end  

 

这里的operationId属性不是必须的

 

C代码  收藏代码
  1. @implementation MyTask  
  2.   
  3. @synthesize operationId;  
  4.   
  5. - (void )main{   
  6.     NSLog(@"task %i run … " ,operationId);   
  7.     [NSThread sleep<span>F</span>orTimeInterval:10];   
  8.     NSLog(@"task %i is <span>f</span>inished. " ,operationId);   
  9. }  
  10.   
  11. @end  

 

 

必须
- (void)main;方法,[MyTask start]是执行main方法

 

 

二:任务队列(NSOperationQueue)

 

NSOperationQueue *newOperationQueue = [[NSOperationQueue alloc] init];
[ newOperationQueue  addOperation:Operation];

 

 

以上三种Operation都可以添加到NSOperationQueue中,添加后立即被执行。

 

NSOperationQueue 可以设置最大并行执行任务数。默认值为-1无限制。

 

 

三:多个任务之间存在依赖关系

 

设置方式:

[self.firstOperation addDependency :self.secondOperation];

 

dependency:附属的意思

 

把secondOperation做为firstOperation的附属。因此先执行secondOperation,再执行firstOperation 。

 

 

四:延时执行某个方法

 

1: performSelector:withObject:afterDelay:

 

 

C代码  收藏代码
  1. - ( void ) connectionHasFailedWithError:(NSError *)paramError onRemoteURL:(NSURL *)paramRemoteURL{  
  2.     /* We failed to download the file. Attempt to download it again after 3 seconds */   
  3.     [self performSelector:@selector(attemptToDownloadRemoteURL:) withObject:paramRemoteURL afterDelay:3.0f];  
  4. }  
  5.   
  6. - (void ) attemptToDownloadRemoteURL:(NSURL *)paramRemoteURL{  
  7.     /* Attempt to download the remote file again here by initializing  
  8.     a new connection ... */   
  9. }  

 

该方法只能接受一个参数。如果需要传递多个参数怎么办呢???

     让selector调用的方法接受的参数类型修改为Dictionary类型。

 

 

(1)如果调用的selector不接受参数则,withObject:nil

(2) 通过performSelector:withObjcet:afterDelay调用的方法不能有返回值

 

 

2:取消延时执行的方法

 

(1)cancelPreviousPerformRequestsWithTarget: 

(2) cancelPreviousPerformRequestsWithTarget:selector:object:

 

 

五:NSTimer

 

1:scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

 

2:invalidate

调用invalidate方法,不仅是释放NSTimer,还释放userinfo对象。
如果repeats设置为NO,NSTimer在调用完成之后就知道失效,随即释放userinfo对象

 

3:scheduledTimerWithTimeInterval:invocation:repeats:

 

C代码  收藏代码
  1. - ( void ) startPainting{  
  2.     SEL selectorToCall = @selector(paint:);  
  3.     NSMethodSignature *methodSignature = [[self class ] instanceMethodSignatureForSelector:selectorToCall];  
  4.     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];  
  5.     [invocation setTarget:self];  
  6.     [invocation setSelector:selectorToCall];  
  7.   
  8. /* Start a scheduled timer now */   
  9. NSTimer *newTimer =[NSTimer scheduledTimerWithTimeInterval:1.0  
  10.                         invocation:invocation  
  11.                            repeats:YES];  
  12.   
  13.     self.paintingTimer = newTimer;  
  14. }  

 

 4:timerWithTimeInterval:target:selector:userInfo:repeats:

   (用该方式,需要把timer添加到runloop中)

 

C代码  收藏代码
  1. - ( void ) startPainting{  
  2.     NSTimer *newTimer = [NSTimer timerWithTimeInterval:1.0  
  3.                             target:self  
  4.                           selector:@selector(paint:)  
  5.                           userInfo:nil  
  6.                            repeats:YES];  
  7.   
  8.     self.paintingTimer = newTimer;  
  9.   
  10.     [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];  
  11. }  

 

5:timerWithTimeInterval:invocation:repeats:

  (用该方式,需要把timer添加到runloop中)

C代码  收藏代码
  1. - ( void ) startPainting{   
  2.     SEL selectorToCall = @selector(paint:);  
  3.     NSMethodSignature *methodSignature =[[self class ] instanceMethodSignatureForSelector:selectorToCall];  
  4.     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];  
  5.   
  6.     [invocation setTarget:self];  
  7.     [invocation setSelector:selectorToCall];  
  8.     NSTimer *newTimer = [NSTimer timerWithTimeInterval:1.0  
  9.                         invocation:invocation  
  10.                            repeats:YES];  
  11.     self.paintingTimer = newTimer;  
  12.     [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer  
  13.                      forMode:NSDefaultRunLoopMode];  
  14. }  

 

 

6:NSTimer 响应函数定义格式

 

需有一个NSTimer *类型的参数

 

C代码  收藏代码
  1. - ( void ) paint:(NSTimer *)paramTimer{  
  2.     /* Do something here */   
  3.     NSLog(@"Painting" );  
  4. }  

 

 

 

六:NSThread

 

1:initWithTarget:selector:object:

 

2:detachNewThreadSelector:toTarget: withObject:

 

以上两种方式,selector调用的函数,必须声明自己的NSAutoreleasePool

 

 

3:performSelectorInBackground: withObject:

     一个简单的方法来创建线程,而无需直接处理线程。

C代码  收藏代码
  1. [self performSelectorInBackground:@selector(thirdCounter) withObject:nil];  

 

4:start

调用start方法启动线程

 

5:cancel

调用cancel方法,并把变量赋值为nil

 

6:cancel vs exit

 

对于线程调用cancel方法停止,不要调用exit,因为exit方法没有给线程清理自己并释放资源的时间

 

7:线程的内存泄露

 

C代码  收藏代码
  1.  - ( void ) newThreadEntryPoint{  
  2.     /* A thread without an autorelease pool to test the following code */   
  3.     //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   
  4.       
  5.     /* This WILL cause a memory leak */   
  6.     [self performSelector:@selector(allocateSomething)];  
  7.   
  8.     /* This will NOT cause a memory leak */   
  9.     [self performSelectorOnMainThread:@selector(allocateSomething)  
  10.                 withObject:nil  
  11.                  waitUntilDone:YES];  
  12.     //[pool release];   
  13. }  
  14.   
  15. - (void ) allocateSomething{  
  16.     NSBundle *mainBundle = [NSBundle mainBundle];  
  17.     NSString *imagePath = [mainBundle pathForResource:@"MyImage"  ofType:@ "png" ];  
  18.     NSData *imageData = [NSData dataWithContentsOfFile:imagePath];  
  19.     UIImage *myImage = [[[UIImage alloc] initWithData:imageData] autorelease];  
  20.     /* Do something with the image here */   
  21. }  
  22.   
  23. - (void )viewDidLoad {  
  24.   
  25.     [NSThread detachNewThreadSelector:@selector(newThreadEntryPoint)  
  26.                        toTarget:self  
  27.                     withObject:nil];  
  28. }  

 

 

UIImage *myImage = [[[UIImage alloc] initWithData:imageData] autorelease];-------------自动释放池的范围

 

/* This WILL cause a memory leak */
[self performSelector:@selector(allocateSomething)];

调用改方法myImage 对象被添加进该新建线程的自动释放池,但因为在这里没有声明NSAutoreleasePool 造成内存泄露

 


 /* This will NOT cause a memory leak */
[self performSelectorOnMainThread:@selector(allocateSomething)
withObject:nil
     waitUntilDone:YES];

 

调用改方法myImage 对象被放进主线程的自动释放池,在主线程销毁是被自动释放

分享到:
评论

相关推荐

    在主线程上创建精准定时器

    在主线程上面创建定准定时器,一个简单的demo

    IOS计时器设计

    多种方法实现计时器,包括通信获取服务器端系统时间,多线程实现,NSTimer实现等等 http://blog.csdn.net/dingxiaowei2013/article/details/9864279

    iOS App使用GCD导致的卡顿现象及解决方法

    最近在调研 iOS app 中存在的各种卡顿现象以及解决方法。 iOS App 出现卡顿(stall)的概率可能超出大部分人的想象,尤其是对于大公司旗舰型 App。一方面是由于业务功能不停累积,各个产品团队之间缺乏协调,大家都...

    iOS GCD-Program-master

    ios 完美使用gcd 哪怕菜鸟都会使用的线程操作,队列,定时器,等待等

    iOS中使用NSProgress类来创建UI进度条的方法详解

    单任务进度的监听是NSProgress最简单的一种运用场景,我们来用定时器模拟一个耗时任务,示例代码如下: @interface ViewController () { NSProgress * progress; } @end @implementation ViewController - (void)...

    ios-仿QQ、微信表情下落动画.zip

    使用CADisplayLink创建一个刷新定时器,设置帧刷新调用方法时间,然后加入mainRunLoop里面执行。具体加在主线程的RunLoop还是子线程的RunLoop可根据自己项目进行修改。

    ios开发记录

    //判断定时器的指针是否存在(定时器的对象是否存在) if(_tim){ //必须在定时器失效以后将定时器的指针至为空 [_tim invalidate]; invalidate使…无效 _tim=nil; }else{ _tim=[NSTimer ...

    leetcode站台停留次数-iOS-Ready-For-Interview:认真工作!热爱生活!

    6.NSTimer定时器 准备资料: 《iOS与OS X多线程和内存管理》《52个有效方法》 实践: Swift 《objccn-swifter-tips》《objccn-advanced-swift》 UIKit UIView、UIViewController生命周期 事件响应链 UItableView优化 ...

    基于C++11线程池技术简单易用的轻量级网络编程框架源码.zip

    使用线程实现的简单易用的定时器。 信号量。 线程组。 简单易用的线程池,可以异步或同步执行任务,支持functional 和 lambad表达式。 工具库 文件操作。 std::cout风格的日志库,支持颜色高亮、代码定位、异步打印...

    ios-鹏哥哥放大镜.zip

    鹏哥哥放大镜 封装好 线程安全 无泄漏 简单易用 移植性好 使用中有问题 请前去GitHub与我联系 感谢您的使用 对您有帮助的话 送我一颗星星可好 使用简介: 1.PGGMagnigierView导入放大镜类 也可以在这里面进行镜框...

    Objective-C开发范例代码大全

    ● 构建使用了日期、定时器与内存管理的应用 ● 如何在其他平台上使用Objective-C  凭借深入的代码示例与清晰的解释说明,本书将帮助你轻松解决iOS开发人员每天都 会面临的挑战。通过使用书中介绍的知识与技能,...

    Unity3D 2018 最新最全800个脚本教程

    Unity3D音乐开关与音量条 Unity3D教程:GUILayout.Window和GUI.Window的区别 在游戏中改变地形高度 教你如何创建unity3d多个定时器,以及定时器的其他操作 Unity3D使用LitJson解析服务器上的JSON IOS下文件保存和...

    ZLToolKit:一个基于C++11的轻量级网络框架,基于线程池技术可以实现大并发网络IO

    一个基于C++11简单易用的轻量级网络编程框架项目特点基于...线程库使用线程实现的简单易用的定时器。信号量。线程组。简单易用的线程池,可以异步或同步执行任务,支持functional 和 lambad表达式。工具库文件操作。std

    iOS之UITableView计时器的实现方式总结(NSTimer、DispatchSource、CADisplayLink)

    今天博客中所涉及的内容并不复杂,都是一些平时常见的一些问题,通过这篇博客算是对UITableView中使用定时器的几种方式进行总结。本篇博客会给出在TableView中使用NSTimer或者DispatchSourcer中常见的五种方式。当然...

    多样式自定义无限滚动

    源码CorePPTVC,幻灯终结者:超简单、酷且炫、多样式、...支持定时器自动切换且界面push定时器停止. .8.支持幻灯点击事件回调. .9.幻灯框架可任意扩展与自定义. .9.内部很多控件均可高度自定义样式,灵活性非常大.

    CorePPTVC:幻灯终结者:超简单、酷且炫、多样式、自定义!

    支持定时器自动切换且界面push定时器停止..8.支持幻灯点击事件回调..9.幻灯框架可任意扩展与自定义..9.内部很多控件均可高度自定义样式,灵活性非常大.使用示例 //定义数据模型。如果数据来自远程服务器,可一键转...

Global site tag (gtag.js) - Google Analytics