保持网络活动指标旋转

时间:2012-07-19 15:05:11

标签: ios

我发现有时你可能会在你的iPhone应用程序中做一些事情,要求用户在完成时等待。这通常是与网络相关的活动,但在其他情况下可能不是。在我的情况下,我正在解析来自网络连接的响应,并希望网络活动指示器保持旋转,即使它已经下载了内容。

以下是我正在做的事情:

applicationDelegate.m:

- (void)setNetworkActivityIndicatorVisible:(BOOL)setVisible
{
    static NSInteger NumberOfCallsToSetVisible = 0;
    if (setVisible) 
        NumberOfCallsToSetVisible++;
    else 
        NumberOfCallsToSetVisible--;

    // Display the indicator as long as our static counter is > 0.
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(NumberOfCallsToSetVisible > 0)];
}

otherView.m:

dispatch_queue_t dataLoadingQueue = dispatch_queue_create("synchronise", NULL);
    dispatch_async(dataLoadingQueue,
                   ^{
                       [appDelegate setNetworkActivityIndicatorVisible:YES];
                       [[DataLoader instance]LoadDataForGrewal];
                       [[FieldConsultantViewModelManager instance] resetCache];  
                       [[DailyFieldConsultantViewModelManager instance] clearCache];
                       [appDelegate loadMainViews];
                       [[DataLoader instance]LoadDataForOtherEntities]; 
                       [appDelegate setNetworkActivityIndicatorVisible:NO];
                   });    
 dispatch_release(dataLoadingQueue);

正如你可以看到的,我正在尝试在将数据更新到数据库时保留网络指示器,但它不起作用,任何线索/建议?

由于 编辑:

dispatch_queue_t dataLoadingQueue = dispatch_queue_create("synchronise", NULL);
    dispatch_async(dataLoadingQueue,
                   ^{
                       dispatch_async(dispatch_get_main_queue(), ^{ [self setNetworkActivityIndicatorVisible:YES]; });
                       [[DataLoader instance]LoadDataForGrewal];
                       [[FieldConsultantViewModelManager instance] resetCache];  
                       [[DailyFieldConsultantViewModelManager instance] clearCache];
                       [appDelegate loadMainViews];
                       [[DataLoader instance]LoadDataForOtherEntities]; 
                       dispatch_async(dispatch_get_main_queue(), ^{ [self setNetworkActivityIndicatorVisible:NO]; });
                   });    
 dispatch_release(dataLoadingQueue);

它不起作用我不知道为什么因为我是ios的新手

2 个答案:

答案 0 :(得分:2)

尝试在主队列上调度setNetworkActivityIndicatorVisible:调用,因为UIApplication在UIKit中,UIKit不是线程安全的。

答案 1 :(得分:1)

对于寻找完整解决方案的人来说,这就是我使用的(在 Swift 中)。停止网络指示时,它会延迟很短的持续时间。如果您有大量串行网络请求,这将防止指示灯闪烁。

private var networkActivityCount = 0

func updateNetworkActivityCount(increaseNumber increase:Bool)
{
    let appendingCount = increase ? 1 : -1
    networkActivityCount += appendingCount
    let delayInSeconds = increase ? 0.0 : 0.7
    perform({ () -> () in
        let application = UIApplication.sharedApplication()
        let shouldBeVisible = self.networkActivityCount > 0
        if application.networkActivityIndicatorVisible != shouldBeVisible {
            application.networkActivityIndicatorVisible = shouldBeVisible
        }
    }, afterDelay: delayInSeconds)
}

perform:afterDelay定义为

public func perform(block: ()->(), afterDelay:Double) {
    let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(afterDelay * Double(NSEC_PER_SEC)))
    dispatch_after(dispatchTime, dispatch_get_main_queue(), {
         block()
    })
}

只需在您的请求之前调用updateNetworkActivityCount(increaseNumber: true),并在请求回调中使用false调用updateNetworkActivityCount(increaseNumber: true) let task = urlSession.dataTaskWithRequest(request) { data, response, error in self.updateNetworkActivityCount(increaseNumber: false) ... 。即:

  setInterval(function(){
           $('#show_products').load('products.php');
       },3000);