gpt4 book ai didi

ios - 多点连接减缓 View 创建

转载 作者:行者123 更新时间:2023-12-01 18:17:23 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

关于ios - 多点连接减缓 View 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20560047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com