1

マルチピア接続を使用するアプリがあり、あるデバイスがグループ内の他のデバイスにデータを送信すると、他のすべてのデバイスは、データが送信されたという通知を画面に表示することになっており、送信するかどうかのオプションがあります。受け入れ。何をしても何度も何度も遭遇する問題は、通知ビューが表示されるまでに少なくとも5秒かかり、ほとんどの場合それ以上かかることです(UIAlertViewの代わりに独自の迅速で大雑把な代替物を作成しました-示されているようにコメントアウトされています-UIAlertViewが問題であり、何も解決しなかった場合)。session:didReceiveData:fromPeerこの APIのメソッド内で通知コードを実行しています。そして、UIAlertView の上と下の私の NSLog と私の粗雑な置換の両方がすばやく連続して表示されます。ビューが表示されるまでに永遠に時間がかかります。何か案は?これが私のコードです:

-(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

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 に答える