我確實在做你想要使用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」塊的問題。稍後您是否有任何問題調用您的塊?看起來像:「無法恢復當前幀」 –