I know this question has been asked before, but I was just wondering why it isn't working in my particular case.

I am trying to send an invitation from multipeer connectivity from one view controller and receive it on another. My code for sending it is:

```
[self invitePeer:selectedPeerID toSession:self.mySession withContext:nil timeout:timeInterval ];

```

and method is just blank:

```
 - (void)invitePeer:(MCPeerID *)peerID toSession:(MCSession *)session withContext:(NSData *)context timeout:(NSTimeInterval)timeout 
 {

 }

```

My code for receiving and invitation is: 

```
 - (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler 
 {

      // http://down.vcnc.co.kr/WWDC_2013/Video/708.pdf  -- wwdc tutorial, this part is towards the end (p119)

      self.arrayInvitationHandler = [NSArray arrayWithObject:[invitationHandler copy]];
      // ask the user
      UIAlertView *alertView = [[UIAlertView alloc]
                          initWithTitle:peerID.displayName
                          message:@"Would like to create a session with you"
                          delegate:self
                          cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
      [alertView show];

  }

 - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
 {
      // retrieve the invitationHandler and  check whether the user accepted or declined the invitation...

      BOOL accept = (buttonIndex != alertView.cancelButtonIndex) ? YES : NO;

      // respond
      if(accept) {
              void (^invitationHandler)(BOOL, MCSession *) = [self.arrayInvitationHandler objectAtIndex:0];
    invitationHandler(accept, self.mySession);
          }
          else 
          {
              NSLog(@"Session disallowed");
          }
  }

```

I have all the delegate methods correctly set up as well as the same service types and that. But when i try to initiate the session, the tableviewcell which i click on just remains highlighted...

I'm thinking I have to put something in the invitePeer toSession method but I'm not sure...

I copied this directly from Apple's wwdc talk on Multipeer Connectivity referenced in my code... As you can see it is my own implementation of the code and I am not using an advertiser assistant or the mcbrowserviewcontroller.

Does anyone have any suggestions as to how I can get this to work??
['https://Stackoverflow.com/questions/19486762', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2218728/']
This should be expected, as per the [JavaDoc:](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep%28long%29)

> 
> Causes the currently executing thread to sleep (temporarily cease
>  execution) for the specified number of milliseconds, subject to the
>  precision and accuracy of system timers and schedulers. The thread
>  does not lose ownership of any monitors.
> 
> 
> 

What you could do, could be to sleep less, say, 400ms and then check to see if the time has changed.

Alternatively, you could use a [`Timer`](http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html) and fire events once every second and update your time.

Edit: As per your comment, you could do something like so:

```
long initTime = System.getTimeinMillis();
while(true)
{
      Thread.sleep(200);
      if((System.getTimeInMillis() - initTime) >= 1000)
      {
          initTime = System.getTimeInMillis();
          //Update your timer.
      }
}

```
Idea is to go to sleep, wake up(possibly prematurely) and check time again to go to sleep.

Under heavy system load, your timer thread does not get scheduled and thread may not wake up from sleep resulting your clock going off time.

But its always about showing correct time, whenever possible.

General suggestion: maximize sleep to keep wake ups minimum(each wakeup involves one context switch).

Here is something I wrote for myself:

```
   /**
     * Put the calling thread to sleep.
     * Note, this thread does not throw interrupted exception,
     * and will not return until the thread has slept for provided time.
     *
     * @param milliSecond milliSecond time to sleep in millisecond
     */
    public static void sleepForcefully(int milliSecond) {
        final long endingTime = System.currentTimeMillis() + milliSecond;
        long remainingTime = milliSecond;
        while (remainingTime > 0) {
            try {
                Thread.sleep(remainingTime);
            } catch (InterruptedException ignore) {
            }
            remainingTime = endingTime - System.currentTimeMillis();
        }
    }

```

This type of logic is used all over timers, pingers, etc in JDK and other places.