NSOperationQueue setSuspended:NO无法启动操作备份

时间:2014-02-26 03:08:39

标签: ios iphone objective-c concurrency afnetworking

我遇到了一个问题,我正在通过打开和关闭飞机模式来测试我的应用程序以处理间歇性或片状连接。

基本上我需要排队来自用户的提交请求并同步执行此操作,如果请求失败将其从队列中取出,则告诉用户它失败然后执行下一步。每次添加操作可以多次,也可以一次添加1次,无论他们是否需要排队并一次执行一次。

直到我第3次或第4次切断连接时才能正常工作,一旦我这样做,NSOperationQueue的{​​{1}}就不会继续执行,即使我确认他们正坐着在AFHTTPRequestOperations。我有一个自定义任务类,它将NSOperationQueue作为属性,并在其惰性初始化程序中构建请求。

以下是我在代码中基本上做的事情:

我的自定义类包含任务和管理器(因此请求可以在惰性启动器中创建自己)。

AFHTTPRequestOperation

操作管理器自定义类中的代码,用于添加从viewcontrollers发送的任务:

@interface MyTask : NSObject

@property (nonatomic, strong) AFHTTPRequestOperation *request;
@property (nonatomic, strong) AFHTTPRequestOperationManager *manager;
@property (nonatomic, strong) NSString *name;

@end

以下是我处理在线离线暂停和恢复队列的方法:

- (void)addNewRequest:(MyTask *)task {

   [self.operationQueue addOperation:task.request];    
}

我已经确认它会进入简历案例,并且通过发送operationCount方法对operationQueue进行操作。但无论出于何种原因,这个过程在经过几次上线和下线后都会停止。

我还在opQueue上将maxConcurrency设置为1以强制同步行为。

__block NSOperationQueue *operationQueue = self.operationQueue;
        __weak typeof(self) weakSelf = self;
        [self.operationRequestManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

            __strong typeof(weakSelf) strongSelf = weakSelf;

            switch (status) {
                case AFNetworkReachabilityStatusNotReachable:
                    [strongSelf.networkHelper setNetworkActivityIndicatorVisible:NO];
                    [operationQueue setSuspended:YES];

                    break;
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    [strongSelf.networkHelper setNetworkActivityIndicatorVisible:YES];
                    [operationQueue setSuspended:NO];

                    break;
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    [strongSelf.networkHelper setNetworkActivityIndicatorVisible:YES];
                    [operationQueue setSuspended:NO];

                    break;
                default:
                    [strongSelf.networkHelper setNetworkActivityIndicatorVisible:NO];
                    [operationQueue setSuspended:YES];

                    break;
            }
        }];

        [self.operationRequestManager.reachabilityManager startMonitoring];

ETA:尝试将operationQueue设置为atomic,以查看它是否是一个线程问题而且没有解决它。

0 个答案:

没有答案
相关问题