IOS dispatch_get_main_queue()被多次调用
我使用IOS dispatch_get_main_queue()被多次调用,ios,objective-c,objective-c-blocks,reachability,Ios,Objective C,Objective C Blocks,Reachability,我使用可达性和调度异步(调度获取主队列() 当我测试以下代码时,它可以工作,但会被多次调用 家长: @protocol RootViewDelegate <NSObject> @optional -(void)internetIsDownGoToRoot; @end - (void)testInternetConnection { internetReachableFoo = [ReachabilityTony reachabilityWithHostname:@"www.g
可达性和调度异步(调度获取主队列()
当我测试以下代码时,它可以工作,但会被多次调用
家长:
@protocol RootViewDelegate <NSObject>
@optional
-(void)internetIsDownGoToRoot;
@end
- (void)testInternetConnection
{
internetReachableFoo = [ReachabilityTony reachabilityWithHostname:@"www.google.com"];
__weak typeof(self) weakSelf = self;
// Internet is reachable
internetReachableFoo.reachableBlock = ^(ReachabilityTony *reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
[weakSelf sendLoginRequest];
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(ReachabilityTony *reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
CloudConnection *sharedInstance=[CloudConnection sharedInstance];
sharedInstance.isUserLoggedIN=NO;
//update login button
[weakSelf updateButtons];
[weakSelf notifyChild];
});
};
[internetReachableFoo startNotifier];
}
-(void)viewDidAppear:(BOOL)animated
{
[self testInternetConnection];
}
-(void)viewWillDisappear:(BOOL)animated
{
internetReachableFoo= nil;
}
//notify childs no connection come back to root
-(void) notifyChild
{
[delegate internetIsDownGoToRoot];
}
这是parentview,假设我推了5次pop childview并关闭了internet。我在nslog上看到
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
如您所见,我添加了internetReachableFoo=nil;
,但我没有更改任何内容
上面的代码是怎么回事,为什么被多次调用
使用此块可能有什么危险?它会被多次调用,因为每次您弹出子块时,根目录都会获得-viewdide:
并调用-testInternetConnection
,从而重新运行可达性测试
更新:好的,您稍微更改了您的问题。之所以会收到5条“确实消失”消息,是因为您从未停止通知程序。可访问性在通知程序运行期间保持自身的活动状态,因此消除您的引用不会杀死它。您需要明确地说[Internetreachable Foo stopNotifier]
在将其归零之前。它会被多次调用,因为每次您弹出子对象时,根对象都会获得-viewdide:
并调用-testInternetConnection
,从而重新运行可达性测试
更新:好的,您稍微更改了您的问题。之所以会收到5条“确实消失”消息,是因为您从未停止通知程序。可访问性在通知程序运行期间保持自身的活动状态,因此消除您的引用不会杀死它。您需要明确地说[Internetreachable Foo stopNotifier]
在将其归零之前。我知道这就是为什么我会在中调用它,但为什么它会一次调用5或3次,它应该在每个视图中调用一次。如果你怀疑这是真的,你可以确认(或反驳)通过在testInternetConnection
方法的第一行上放置一个断点
,然后检查Debug Navigator
选项卡(导航窗格左侧右侧第三个选项卡),此方法将调用它查看方法调用堆栈链。@MordFustang:好的,我已经更新了我的答案,以涵盖您编辑的问题。我知道这就是为什么我在didebeen
中调用它的原因,但是为什么它一次调用5或3次,它应该每viewdidebeen
调用一次如果您怀疑这是真的,您可以确认(或反驳)通过在testInternetConnection
方法的第一行上放置一个断点
,然后检查Debug Navigator
选项卡(导航窗格左侧右侧第三个选项卡),此方法将调用它查看方法调用堆栈链。@MordFustang:好的,我已经更新了我的答案以涵盖您编辑的问题。
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(
Someone broke the internet :(