Question

I am using multipeer connectivity and this is one of the methods:

-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress

{
NSLog(@"RECEIVING... %@ from peer: %@", progress, peerID);

UIProgressView *progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressBar.frame = CGRectMake(0, 200, 100, 20);
progressBar.progress = 0.5;
UIButton* btn = [BluetoothDeviceDictionary objectForKey:peerID];
[self.view addSubview:progressBar];

UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Alert View Title" message:@"Alert View Text" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
}

For some VERY STRANGE reason, when this method is called, and I know it is called because of the response from the NSLog, the rest of the code is not executed. The alert appears 20 seconds (more or less) after the NSLog has appeared, and the progress view never appears. I can't get why. This happens for most of the methods in multipeer connectivity framework. How is this possible?

EDIT: actually the progress view appears, but much much after the method is called

Was it helpful?

Solution

It's possible that the session delegate's method is being invoked on a background thread. UIKit calls should only be made on the main thread, so you may need to move your code that interacts with UIKit to another method like this:

- (void) updateUI {
    UIProgressView *progressBar = [[UIProgressView alloc];
    initWithProgressViewStyle:UIProgressViewStyleBar];
    progressBar.frame = CGRectMake(0, 200, 100, 20);
    progressBar.progress = 0.5;
    UIButton* btn = [BluetoothDeviceDictionary objectForKey:peerID];
    [self.view addSubview:progressBar];

    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Alert View Title" message:@"Alert View Text" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alertView show];
}

and then call it using:

-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
{
    NSLog(@"RECEIVING... %@ from peer: %@", progress, peerID);
    [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top