我正在开发一个iPAD应用程序,我希望每隔10秒自动将表单内容保存到SQLITE中。现在,如果我按下保存按钮,它将保存到数据库。有没有办法每10-15秒自动保存我在表格中写的东西。帮助我解决这个问题。
答案 0 :(得分:1)
使用NSTimer并每x分钟执行一次保存。代码看起来像这样。它是代码here的修改版本。
@interface MyController : UIViewController
{
@private
NSTimer * countdownTimer;
NSUInteger remainingTicks;
}
-(IBAction)doCountdown: (id)sender;
-(void)handleTimerTick;
-(void) saveData;
@end
@implementation MyController
// { your own lifecycle code here.... }
-(IBAction)doCountdown: (id)sender
{
if (countdownTimer)
return;
remainingTicks = 60;
[self saveData];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(handleTimerTick) userInfo: nil repeats: YES];
}
-(void)handleTimerTick
{
remainingTicks--;
[self updateLabel];
if (remainingTicks <= 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void) saveData
{
//Save your data here
}
@end