NSTimer被打断了

时间:2014-08-20 21:06:45

标签: ios objective-c

摘要:我正在开发一个iOS应用,需要登录Web应用程序中托管的帐户。该帐户在Web应用程序中创建和维护(通过Web界面,而不是iOS应用程序),并且可以在iOS应用程序开始使用该帐户后更改或删除。因此,我需要定期验证该帐户在Web应用程序中是否仍处于活动状态。我使用NSUserDefaults在iOS应用中存储deviceID(唯一的帐号),并希望将此值与硬件唯一ID一起使用,以确认iOS设备仍与活动帐户相关联。

我的问题:如果我实施了verifyDeviceConfiguration方法,那么在用户登录时保持数据新鲜的NSTimers会停止工作。

更多细节: 登录发生在我的WelcomeViewController中,它使用storyboardSegue打开显示内容的MainViewController。这是我的MainViewController的ViewDidLoad中的一个NSTimers的示例:

getWeather = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(startFetchingWeather:) userInfo:nil repeats:YES];

以下是我为验证iOS设备仍与有效帐户相关联而实施的代码。它位于WelcomeViewController:

-(void)verifyDeviceConfiguration:(NSNotification *)notification
{
    [_uManager validateDevice:[[NSUserDefaults standardUserDefaults] valueForKey:@"deviceID"] deviceNumber:[SKFunctions identifierForVendor1]];
}

-(void)didReceiveValidationConfirmation:(NSArray *)message
{   
    if([message count] == 1)
    {
        for (NSArray *currentMessage in message)
        {
            // fails to validate
            if([currentMessage valueForKey:@"loginError"] != nil)
            {
                // empty device and location
                [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"deviceID"];

            // request user pair the device again
            NSString *message = @"There was an error validating this device. Please pair the device again.";
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Validation Error" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alertView showWithCompletion:^(UIAlertView *alertView, NSInteger buttonIndex)
             {
                 [self viewDidAppear:YES];
             }];

        }
        // successfully validates
        else
        {
            // initiate the seque to the device display
            [self performSegueWithIdentifier:@"registeredUserSegue" sender:nil];
        }
    }
}
else
{
    NSLog(@"This should never happen");


  }
}

-(void)deviceValidationFailedWithError:(NSError *)error
{
    NSLog(@"Device validation error");
}

并且,有使用NSURLConnection的代码从服务器获取响应,提供帐户确认。该代码如下:

-(void)validateDevice:(NSNumber *)deviceID deviceNumber:(NSString *)deviceNumber
{
    [self.communicator validateDevice:deviceID deviceNumber:deviceNumber];
}

-(void)receivedValidationConfirmationJSON:(NSData *)objectNotation
{
    NSString *decodedString = [[NSString alloc] initWithData:objectNotation encoding:NSUTF8StringEncoding];
    NSLog(@"Decoded Validation Confirmation: %@", decodedString);

    NSError *error = nil;
    NSArray *validationMessage = [SKLoginBuilder deviceValidationConfirmationJSON:objectNotation error:&error];

    if(error != nil) {
        [self.delegate deviceValidationFailedWithError:error];
    } else {
        [self.delegate didReceiveValidationConfirmation:validationMessage];
    }
}    

-(void)validateDevice:(NSNumber *)deviceID deviceNumber:(NSString *)deviceNumber
{
    NSString *urlAsString = [NSString stringWithFormat:@"%@/%@&d=%@&n=%@&apiKey=%@", API_URL, @"validation", deviceID, deviceNumber, API_KEY_LOGIN];
    NSLog(@"Device Validation URL: %@", urlAsString);

    BOOL validUrl = [SKFunctions validateURL:urlAsString];

    if(validUrl)
    {
        NSURL *url = [[NSURL alloc] initWithString:urlAsString];

        if([NSURL URLWithString:urlAsString] != nil)
        {
            [NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                if(error) {
                    [self.delegate deviceValidationFailedWithError:error];
                } else {
                    [self.delegate receivedValidationConfirmationJSON:data];
                }
            }];
        }
    }
    else
    {
        //error handing for poorly formatted url
        UIAlertView *urlFailure = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was an error validating your tablet. Please logout and login again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
        [urlFailure show];
        }
    }

如果我关闭verifyDeviceConfiguration,一切正常...... NSTimers会触发,数据会刷新。如果我打开verifyDeviceConfiguration,MainViewController将加载,它将填充数据一次(直接调用数据的初始加载,而不是使用NSTimers),并且NSTimers不会触发和刷新数据。

最后,我会处理NSLog语句。

想法?感谢。

1 个答案:

答案 0 :(得分:0)

我生活和学习!我在以下特定事件之后使定时器无效,然后再次尝试运行定时器。真的我想要/需要的是暂停计时器......我明白不能这样做。所以,我采取了不同的方法,一切都很好。

[getTime invalidate];

感谢。