开发笔记7-Multitasking多任务学习

今天接到一起找工作的同学的电话诉苦,本来他还是在那种iOS培训机构培训过,其实我还真看不起那培训机构,还在一家公司干过有过两个app上过线。今天面试可能遇到那种传说中的技术总监类的人物吧,直接被问道无语,同学确实做东西不用心记,好的IDE确实能加快工程进度和提高代码质量,但对于初学者来说容易让他产生依赖,一问一些关键的技术就答不出来了,连些用啥类也说不出来,最要命的是人家认定他干的没啥技术含量,如果让我看也许也是这么认为的,这就是说平时的学习工作一定要消化知识,知其然也知其所以然。电话中人家提到了多任务的知识,这个其实对于我来说也是盲区,今天就查了查《iOS5 Programminig CookBook》找到了Multitasking这一章,看了一下,确实挺费力的,先把今天的做的贴出来吧,做个记录:

先是检测本设备支不支持多任务:

- (BOOL) isMultitaskingSupported{
BOOL result = NO;
if ([[UIDevice currentDevice]
respondsToSelector:@selector(isMultitaskingSupported)]){ 
result = [[UIDevice currentDevice] isMultitaskingSupported];
}
return result;
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if ([self isMultitaskingSupported]){ NSLog(@"Multitasking is supported.");
} else {
NSLog(@"Multitasking is not supported."); 
}
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor]; 
[self.window makeKeyAndVisible];
return YES;
}

下一节这个内容我有些困难,不是百分百调出来,不知是作者的代码有问题,功能是在用户按了home按钮后程序在后台执行:

- (void) timerMethod:(NSTimer *)paramSender{
NSTimeInterval backgroundTimeRemaining =
[[UIApplication sharedApplication] backgroundTimeRemaining];
if (backgroundTimeRemaining == DBL_MAX){ 
NSLog(@"Background Time Remaining = Undetermined");
} else {
NSLog(@"Background Time Remaining = %.02f Seconds",
backgroundTimeRemaining);
} 
}
- (void)applicationDidEnterBackground:(UIApplication *)application{
if ([self isMultitaskingSupported] == NO){ 
return;
}
self.myTimer =
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self selector:@selector(timerMethod:) userInfo:nil
repeats:YES];
self.backgroundTaskIdentifier =
[application beginBackgroundTaskWithExpirationHandler:^(void) {
[self endBackgroundTask]; }];
}

- (void) endBackgroundTask{
dispatch_queue_t mainQueue = dispatch_get_main_queue();
__weak Completing_a_Long_Running_Task_in_the_BackgroundAppDelegate *weakSelf = self;
dispatch_async(mainQueue, ^(void) {
Completing_a_Long_Running_Task_in_the_BackgroundAppDelegate *strongSelf = weakSelf;
if (strongSelf != nil){ [strongSelf.myTimer invalidate]; [[UIApplication sharedApplication]
endBackgroundTask:self.backgroundTaskIdentifier]; strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
}); 
}




当程序切到后台时就在控制台一秒输出个Background Time Remaining = %.02f Seconds,但是方法endBackgroundTask:一直报错,是里面这个__weak声明的类型报错,这个__weak我也不知道是个什么东西,真是惭愧啊,再学学吧。今天就抛下这个问题了,明天解决一下~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值