1

我有一个使用 Multipeer Connectivity 的应用程序,当一个设备向组中的其他设备发送数据时,所有其他设备都应该在他们的屏幕上收到数据发送给他们的通知,然后他们可以选择是否发送接受。无论我做什么,我一次又一次遇到的问题是通知视图至少需要五秒钟才能显示出来,而且大多数时间更长(我什至用我自己的快速和粗略的替代 UIAlertView -如图所示注释掉 - 如果 UIAlertView 是问题,它没有解决任何问题)。我在session:didReceiveData:fromPeer这个 API 的方法中运行了通知代码。我的 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")任何一种编码方法的视图实际出现之前很久就出现在我的控制台窗口中。数据传输正常工作,所有设备都获得了他们应该获得的数据,但我想知道为什么加载这么小的用户界面需要这么长时间。

4

3 回答 3

2

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

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

在 Apple 开发者论坛上向帮助我的人大喊

于 2013-12-15T17:11:27.120 回答
1

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

 -(void)dealloc{
     if(theSession)
         dispatch_async(dispatch_get_main_queue(), ^{
            [theSession release];
          });
      //   more dealloc
  }
于 2014-03-26T03:50:42.680 回答
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
}
于 2014-05-18T16:18:32.883 回答