我确实在做你想要使用NSOperationQueue
的目标。首先,创建一个串行队列,默认情况下将其挂起:
self.operationQueue = [[[NSOperationQueue alloc] init] autorelease];
self.operationQueue.maxConcurrentOperationCount = 1;
[self.operationQueue setSuspended:YES];
然后,创建可达性实例,并注册了kReachabilityChangedNotification
:
[[NSNotificationCenter defaultCenter] addObserver:manager
selector:@selector(handleNetworkChange:)
name:kReachabilityChangedNotification
object:nil];
[self setReachability:[Reachability reachabilityWithHostName:@"your.host.com"]];
[self.reachability startNotifier];
现在,启动和停止队列当网络状态的变化:
:
-(void)handleNetworkChange:(NSNotification *)sender {
NetworkStatus remoteHostStatus = [self.reachability currentReachabilityStatus];
if (remoteHostStatus == NotReachable) {
[self.operationQueue setSuspended:YES];
}
else {
[self.operationQueue setSuspended:NO];
}
}
你可以用你的排队块3210
暂停队列只会阻止操作启动 - 它不会挂起正在进行的操作。在执行操作时,您总会有可能失去网络,因此您应该在操作中说明这一点。
我正在尝试这样的方法,但当连通性返回时,我遇到了“超出范围的dealloc'd”块的问题。稍后您是否有任何问题调用您的块?看起来像:“无法恢复当前帧” –