- 浏览: 582102 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
JYY282:
[i][/i]
Ecshop与Shopex的比较 -
qq247890212:
我也遇见这问题了,真诡异。重新下载个猫换了就好了。 太浪费时间 ...
诡异:ClassNotFoundException: org.springframework.web.filter.CharacterEncoding
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.
- #import <UIKit/UIKit.h>
- @inter<span>f</span>ace OperationsAppDelegate : NSObject <UIApplicationDelegate> {
- UIWindow *window;
- NSBlockOperation *simpleOperation;
- }
- @property (nonatomic, retain) IBOutlet UIWindow *window;
- @property (nonatomic, retain) NSBlockOperation *simpleOperation;
- @end
- #import "OperationsAppDelegate.h"
- @implementation OperationsAppDelegate
- @synthesize window;
- @synthesize simpleOperation;
- - (BOOL ) application:(UIApplication *)application did<span>F</span>inishLaunchingWithOptions:(NSDictionary *)launchOptions {
- /* Here is our block */
- NSBlockOperation *newBlockOperation = [NSBlockOperation blockOperationWithBlock:^{
- NSLog(@"Main Thread = %@" , [NSThread mainThread]);
- NSLog(@"Current Thread = %@" , [NSThread currentThread]);
- NSUInteger counter = 0;
- for (counter = 0;counter < 1000;counter++){
- NSLog(@"Count = %lu" , (unsigned long )counter);
- }
- }];
- /* Make sure we keep the reference somewhere */
- self.simpleOperation = newBlockOperation;
- /* Start the operation */
- [self.simpleOperation start];
- /* Print something out just to test i<span>f</span> we have to wait
- <span>f</span>or the block to execute its code or not */
- NSLog(@"Main thread is here" );
- [window makeKeyAndVisible];
- return YES;
- }
- - (void )dealloc {
- [simpleOperation release];
- [window release];
- [super dealloc];
- }
- @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方法执行改任务
- #import <UIKit/UIKit.h>
- @interface OperationsAppDelegate : NSObject <UIApplicationDelegate> {
- UIWindow *window;
- NSInvocationOperation *simpleOperation;
- }
- @property (nonatomic, retain) IBOutlet UIWindow *window;
- @property (nonatomic, retain) NSInvocationOperation *simpleOperation;
- @end
- - ( void ) simpleOperationEntry:(id)paramObject{
- NSLog(@"Parameter Object = %@" , paramObject);
- NSLog(@"Main Thread = %@" , [NSThread mainThread]);
- NSLog(@"Current Thread = %@" , [NSThread currentThread]);
- }
- - (BOOL ) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- NSNumber *simpleObject = [NSNumber numberWithInteger:123];
- NSInvocationOperation *newOperation =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(simpleOperationEntry:) object:simpleObject];
- self.simpleOperation = newOperation;
- [newOperation release];
- [self.simpleOperation start];
- [window makeKeyAndVisible];
- return YES;
- }
- - (void )dealloc {
- [simpleOperation release];
- [window release];
- [super dealloc];
- }
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.
这里的operationId属性不是必须的
- @implementation MyTask
- @synthesize operationId;
- - (void )main{
- NSLog(@"task %i run … " ,operationId);
- [NSThread sleep<span>F</span>orTimeInterval:10];
- NSLog(@"task %i is <span>f</span>inished. " ,operationId);
- }
- @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:
- - ( void ) connectionHasFailedWithError:(NSError *)paramError onRemoteURL:(NSURL *)paramRemoteURL{
- /* We failed to download the file. Attempt to download it again after 3 seconds */
- [self performSelector:@selector(attemptToDownloadRemoteURL:) withObject:paramRemoteURL afterDelay:3.0f];
- }
- - (void ) attemptToDownloadRemoteURL:(NSURL *)paramRemoteURL{
- /* Attempt to download the remote file again here by initializing
- a new connection ... */
- }
该方法只能接受一个参数。如果需要传递多个参数怎么办呢???
让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:
- - ( void ) startPainting{
- SEL selectorToCall = @selector(paint:);
- NSMethodSignature *methodSignature = [[self class ] instanceMethodSignatureForSelector:selectorToCall];
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
- [invocation setTarget:self];
- [invocation setSelector:selectorToCall];
- /* Start a scheduled timer now */
- NSTimer *newTimer =[NSTimer scheduledTimerWithTimeInterval:1.0
- invocation:invocation
- repeats:YES];
- self.paintingTimer = newTimer;
- }
4:timerWithTimeInterval:target:selector:userInfo:repeats:
(用该方式,需要把timer添加到runloop中)
- - ( void ) startPainting{
- NSTimer *newTimer = [NSTimer timerWithTimeInterval:1.0
- target:self
- selector:@selector(paint:)
- userInfo:nil
- repeats:YES];
- self.paintingTimer = newTimer;
- [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
- }
5:timerWithTimeInterval:invocation:repeats:
(用该方式,需要把timer添加到runloop中)
- - ( void ) startPainting{
- SEL selectorToCall = @selector(paint:);
- NSMethodSignature *methodSignature =[[self class ] instanceMethodSignatureForSelector:selectorToCall];
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
- [invocation setTarget:self];
- [invocation setSelector:selectorToCall];
- NSTimer *newTimer = [NSTimer timerWithTimeInterval:1.0
- invocation:invocation
- repeats:YES];
- self.paintingTimer = newTimer;
- [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer
- forMode:NSDefaultRunLoopMode];
- }
6:NSTimer 响应函数定义格式
并需有一个NSTimer *类型的参数
六:NSThread
1:initWithTarget:selector:object:
2:detachNewThreadSelector:toTarget: withObject:
以上两种方式,selector调用的函数,必须声明自己的NSAutoreleasePool
3:performSelectorInBackground: withObject:
一个简单的方法来创建线程,而无需直接处理线程。
4:start
调用start方法启动线程
5:cancel
调用cancel方法,并把变量赋值为nil
6:cancel vs exit
对于线程调用cancel方法停止,不要调用exit,因为exit方法没有给线程清理自己并释放资源的时间
7:线程的内存泄露
- - ( void ) newThreadEntryPoint{
- /* A thread without an autorelease pool to test the following code */
- //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- /* This WILL cause a memory leak */
- [self performSelector:@selector(allocateSomething)];
- /* This will NOT cause a memory leak */
- [self performSelectorOnMainThread:@selector(allocateSomething)
- withObject:nil
- waitUntilDone:YES];
- //[pool release];
- }
- - (void ) allocateSomething{
- NSBundle *mainBundle = [NSBundle mainBundle];
- NSString *imagePath = [mainBundle pathForResource:@"MyImage" ofType:@ "png" ];
- NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
- UIImage *myImage = [[[UIImage alloc] initWithData:imageData] autorelease];
- /* Do something with the image here */
- }
- - (void )viewDidLoad {
- [NSThread detachNewThreadSelector:@selector(newThreadEntryPoint)
- toTarget:self
- withObject:nil];
- }
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 对象被放进主线程的自动释放池,在主线程销毁是被自动释放
发表评论
-
Objective-C 与 C++ 的异同
2013-04-02 12:03 1401http://www.cnblogs.com/y041039 ... -
Cocos2D-X是全球知名的开源跨平台手机游戏引擎
2013-01-22 10:05 2757http://www.oschina.net/p/cocos ... -
iOS Keyboard 键盘高度变化 自适应
2013-01-15 15:43 3253[[NSNotificationCenter default ... -
iOS使用自定义字体
2012-11-27 12:11 12145From: http://blog.csdn.net/csy1 ... -
4 款类似 Facebook/Path 切换效果的 iOS 组件
2012-11-27 12:03 2200From: http://blog.csdn.net/lia ... -
Path 2.0的UI界面设计详细介绍
2012-11-27 11:56 1472如Path的创始人Dave Morin ... -
史上最全的App Store邮箱列表
2012-11-27 11:51 1272From: http://roybaby.blog.51cto ... -
iOS从info.plist 获取项目的名称及版本号
2012-11-16 10:54 1676From: http://blog.sina.com.cn/s ... -
MapKit annotation drag and drop with callout info update
2012-10-13 10:38 2410http://hollowout.blogspot ... -
NSArray 或NSDictionary 调用writeToFile方法失败原因
2012-08-31 10:03 4489NSArray 或NSDictionary 调用writeTo ... -
如何让IOS应用从容地崩溃
2012-08-30 15:25 1621From: http://www.cocoachina.com ... -
iOS中判断设备系统版本
2012-08-29 17:17 31716在iOS开发中,经常要考虑系统的向下兼容,如果使用 ... -
iOS 汉字转拼音
2012-08-21 16:42 1471From: http://www.cnblogs.com/v2 ... -
iOS模拟器截图工具
2012-08-17 16:35 1663From: http://magicalboy.com/ios ... -
XCode下的iOS单元测试
2012-08-10 17:47 1171From: http://mobile.51cto.com/ ... -
AFNetworking
2012-08-08 10:54 4655AFNetworking on github: https:/ ... -
Wrapping Conventions
2012-08-01 15:54 826Wrapping Conventions ... -
Core Animation如何使显式动画结束时的值直接作用Layer
2012-08-01 14:51 3799(1)使用隐式动画会直接改变layer的属性值,如: ima ... -
How To Debug Memory Leaks with XCode and Instruments Tutoria
2012-07-31 16:30 1059From: http://www.raywenderlich. ... -
Using Properties in Objective-C Tutorial
2012-07-31 16:27 932From: http://www.raywenderlich. ...
相关推荐
毕设和企业适用springboot企业数据管理平台类及跨境电商管理平台源码+论文+视频
功能说明: 环境说明: 开发软件:VS 2017 (版本2017以上即可,不能低于2017) 数据库:SqlServer2008r2(数据库版本无限制,都可以导入) 开发模式:mvc。。。
labview程序代码参考学习使用,希望对你有所帮助。
毕设和企业适用springboot社交应用平台类及用户数据分析平台源码+论文+视频
大米外贸商城系统 简称damishop 完全开源版,只需做一种语言一键开启全球133中语言自动翻译功能,价格实现自动汇率转换,集成微信支付宝 paypal以及国外主流支付方式,自带文章博客系统。 软件架构 基于MVC+语言包模式,增加控制台,API导入产品方便对接其他系统(带json示例数据)。 使用要求 PHP7.4+ MYSQL5.6+ REDIS(可选) 安装方法 composer install 打开安装向导安装 http://您的域名/install 特色 1、缓存层增加时间与批量like删除 2、API产品导入方便对接其他系统 3、增加控制台命令行,命令行生成语言翻译包 4、后台一键开启自动翻译模式,支持全球133中语言,由于google代理翻译需要收费,这个功能需要付费。 5、可选购物车与ajax修改购物车产品 6、一键结算checkout 7、增加网站前台自定义路由 方便seo 更新日志 v3.9.7 集成鱼码支付接口,方便个人站长即使收款到账使用 v3.9.3 更新内容 1:增加ueditor与旧编辑器切换 2:增加可视化布局插
labview程序代码参考学习使用,希望对你有所帮助。
毕设和企业适用springboot生鲜鲜花类及生物识别平台源码+论文+视频.zip
毕设和企业适用springboot企业健康管理平台类及视觉识别平台源码+论文+视频.zip
毕设和企业适用springboot视频编辑类及餐饮管理平台源码+论文+视频.zip
labview程序代码参考学习使用,希望对你有所帮助。
毕设和企业适用springboot社区物业类及智能仓储平台源码+论文+视频
毕设和企业适用springboot企业知识管理平台类及人工智能医疗平台源码+论文+视频
毕设和企业适用springboot汽车电商类及新闻传播平台源码+论文+视频
毕设和企业适用springboot生鲜鲜花类及全渠道电商平台源码+论文+视频.zip
毕设和企业适用springboot企业数据智能分析平台类及投票平台源码+论文+视频
毕设和企业适用springboot全渠道电商平台类及人工智能客服平台源码+论文+视频
毕设和企业适用springboot企业云存储平台类及AI数据标注平台源码+论文+视频
毕设和企业适用springboot人工智能客服系统类及旅游规划平台源码+论文+视频
毕设和企业适用springboot社交电商类及环境监控平台源码+论文+视频
毕设和企业适用springboot生鲜鲜花类及大数据存储平台源码+论文+视频