您的位置:首页 > 移动开发 > IOS开发

iOS 多线程下的NStimer 的使用

2015-12-02 17:02 579 查看
                    //开启多线程一直扫描
当前wifi是否和扫描到的wifi相同
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0), ^{
                        [NSTimer
scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(isGoWebView:)
userInfo:nil
repeats:YES];
//                        do {
//                            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
//                        } while (isCurrentWifiNameEqualToScanWifiName == NO);   //当前wifi与扫码扫到的wifi不相同时
                        
                        while (isCurrentWifiNameEqualToScanWifiName ==
NO) {
//                            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
                            
//                            [runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]]; //每隔10秒检查下线程循环条件
                            [[NSRunLoop
currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate
dateWithTimeIntervalSinceNow:1.0f]];
                        }
                        
                        if (isCurrentWifiNameEqualToScanWifiName ==
YES) {
                            dispatch_async(dispatch_get_main_queue(), ^{
                                WebViewController *wVC = [[WebViewController
alloc] init];
                                [self.navigationController
pushViewController:wVC animated:YES];
                                [wVC loadDataWithUrlStr:_regularString];
                            });
                        }
                        
                    });
                    
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0), ^{
                        NSTimer *timer = [NSTimer
timerWithTimeInterval:1.0f
target:self
selector:@selector(isGoWebView:)
userInfo:nil
repeats:YES];
                        NSRunLoop *runLoop = [NSRunLoop
currentRunLoop];
                        [runLoop addTimer:timer
forMode:NSDefaultRunLoopMode];
                        while (isCurrentWifiNameEqualToScanWifiName ==
NO) {
                            [runLoop runUntilDate:[NSDate
dateWithTimeIntervalSinceNow:1.0f]];
                        }
                        
                        if (isCurrentWifiNameEqualToScanWifiName ==
YES) {
                            dispatch_async(dispatch_get_main_queue(), ^{
                                WebViewController *wVC = [[WebViewController
alloc] init];
                                [self.navigationController
pushViewController:wVC animated:YES];
                                [wVC loadDataWithUrlStr:_regularString];
                            });
                        }
                    });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: