0

My app has three states:

  • A) connected to server
  • B) connected to wifi, but no internet
  • C) no connection

The first state is the default mode of operation of the app, and the other two (B & C) are presented as modal view controllers. On various reachability changes, the correct modal view should appear.

Unfortunately, in certain cases B & C both attempt to be presented at the same time, or B is currently presented and may be a millisecond away from closing, but C is already trying to show itself.

My hack of a solution is to basically create a semaphore with some delegates for each modal view- when a modal view opens, it sends a message via the delegate to go to the main view to turn a flag on. Then when the second modal view tries to open, it sees the first is on and waits until it is off. That seems like a ridiculous hack for me trying to make a very basic state machine.

Any thoughts?

nflacco
  • 4,972
  • 8
  • 45
  • 78

2 Answers2

1

You don't specify in which cases B & C could present themselves both, so the first thing that comes to mind is that you could look for a different way to check whether you are in case B or C so that you can disambiguate better.

Speaking about the flag you are using, I guess the ugly part has to do with the waiting you mention. This seems overkill to me. One alternative is instead of letting the second modal view to wait for the flag to be reset, you make the first modal view send a notification which the second view is observing. Implementation of this is pretty trivial and you can find many examples in S.O. (e.g., this one).

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • Unfortunately the B & C cases seem to overlap based on the output of my reachability mechanism (or more properly, some of the conditions that make up B & C overlap). Re notifications, my app already does a significant amount of this for other purposes and I want to keep the network state and associated UI stuff as clear of that as possible. – nflacco Jan 24 '12 at 23:51
1

I'd collapse the separate B & C cases into a single view controller that changes its view based on whether B or C is happening at any given moment. That way you don't have them competing, because there's only ever one view controller.

jsd
  • 7,673
  • 5
  • 27
  • 47
  • This seems to be the simplest solution- no notification, no delegation so the code will be as maintainable as possible. – nflacco Jan 24 '12 at 23:52