dispatch_after CLLocationManager didUpdateLocations或didFailWithError

时间:2014-05-18 12:38:47

标签: ios objective-c grand-central-dispatch cllocationmanager nsnotificationcenter

我有这段代码管理"拉动刷新"它会更新设备的位置:

[self.glassScrollView.foregroundScrollView addPullToRefreshWithActionHandler:^{

    int64_t delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        NSLog(@"Pulled");
        [weakSelf.locationManager startUpdatingLocation];
        [weakForegroundScrollView.pullToRefreshView stopAnimating];
    });
}];

当STARTS更新动画停止的位置时,但不是在它实际更新位置或为其返回错误时。这会产生一种尴尬的情况,它似乎已经更新了位置和UI(旋转轮动画消失了),但它仍在通过CLLocationManagerDelegate方法处理它。

那么,"连接"最好的方式是什么?这个队列的locationManager:didUpdateLocationslocationManager:didFailWithError方法? 我正在考虑添加一些"听众"在上面的代码中等待在CLLocationManagerDelegate方法中调用某个方法。

这里也是我的locationManager:didUpdateLocations方法。

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{
    NSLog(@"didUpdateLocations");
    CLLocation *currentLocation = [locations lastObject];
    // do stuff in the UI

    // I stop updating the location to save battery
    [self.locationManager stopUpdatingLocation];
}

谢谢!

2 个答案:

答案 0 :(得分:0)

确定。此代码可能有所帮助: -

-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whenCompleted) name:@"whenCompleted" object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"whenCompleted" object:nil];
}

-(void)whenCompleted{
  [weakForegroundScrollView.pullToRefreshView stopAnimating];
}

执行所有方法后,在最后一个方法结束之前,发布通知: -

[[NSNotificationCenter defaultCenter]postNotificationName:@"whenCompleted" object:nil];

然后,当进度完成时,它将停止动画

答案 1 :(得分:0)

您可以使用以下内容处理拉取请求:

[self.glassScrollView.foregroundScrollView addPullToRefreshWithActionHandler:^{

    int64_t delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        NSLog(@"Pulled");
        [weakSelf.locationManager startUpdatingLocation];
        [weakForegroundScrollView.pullToRefreshView stopAnimating];
    });
}];

我假设一秒延迟是因为你想在一秒钟之后隐藏拉动以刷新,即使刷新仍在进行中。

我建议的是:

[self.glassScrollView.foregroundScrollView addPullToRefreshWithActionHandler:^{

    [weakSelf.locationManager startUpdatingLocation];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC);, dispatch_get_main_queue(), ^(void){
        [weakSelf.glassScrollView.foregroundScrollView setContentOffset:CGPointZero animated:YES];
    });
}];

这会使UIActivityIndicatorView旋转,但会将其滚动到屏幕上。如果你再把它拉下来,你会发现它还在继续。这是一种简单的方法来保持活动指标的运行,但让用户不再这样做。

然后,正如Ricky所说,只有在检索到位置时才会stopAnimating(或向[NSNotificationCenter defaultCenter]发布本地通知,以便您的观察者可以这样做)。但是,不要只是在一段时间后尝试stopAnimating,而是将其与实际完成的任何异步任务联系起来。