我有一个使用 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")
任何一种编码方法的视图实际出现之前很久就出现在我的控制台窗口中。数据传输正常工作,所有设备都获得了他们应该获得的数据,但我想知道为什么加载这么小的用户界面需要这么长时间。