Tag: nstimer

iPhone 4有一个绝对确定的方式来长期NSTimer火灾

我一直有我的NSTimers和背景select器的麻烦。 这是让我疯狂,需要很长时间来尝试每一个调整。 为了保持我的理智,以及后代cocoa程序员的理智,我问了这个问题: 有一个绝对100%的可靠的方法来让一个定时的,长期的计时器在晚些时候触发,而不pipe它是从后台线程,主线程等调用的吗? 似乎我一直都不得不一遍一遍地解决使用NSTimers的大多数类的相同的问题。 他们在短期testing期间工作,比方说,我设置定时器通过后台线程在10秒内触发。 它工作,因为仍然有运行循环运行。 但是,一旦我把火灾时间改变到了我真正想要的时间,比如15-30分钟,就有了沉默。 运行循环已经结束,我不知道如何处理这种情况。 什么都没有发生,几天后我发现了这样的错误,一旦我已经忘记了哪个计时器会对此负责。 目前,我正在用select器做一些非常非常难看的舞蹈,例如这里有一个testing方法(它似乎适用于10分钟的定时器): //this is a test method to simulate a background task requesting a timer [self performSelectorInBackground:@selector(backgroundReminderLongTermTest:) withObject:nil]; //this is a method similar to the one that the background thread would be trying to invoke -(void)backgroundReminderLongTermTest:(id)sender { [self performSelectorOnMainThread:@selector(backgroundReminderFromMainThread:) withObject:nil waitUntilDone:NO]; } //this is a wrapper […]

方法不调用dispatch_async并重复NSTimer

我正在开发一个应用程序,我想使用dispatch_async在单独的队列中调用方法。 我想在一定的时间间隔后反复调用这个方法。 但是这个方法没有被调用。 我不知道什么是错的。 这是我的代码: dispatch_async( NotificationQueue, ^{ NSLog(@"inside queue"); timer = [NSTimer scheduledTimerWithTimeInterval: 20.0 target: self selector: @selector(gettingNotification) userInfo: nil repeats: YES]; dispatch_async( dispatch_get_main_queue(), ^{ // Add code here to update the UI/send notifications based on the // results of the background processing }); }); -(void)gettingNotification { NSLog(@"calling method "); }

iOS不是典型的后台位置跟踪计时器问题

我正在开发一个示例应用程序,跟踪用户在后台的位置,但我不想离开位置服务始终启用,但在我的计时器中的某些行为不正确。 我的想法是,每x分钟,服务继续,当它有一个正确的新位置,它被释放,现在被设置为10秒,只是为了testing。 (显着位置变化没有窍门,没有足够的指责) 我search了很多(iOS开发人员中心+ StackOverflow),并find了“新的”后台位置function,它允许您在使用beginBackgroundTaskWithExpirationHandler,几个模块和一个计时器后,在运行后10分钟后运行代码。 我将背景模式设置为“位置”,现在我认为我不需要处理背景时间的结束(首先,我想每15-20秒获取一个位置) 代码工作“很好”,但是: 定时器有时会发生,有时不会。 计时器启动时,至less需要10分钟才能完成。 在操作系统中的一些随机操作(如进入search桌面)似乎估计计时器开火(不知道这一点,我不知道如何可能,但它是…) 通过代码将是另一个问题。 应用程序的方法: // applicationDidEnterBackground – (void)applicationDidEnterBackground:(UIApplication *)application{ NSLog(@"to background"); 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(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task. _triggertimer = […]

如何知道何时使`NSTimer`无效

这是我的问题: 我有一个模型类,其中有一个NSTimer ,我希望计时器运行模型对象的整个生命周期。 初始化很简单:我只是在init方法中有以下代码行: self.maintainConnectionTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(maintainConnection) userInfo:nil repeats:YES]; 但是,我的问题是, 当模型从内存中释放时 , 如何使这个计时器无效? 现在,这通常很容易,但是,据我所知,当您计划一个NSTimer ,操作系统维护一个强指针到Timer对象。 我该如何处理? 有没有一种方法在模型从内存中释放之前被调用?

在iPhone中创build一个秒表

我正在尝试创build一个简单的秒表。 我提到这个网站( http://www.apptite.be/tutorial_ios_stopwatch.php )做这个应用程序。 当我点击开始button,我得到-596:-31:-23:-648秒表不运行。 代码如下所示: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController{ UILabel *lbl; NSTimer *stopTimer; NSDate *startDate; } @property (strong,nonatomic) IBOutlet UILabel *lbl; -(IBAction)startPressed:(id)sender; -(IBAction)stopPressed:(id)sender; -(void)updateTimer; @end ViewController.m #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize lbl; – (void)viewDidLoad { [super viewDidLoad]; lbl.text = @"00.00.00.000"; } – (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // […]

当应用程序进入后台时如何保持NSTimer?

我在这里,因为没有find我的问题的任何解决scheme:( 我正在做一个简单的应用程序,我必须发送(通过套接字)一些信息到服务器(如GPS升/ L,准确性,电池电量等)。 当应用程序在前台时,当前的代码工作正常。 myTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target:self selector: @selector(sendPosToServer:) userInfo:nil repeats: YES]; myTimer2 = [NSTimer scheduledTimerWithTimeInterval: 3.0 target:self selector: @selector(sendBatteryToServer:) userInfo:nil repeats: YES]; myTimer3 = [NSTimer scheduledTimerWithTimeInterval: 5.0 target:self selector: @selector(sendResToServer:) userInfo:nil repeats: YES]; myTimer4 = [NSTimer scheduledTimerWithTimeInterval: 5.0 target:self selector: @selector(sendQResToServer:) userInfo:nil repeats: YES]; myTimer5 = [NSTimer scheduledTimerWithTimeInterval: 3.0 target:self selector: […]

我如何启动和停止NSTimer?

我开发了Stop Watch应用程序。 在我的应用程序中,有两个UIButtons, StartBtn和StopBtn ,并且还使用了NSTimer 。 现在,我想在用户点击StartBtn时启动NSTimer ,并且在StopBtn单击时也停止。 我知道NSTimer被[MyTimerName invalidate];停止[MyTimerName invalidate]; 方法,但我不知道如何再次启动NSTimer ?

CountDown Timer ios教程?

我正在做一个考试正在进行的应用程序,所以考试开始时,应该从那开始。 例如30分钟,应该像29:59那样减less。 我怎样才能实现呢? 任何人都可以给我一个示例或简单的一步教程,我可以遵循吗?

如何在音乐播放器中制作倒数计时器?

我有一个标签,它的值是“03:48”。 我想像一个音乐播放器倒计时。 我怎样才能做到这一点? 03:48 03:47 03:46 03:45 … 00:00 var musictime =3:48 func stringFromTimeInterval(interval: NSTimeInterval) -> String { let interval = Int(interval) let seconds = interval % 60 let minutes = (interval / 60) return String(format: "%02d:%02d", minutes, seconds) } func startTimer() { var duration=musictime.componentsSeparatedByString(":") //split 3 and 48 var count = duration[0].toInt()! * 60 […]

使计时器在后台iOS上运行超过3分钟

所以我需要这个应用程序运行超过3分钟的计时器,并播放声音非常45秒,这里的大多数解决scheme只是不到3分钟在iOS上。 除非应用程序停止,否则有办法让它始终运行吗? 经过研究,我猜解决scheme是实现beginBackgroundTaskWithExpirationHandler 并通过在plist上给予一个handler并设置位置更新来停止它? 至于audio,除了放置plist,还有其他什么需要做audio播放? Swift如何使用NSTimer背景? iphone – NSTimers在后台在后台 运行应用程序超过10分钟