修复我的网络活动指示器 [英] Fixing my network activity indicator

查看:83
本文介绍了修复我的网络活动指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络活动指示器有问题,有时它会在不应该显示时继续显示.

I have a problem with my network activity indicator in that sometimes it will continue to be displayed when it should not be.

我为此写了我自己的经理,然后换成使用这样的NSAssert语句的经理...

I wrote my own manager for it and swapped it out for one that uses an NSAssert statement like this...

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

    // The assertion helps to find programmer errors in activity indicator management.
    // Since a negative NumberOfCallsToSetVisible is not a fatal error,
    // it should probably be removed from production code.
    NSAssert(NumberOfCallsToSetVisible >= 0, @"Network Activity Indicator was asked to hide more often than shown");

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

我在SO上找到它,并立即指出我使用此功能时出现了问题.

I found it on SO and it has immediately pointed out that something is going wrong with my use of this function.

我所有的网络活动都完全通过单个NSOperationQueue运行,该单个c1由单例类管理.每个操作都是NSOperation的子类(实际上是TemplateOperation的子类,TemplateOperation是NSOperation的子类).

All of my network activity is exclusively run through a single NSOperationQueue which is managed by a singleton class. Every operation is a subclass of NSOperation (actually a subclass of a TemplateOperation which is a subclass of NSOperation).

无论如何,所有的下载和上传都工作正常,我正在像这样进行所有操作...

Anyway, all the downloads and uploads are working fine and I'm doing them all like this...

- (void)sendRequest:(NSURLRequest *)request
{
    NSError *error = nil;
    NSURLResponse *response = nil;

    [[NetworkManager sharedInstance] setNetworkActivityIndicatorVisible:YES];
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    [[NetworkManager sharedInstance] setNetworkActivityIndicatorVisible:NO];

    // other stuff...

    [self processData:data];
}

重要的几行紧接在我同步发送NSURLConnection之前和之后.

The important lines are immediately before and after I send the NSURLConnection synchronously.

在发送请求之前,我立即将网络活动指示器设置为可见(使用我的经理类),然后在将其设置为不可见之后立即设置.

Immediately before I send the request I set the network activity indicator to visible (using my manager class) and then immediately after I set it back to invisible.

除了NSAssert指出,在某些地方这无法正常发生.

Except the NSAssert has pointed out that somewhere this is not happening properly.

是否可能是从多个线程运行此功能导致问题?我该怎么解决?

Could it be that running this function from multiple threads could be causing an issue? How could I solve this?

推荐答案

整数递增或递减不是线程安全的(据我所知),因此,如果两个线程同时"调用您的方法,则计数可能不会获得已正确更新.

Integer increment or decrement is not thread-safe (as far as I know), so if two threads call your method "simultaneously", the count might not get updated properly.

一种解决方案是添加一些同步指令(例如@synchronized) 根据您的方法.或者,您可以使用原子增量/减量功能:

One solution would be to add some synchronization directive (such as @synchronized) to your method. Or you use the atomic increment/decrement functions:

#include <libkern/OSAtomic.h>

- (void)setNetworkActivityIndicatorVisible:(BOOL)setVisible {
    static volatile int32_t NumberOfCallsToSetVisible = 0;
    int32_t newValue = OSAtomicAdd32((setVisible ? +1 : -1), &NumberOfCallsToSetVisible);

    NSAssert(newValue >= 0, @"Network Activity Indicator was asked to hide more often than shown");
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(newValue > 0)];
}

这篇关于修复我的网络活动指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆