0

These are two methods I have:

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

{
NSLog(@"RECEIVING... %@ from peer: %@", progress, peerID);
[self performSelectorOnMainThread:@selector(updateUIWithPeerId:) withObject:nil waitUntilDone:YES];
}

- (void) updateUIWithPeerId:(MCPeerID *)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];
}

The first calls the second with this code:

[self performSelectorOnMainThread:@selector(updateUIWithPeerId:) withObject:nil waitUntilDone:YES];

But I want to pass on to the second method this variable: peerID. Usually I would do:

[self updateUIWithPeerId: peerID];

but I can't do it in performSelectorOnMainThread

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Alessandro
  • 4,000
  • 12
  • 63
  • 131

1 Answers1

2
[self performSelectorOnMainThread:@selector(updateUIWithPeerId:)
                       withObject:peerID
                    waitUntilDone:YES];

From the documentation:

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait

...

arg
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.


Alternatively

dispatch_sync(dispatch_get_main_queue(), ^{
    [self updateUIWithPeerId:peerID];
}

but be absolutely sure not to invoke this from the main queue, since calling dispatch_sync on the current queue will result in a deadlock.

Some insights about the slight difference between the two approaches can be found here: Whats the difference between performSelectorOnMainThread and dispatch_async on main queue?

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • Thanks for your advice. Should I do - (void)performSelector:updateUIWithPeerId onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait if I am thinking to use the method [self performSelectorOnMainThread:@selector(updateUIWithPeerId:) withObject:peerID waitUntilDone:YES]; more than once in my code? – Alessandro Oct 04 '13 at 17:49
  • Sorry I don't understand the question. – Gabriele Petronella Oct 04 '13 at 17:51
  • Whenever I call the code you provided me it will result in the void method - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thread withObject:(id)arg waitUntilDone:(BOOL)wait. The point is that it might be a different method e.g. instead of updateUIWithPeerId, it might be updateUIWithPeerId2. How do I differentiate between the two within the void method? – Alessandro Oct 04 '13 at 17:53
  • I'm frankly not following. What you do you mean by *within* the void method? What's your use case? – Gabriele Petronella Oct 04 '13 at 17:57