1.timer使用注意在使用uiscrollView划动时停止
问题描述:
用一个NSTimer定时刷新,而在UISCrollView划动发生的过程中,刷新的效果停止.
解决办法:
- (void)setTimer
{
NSTimer *timer;
NSDate *date = [NSDate date];
timer = [[NSTimer alloc] initWithFireDate:date interval:m_ablum.m_displayInterval target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.m_timer = timer;
[timer release];
}
在NSDefaultRunLoopMode模式下,没有更新.
需要注意的:
timer = [[NSTimer alloc] initWithFireDate:date interval:m_ablum.m_displayInterval target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
在设置为target时,其retaincount 加一.!!!!
一定注意
(转自:http://hi.baidu.com/hh20040410/blog/item/dbc62c01e92944da267fb5ee.html)
2. NSClassFromString 和 NSSelectorFromString
NSClassFromString
NSSelectorFromString
正常来说,
id myObj = [[NSClassFromString(@"MySpecialClass") alloc] init];
和
id myObj = [[MySpecialClass alloc] init];
是一样的。但是,如果你的程序中并不存在MySpecialClass这个类,下面的写法会出错,而上面的写法只是返回一个空对象而已。
因此,在某些情况下,可以使用NSClassFromString来进行你不确定的类的初始化。
比如在iPhone中,NSTask可能就会出现这种情况,所以在你需要使用NSTask时,最好使用:
[[NSClassFromString(@"NSTask") .....]]
而不要直接使用[NSTask ...]这种写法。
NSClassFromString的好处是:
1 弱化连接,因此并不会把没有的Framework也link到程序中。
2 不需要使用import,因为类是动态加载的,只要存在就可以加载。
for (int c=0; c<[classNames count]; c++) {
NSString *className=[classNames objectAtIndex:c];
id class=[[NSClassFromString(className) alloc] init];
for (int i=0; i<[params count]; i++) {
[class performSelector:NSSelectorFromString([NSString stringWithFormat:@"setA%i",i])];
}
}
转自:http://hi.baidu.com/hh20040410/blog/item/81fe1399fb3e8e046f068c43.html
3.
多线程之NSInvocationOperation
T 多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了 NSOperation这个类,提供了一个优秀的多线程编程方法。
本次介绍NSOperation的子集,简易方法的NSInvocationOperation:
@implementation MyCustomClass - (void)launchTaskWithData:(id)data { //创建一个NSInvocationOperation对象,并初始化到方法 //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method) //在这里,object后的值是想传递给前面方法的数据 NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:selfselector:@selector(myTaskMethod:) object:data]; // 下面将我们建立的操作“Operation”加入到本地程序的共享队列中(加入后方法就会立刻被执行) // 更多的时候是由我们自己建立“操作”队列 [[MyAppDelegate sharedOperationQueue] addOperation:theOp]; } // 这个是真正运行在另外一个线程的“方法” - (void)myTaskMethod:(id)data { // Perform the task. } @end
一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。下面是建立并初始化一个操作队列:
@interface : UIViewController { NSOperationQueue *operationQueue; //在头文件中声明该队列 } @end @implementation MyViewController - (id)init {self = [super init]; if (self) {operationQueue = [[NSOperationQueue alloc] init]; //初始化操作队列 [operationQueue setMaxConcurrentOperationCount:1]; //在这里限定了该队列只同时运行一个线程 //这个队列已经可以使用了 } return self; } - (void)dealloc { [operationQueue release]; //正如Alan经常说的,我们是程序的好公民,需要释放内存! [super dealloc]; } @end
简单介绍之后,其实可以发现这种方法是非常简单的。很多的时候我们使用多线程仅仅是为了防止主线程堵塞,而 NSInvocationOperation就是最简单的多线程编程,在iPhone编程中是经常被用到的。
转自:http://hi.baidu.com/hh20040410/blog/item/db043782034f01b36c81199b.html
问题描述:
用一个NSTimer定时刷新,而在UISCrollView划动发生的过程中,刷新的效果停止.
解决办法:
- (void)setTimer
{
NSTimer *timer;
NSDate *date = [NSDate date];
timer = [[NSTimer alloc] initWithFireDate:date interval:m_ablum.m_displayInterval target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.m_timer = timer;
[timer release];
}
在NSDefaultRunLoopMode模式下,没有更新.
需要注意的:
timer = [[NSTimer alloc] initWithFireDate:date interval:m_ablum.m_displayInterval target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
在设置为target时,其retaincount 加一.!!!!
一定注意
(转自:http://hi.baidu.com/hh20040410/blog/item/dbc62c01e92944da267fb5ee.html)
2. NSClassFromString 和 NSSelectorFromString
NSClassFromString
NSSelectorFromString
正常来说,
id myObj = [[NSClassFromString(@"MySpecialClass") alloc] init];
和
id myObj = [[MySpecialClass alloc] init];
是一样的。但是,如果你的程序中并不存在MySpecialClass这个类,下面的写法会出错,而上面的写法只是返回一个空对象而已。
因此,在某些情况下,可以使用NSClassFromString来进行你不确定的类的初始化。
比如在iPhone中,NSTask可能就会出现这种情况,所以在你需要使用NSTask时,最好使用:
[[NSClassFromString(@"NSTask") .....]]
而不要直接使用[NSTask ...]这种写法。
NSClassFromString的好处是:
1 弱化连接,因此并不会把没有的Framework也link到程序中。
2 不需要使用import,因为类是动态加载的,只要存在就可以加载。
for (int c=0; c<[classNames count]; c++) {
NSString *className=[classNames objectAtIndex:c];
id class=[[NSClassFromString(className) alloc] init];
for (int i=0; i<[params count]; i++) {
[class performSelector:NSSelectorFromString([NSString stringWithFormat:@"setA%i",i])];
}
}
转自:http://hi.baidu.com/hh20040410/blog/item/81fe1399fb3e8e046f068c43.html
3.
多线程之NSInvocationOperation
T 多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了 NSOperation这个类,提供了一个优秀的多线程编程方法。
本次介绍NSOperation的子集,简易方法的NSInvocationOperation:
@implementation MyCustomClass - (void)launchTaskWithData:(id)data { //创建一个NSInvocationOperation对象,并初始化到方法 //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method) //在这里,object后的值是想传递给前面方法的数据 NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:selfselector:@selector(myTaskMethod:) object:data]; // 下面将我们建立的操作“Operation”加入到本地程序的共享队列中(加入后方法就会立刻被执行) // 更多的时候是由我们自己建立“操作”队列 [[MyAppDelegate sharedOperationQueue] addOperation:theOp]; } // 这个是真正运行在另外一个线程的“方法” - (void)myTaskMethod:(id)data { // Perform the task. } @end
一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。下面是建立并初始化一个操作队列:
@interface : UIViewController { NSOperationQueue *operationQueue; //在头文件中声明该队列 } @end @implementation MyViewController - (id)init {self = [super init]; if (self) {operationQueue = [[NSOperationQueue alloc] init]; //初始化操作队列 [operationQueue setMaxConcurrentOperationCount:1]; //在这里限定了该队列只同时运行一个线程 //这个队列已经可以使用了 } return self; } - (void)dealloc { [operationQueue release]; //正如Alan经常说的,我们是程序的好公民,需要释放内存! [super dealloc]; } @end
简单介绍之后,其实可以发现这种方法是非常简单的。很多的时候我们使用多线程仅仅是为了防止主线程堵塞,而 NSInvocationOperation就是最简单的多线程编程,在iPhone编程中是经常被用到的。
转自:http://hi.baidu.com/hh20040410/blog/item/db043782034f01b36c81199b.html