0

这是我到目前为止所尝试的:

- (void)applicationWillResignActive:(UIApplication *)application
{

     timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(triggerTimer:) userInfo:nil repeats:FALSE];
     NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
     [runLoop addTimer:timer forMode:NSRunLoopCommonModes];
     [runLoop run];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    if (timer && [timer isValid]) {
        [timer invalidate];
     }
}

我的问题是,如果我使计时器无效,runloop 仍在运行并冻结我的 UI(动画不工作,滚动不工作等)。有什么想法我怎么能做到这一点?

提前致谢!

4

1 回答 1

1

你不应该创建一个 timer applicationWillResignActive。相反,您应该将当前日期/时间保存在applicationDidEnterBackground.

// Not sure how you are keeping session information
// You can use a variable to store session id
// or simple keep a bool to indicate session is valid
// In this example, let say I just keep a session BOOL

- (void)applicationDidEnterBackground:(UIApplication *)application {
   // save the save the app enters background
   backgroundTime_ = [NSDate date];        
}

// In this example I am going to check if my session is valid in two stages
// You can do it in one stage if you like
- (void)applicationWillEnterForeground:(UIApplication *)application {
   // I only need to do a time-out check if I have a valid session
   if (isValidSession_ && backgroundTime_)
   {
       // get the number of second since we entered background
       NSTimeInterval span = [backgroundTime_ timeIntervalSinceNow];
       if (span > (15 * 60))
       {
           isValidSession_ = NO;      
       }
   }

}

// This is wheer the magic occurs
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // check if session is still valid
    if (!isValidSession_)
    {
        // Load the login view
    }
}
于 2012-12-14T21:20:23.750 回答