多重连接减缓视图创建

时间:2013-12-13 05:59:32

标签: ios uiview uialertview multipeer-connectivity

我有一个使用Multipeer Connectivity的应用程序,当一个设备向组中的其他设备发送数据时,所有其他设备应该在其屏幕上收到数据发送给他们的通知,然后他们可以选择是否或不接受它。无论我做什么,我一次又一次地运行的问题是,通知视图至少需要五秒才能显示,而且大多数时间更长(我甚至用自己的快速和粗略替代UIAlertView - 如图所示注释 - 如果UIAlertView是问题,它没有解决任何问题)。我在此API的session:didReceiveData:fromPeer方法中运行了通知代码。而我的NSLog在UIAlertView的上方和下方以及我的原始替换都快速连续显示,它只需要永远显示出来。有任何想法吗?这是我的代码:

-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
    NSLog(@"DATA RECEIVED: %lu bytes!", (unsigned long)data.length);
    dataReceived = data;
    receivedDataDict = [[NSMutableDictionary alloc] init];
    NSLog(@"Initializing ReceivedDataDict");
    receivedDataDict = [NSKeyedUnarchiver unarchiveObjectWithData:dataReceived];
    NSLog(@"ReceivedDataDict is Unarchived");

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Hey!"
                                               message: [[NSString alloc] initWithFormat:@"%@ is trying to send you data! Do you accept?", [peerID displayName]]
                                              delegate: self
                                     cancelButtonTitle:@"Nuh Uh!!"
                                     otherButtonTitles:@"Ok, I guess.",nil];
    [alert show];

//    [self.view addSubview:greyOut];
//    
//    CGRect headsUpViewRect = CGRectMake(236, 400, 300, 200);
//    headsUpView = [[UIView alloc] initWithFrame:headsUpViewRect];
//    headsUpView.backgroundColor = [UIColor whiteColor];
//    headsUpView.layer.cornerRadius = 10;
//    
//    CGRect headsUpTitleLblRect = CGRectMake(20, 20, 260, 30);
//    UILabel *headsUpTitleLbl = [[UILabel alloc] initWithFrame:headsUpTitleLblRect];
//    headsUpTitleLbl.text = [[NSString alloc] initWithFormat:@"%@ is trying to send you matches!", [peerID displayName]];
//    headsUpTitleLbl.font = [UIFont systemFontOfSize:15];
//    headsUpTitleLbl.textAlignment = NSTextAlignmentCenter;
//    [headsUpView addSubview:headsUpTitleLbl];
//    
//    CGRect headsUpSubTitleLblRect = CGRectMake(50, 60, 200, 30);
//    UILabel *headsUpSubTitleLbl = [[UILabel alloc] initWithFrame:headsUpSubTitleLblRect];
//    headsUpSubTitleLbl.text = @"Do you accept their gift?";
//    headsUpSubTitleLbl.font = [UIFont systemFontOfSize:10];
//    headsUpSubTitleLbl.textAlignment = NSTextAlignmentCenter;
//    [headsUpView addSubview:headsUpSubTitleLbl];
//    
//    CGRect confirmBtnRect = CGRectMake(200, 120, 80, 50);
//    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//    [confirmBtn addTarget:self action:@selector(headsUpViewClickedButtonAtIndex1) forControlEvents:UIControlEventTouchUpInside];
//    confirmBtn.frame = confirmBtnRect;
//    [confirmBtn setTitle:@"Of Course!" forState:UIControlStateNormal];
//    confirmBtn.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
//    [headsUpView addSubview:confirmBtn];
//    confirmBtn.layer.cornerRadius = 5;
//    
//    CGRect cancelBtnRect = CGRectMake(20, 120, 80, 50);
//    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//    [cancelBtn addTarget:self action:@selector(cancelHeadsUpView) forControlEvents:UIControlEventTouchUpInside];
//    cancelBtn.frame = cancelBtnRect;
//    [cancelBtn setTitle:@"Nuh Uh!!" forState:UIControlStateNormal];
//    cancelBtn.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
//    [headsUpView addSubview:cancelBtn];
//    cancelBtn.layer.cornerRadius = 5;
//    
//    headsUpView.transform = CGAffineTransformMakeScale(0.01, 0.01);
//    [self.view addSubview:headsUpView];
//    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut
//                     animations:^{
//                         headsUpView.transform = CGAffineTransformIdentity;
//                     }
//                     completion:^(BOOL finished){
//                     }];

    NSLog(@"Should Show Alert");
}

NSLog(@"Should Show Alert")在我的控制台窗口中出现LONG,然后才会出现任何编码方式的视图。数据传输可以正常工作,所有设备都可以获得它们应该得到的数据,但我想知道为什么这么小的用户界面需要加载这么长时间。

3 个答案:

答案 0 :(得分:2)

原来我的用户界面代码没有作为主线程的一部分被调用,所以我的解决方案看起来像这样:

dispatch_async(dispatch_get_main_queue(), ^(void) {
     UIAlertView *alert = …
});

在Apple开发者论坛上向那些帮助我的人们大声疾呼

答案 1 :(得分:1)

我不使用ARC。我发现有必要将会话的发布放在队列中以避免延迟:

 -(void)dealloc{
     if(theSession)
         dispatch_async(dispatch_get_main_queue(), ^{
            [theSession release];
          });
      //   more dealloc
  }

答案 2 :(得分:1)

我有一些UI更改要显示,所以根据teamDriven和Marco的评论将其更改为此。我正在努力去理解什么阻碍了主线程,并且从没想过我认为我没有开始主线程

// received data from remote peer
- (void)session:(MCSession *)session
 didReceiveData:(NSData *)data
       fromPeer:(MCPeerID *)peerID
{
    NSLog(@"session:didReceiveData:fromPeer");

    // didReceiveData is on background thread, and we have UI activity that we want on main thread
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [self processReceivedData:data fromPeer:peerID];
    });
}


// process received data
- (void)processReceivedData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
     // UI stuff
}