嘿我正在开发一个应用程序,我必须每隔30秒进行一次API调用,所以我为它创建了NSTimer.但当我的应用程序进入后台计时器后3-4分钟停止发射.因此它在后台只能工作3-4分钟,但之后却没有.如何修改我的代码,以便计时器不会停止.
这是我的一些代码.
- (IBAction)didTapStart:(id)sender {
NSLog(@"hey i m in the timer ..%@",[NSDate date]);
[objTimer invalidate];
objTimer=nil;
UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
objTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self
selector:@selector(methodFromTimer) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:objTimer forMode:UITrackingRunLoopMode];
}
-(void)methodFromTimer{
[LOG debug:@"ViewController.m ::methodFromTimer " Message:[NSString stringWithFormat:@"hey i m from timer ....%@",[NSDate date] ]];
NSLog(@"hey i m from timer ....%@",[NSDate date]);
}
Run Code Online (Sandbox Code Playgroud)
我甚至用以下内容更改了代码:
[[NSRunLoop mainRunLoop] addTimer:objTimer forMode:NSRunLoopCommonModes];
Run Code Online (Sandbox Code Playgroud)
这也不起作用.
我现在正在玩C并尝试理解字符串.有人可以解释为什么这是有效的:
char test[] = "test";
Run Code Online (Sandbox Code Playgroud)
以下为什么没有?
char test[255];
test = "test";
Run Code Online (Sandbox Code Playgroud) 在facebook组中,我看到了一个类似的问题:
如果一行在下面的二维数组中占主导地位,哪一个是有利的,为什么呢?
a) for(i=0;i<1000;i++)
for(j=0;j<1000;j++)
temp=temp+a[i][j];
b) for(j=0;j<1000;j++)
for(i=0;i<1000;i++)
temp=temp+a[i][j]
Run Code Online (Sandbox Code Playgroud)
从我的观点来看,我可以看出上述两个陈述没有区别.我猜这些都是一样的.
如果我错了,请纠正我?