iOS Multipeer Connectivity: invitation races and leader election

This is an informal discussion, and I could be mistaken.

Suppose your app begins advertising its service when the app starts.  Suppose your app has a button the user can click to begin browsing for connections.  What happens when two users begin browsing at the same time?  It might be a race to invite the other.

An invitation race

Race conditions are often discussed in computer science.

In this specific, MPC invitation race, two instances have the following event sequences, at the same time:

  • app advertises
  • user touches “Connect” button
  • app calls framework to display browser, showing the other instance (candidate peer)
  • user chooses a candidate peer
  • framework receives invitation and displays invitation dialog from the other
  • user chooses to accept the invitation

Now what happens?  In my experience, it fails to make a connection.  When I and another tester got into an invitation race, my app would connect then immediately disconnect.

You can imagine the same thing for a real birthday party.  Two people invite the other to a birthday party on the same date and then both as invitees via mail accept the other’s invitation.

How important is solving an MPC invitation race?

Probably not very important.  It is likely your MPC users are within shouting distance.  They can solve the race problem just by talking to each other: “OK, you invite me and I will accept your invitation.”  The users can figure it out themselves without any instruction.  So it’s “would be nice” to solve the invitation race.

Does the MPC framework solve the invitation race problem for you?

Apparently not.  I tried using the high-level MPC API (the one that provides the GUI’s for browsing and invitations.)    It was only when we negotiated by shouting that we could make a connection.  It could be that my code is wrong, that I’m just missing a simple step to fix the problem.  But it seems like the hooks are not in the API.  You can learn when the invitation dialog closes, but you can’t know that the framework received an invite and is about to display an invitation (so that your app could choose NOT to display the invitation since the app knows it has already extended a contending, simultaneous invite.)

Using the low-level MCP API (the one where you provide the GUI), it seems there exist methods in the API that let you implement a solution to an invitation race.  Sample code for leader election.  So the hooks are there, but you must implement it, and you must implement the GUI also.