当互联网连接不良时,viewWillAppear和viewDidAppear之间的延迟

时间:2017-03-22 08:29:34

标签: ios

我的应用程序存在问题:当互联网连接不良时,viewWillAppear的结尾与viewDidAppear的开头之间会出现延迟(约4秒)。但是当互联网正常时,问题就不会发生!

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [_btnMenu setBadgeTextColor:[UIColor whiteColor]];
    [self updateNotification];
    [_btnMenu setBadgeEdgeInsets:UIEdgeInsetsMake(15, 0, 0, 8)];
    [_btnMenu setHideWhenZero:YES];
    [self setNeedsStatusBarAppearanceUpdate];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [WSProgressHUD dismiss];        
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [super setUpLocalizationString];
    willRoute = NO;

    // delay single tap recognition until it is clearly not a double
    [_mapBoxView removeGestureRecognizer:taptap];
    [_mapBoxView removeGestureRecognizer:doubleTap];

    doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
    doubleTap.numberOfTapsRequired = 2;
    [self.mapView addGestureRecognizer:doubleTap];

    taptap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissCallout:)];
    [taptap requireGestureRecognizerToFail:doubleTap];
    [taptap requireGestureRecognizerToFail:_longPress];
    taptap.delegate = self;
    [self.mapBoxView addGestureRecognizer:taptap];
    GoongInstance.mapBoxView = _mapBoxView;


    self.navigationController.navigationBarHidden = YES;
    [self.view layoutIfNeeded];
    [self.view updateConstraints];
    _searchTextField.delegate = self;
    [_searchTextField resignFirstResponder];
    if (!isPresentingOtherVC) {
        _radialMenuYConstraint.constant = -70;
        _dropPinViewHeightConstraint.constant = 0;
        _btnWarning.hidden = YES;

        [_mapBoxView setScrollEnabled:YES];
        [_mapBoxView setZoomEnabled:YES];
        [_mapBoxView setRotateEnabled:YES];
        [_mapBoxView setPitchEnabled:YES];
        [_mapBoxView removeFromSuperview];
        [self.mapView addSubview:_mapBoxView];
        _mapBoxView.frame = CGRectMake(0, 0, _mapView.frame.size.width, _mapView.frame.size.height);
        _mapBoxView.delegate = self;
        [self.view layoutIfNeeded];
    }

}

2 个答案:

答案 0 :(得分:2)

我确信,您正在阻止应用程序执行的主线程上执行某些操作(例如Internet检查或任何其他网络操作)。做互联网检查后台线程。

<强>目标C

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // DO the internet checking or any other network operations here
});

Swift 3.0

DispatchQueue.global(qos: .background).async {
    print("This is run on the background queue")
    // DO the internet checking or any other network operations here
}

答案 1 :(得分:0)

请使用仪器找出哪些花费更多时间。如果基于互联网的东西,然后首先检查互联网连接,然后去基于互联网的声明(如果poosble在背景上)

检查互联网:

https://cocoapods.org/pods/Reachability

相关问题