Nstimer count wrong

我只是一个虾纸丫 提交于 2019-12-11 20:17:03
问题 I have a problem with NSTimer. I start and repeat it every 100ms and I have a counter to check if i runed 10s. The problem is it stoped in near 1 minute, not in 10s. Here is my code timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkTimer) userInfo:nil repeats:YES]; -(void)checkTimer { timerCount += 1; if(timerCount == 1) { NSLog(@"start"); } if(timerCount == 103) { NSLog(@"i got it"); } } here is the log: 2012-11-19 08:57:42.063 Karagram[11708:540b] start

How to call a method every nth minute while app is in background?

旧街凉风 提交于 2019-12-11 19:44:47
问题 Hello everyone I need to call a method when the app is in background. This is now i am trying to achieve this but the timer is never called. - (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication* app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue

Using NSTimer to play sound at set intervals, not working in background Xcode6.4

元气小坏坏 提交于 2019-12-11 16:29:30
问题 I'm brand new to writing apps. Just wrote my first app for training for a half marathon. It is a run/walk timer that plays a "Run" or "Walk" sound when it's time to switch activity mode, users select those intervals. The app works fine on my phone when it's active, but stops in the background. I searched through a lot of posts on Stackflow, and many said you should not use NStimer in the background. Would love some help is getting this to work in the background. Thanks in advance. I'm using

How to update UICollectionViewCell's UILabel every second (swift)

允我心安 提交于 2019-12-11 16:23:59
问题 I have a UICollectionViewCell with a UILabel showing the day difference between two dates. I want to update it every second. I have tried using NSTimer to reload the data of the UICollectionview , but it makes the UI very slow. How do I go about achieving this? Thanks. 回答1: You do not reload the whole CollectionView or even the individual cells just to update the label. You have 2 options: Write a NSTimer in the ViewController and just refresh the content of the visible cells. Schedule a

ios8 Swift SpriteKit - Pause and Resume NSTimers in swift

我与影子孤独终老i 提交于 2019-12-11 13:20:01
问题 I have searched many times on the internet but could not find the answer to this question. I know how to pause and resume NSTimers by using the invalidate functions - timer.invalidate. and I know how to resume them. But I have a SpriteKit game. When I pause my game, I stop everything and the timers. I know that I can stop them using .invalidate but when I invalidate them: For example lets say I have a 5 second timer that runs continously that spawns one block. When the timer reaches second 3

How to stop a thread created with dispatch_async?

守給你的承諾、 提交于 2019-12-11 13:18:48
问题 I want to forcibly stop the thread created by dispatch_async if its in use for too much time, for example pass over 5 minutes. By searching over the internet, I got some one thought there was no way to stop the thread, does any one know that? In my imagine, I want to create a NSTimer to stop the thread when time specified passed. + (void)stopThread:(NSTimer*)timer { forcibly stop the thread??? } + (void)runScript:(NSString *)scriptFilePath { [NSTimer scheduledTimerWithTimeInterval:5*60 target

Timer when leaving app and returning on ios

大兔子大兔子 提交于 2019-12-11 11:28:11
问题 Currently on my iOS app, when the user exits to the home screen and goes back into the app, it requests a login credentials which is set in my AppDelegate. But what I am trying to do is if the user goes out of the app and back in within for example 2 minutes, the timer resets and the user does not need to input his password. When the user goes back into the app after 2 minutes, it will alert him to input the password again. Any help will be greatly appreciated. Thanks! 回答1: Use NSUserDefaults

How to add background infinite timer with background mode BLE Central in IOS?

情到浓时终转凉″ 提交于 2019-12-11 10:37:29
问题 I want to keep my app can fire timer at specific time even when in background,now my app use BLE Central background mode, and I need to send some data to blue peripheral at a specific time, how to implement this? Now the timer selector seems can only be fired when enter back to foreground. I use Xcode 6.3, tested both on iOS 8.1.3 and iOS 8.3. 回答1: But the timer selector seems can only be fired when app become foreground again. Should I use dispatch_after instead of nstimer?But the dispatch

NSTimer bad access

痞子三分冷 提交于 2019-12-11 09:00:46
问题 I'm trying to create an NSTimer, then invalidate and release it, then set it to a new timer. However, I am getting an EXC_BAD_ACCESS when trying to set the timer member var again. Code below: 1) I set the timer member var (it's set to retain) self.mPageTimer = [NSTimer scheduledTimerWithTimeInterval:kPageTimerInterval target:self selector:@selector(pageTimerCallback) userInfo:nil repeats:NO]; 2) I let it go [mPageTimer invalidate]; [mPageTimer release]; This results in the crash when I try to

while loop using NSTimer

久未见 提交于 2019-12-11 08:14:40
问题 I want to create a NSTimer that runs for lets say 10 minutes. I then want to write a while loop aftewards delaying 10 minutes of time before the line afterwards is executed. For example. NSTimer * countDown = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector userInfo:nil repeats:NO]; while (countDown == stil running (hasnt reached 10 minute mark) ){ // kill 10 minutes of time }//when 10 minutes is up execute next line of code 回答1: First The timeInterval parameter of