Handling UI events that happen close to simultaneously

I've found quite a few bugs in my app by tapping the screen extremely quickly so that a second event gets processed before the first has finished executing. Things like pushing and popping a view controller at the same time can put the app into a state with unpredictable results.

I've started solving these by adding flags to control it. So when I handle a push event I set a flag to ignore any subsequent push or pop events until the view appears again when I clear it.

This is solving the problem but it feels very much like a hack and also quite brittle. If I make a mistake in the view controller life cycle I may end up with buttons not working since the flag is in an incorrect state.

Has anyone else dealt with this problem?

Posted Mar 22, 2015, 11:18 PM UTC

312
ratherbealurker•

A state or busy flag doesn't sound hacky to me.

I use states a lot.

Recently I used it on a login screen for a game. You can login or create a user, so I use an enum for the state.

I just added a third state for switching and in the animation sequence for the button pop in I added a call block at the end, it sets the state to the correct one.

If you hit the button it just returns if the state is switching.

2
askoruliOP•

If I was only doing this in a few places it wouldn't feel so bad. But almost every button needs a check to make sure double taps can't happen.

At least I'm not alone in doing this.

2
ssrobbi•

Why don't you just disable the button until after the push finishes?

1
askoruliOP•

That requires more state management. There could be multiple buttons, cells and gestures any of which could cause an issue if pressed simultaneously. I would rather set a flag that causes actions to be ignored than disable buttons all over the screen.

1
ratherbealurker•

our situations are probably different, but i have 4 buttons on one screen and they're all controlled with that one state.

Granted, i only have to set the state in one button, the one that causes the animation of the others. But it wouldn't be much to have a function run in each button to set the state to switching mode.

But when i set the state in any one button it applies for all, the state is for the scene and they all see it.

But maybe i am not visualizing your scenario correctly.

1
askoruliOP•

This case is common across most of my app but manifests worse in some places than others. Let's say a screen has a table view and a back button. Clicking a cell will push the next view controller, back pops to the previous. There are custom animations between each view controller. If I tap a cell and then immediately press back the next VC will be pushed and the current one will be removed.

This causes all sorts of problems with the VC lifecycle and creates all sorts of edge cases that I don't want to have to deal with.

1
quellish•

The main queue is serial - only one thing can happen at a time. How are things executing out of order?

1
askoruliOP•

Order isn't the problem. The problem is that the view controller is still visible on the screen and accepting touches after push/pop has been called.

1
aporcelaintouch•

Why don't you disable the views you don't need from user interaction so they can't screw things up? Or are they supposed to be allowed to interact with that other view?

1
askoruliOP•

That might be a better idea than what I'm doing. I'm setting userInteractionEnabled during the navigation animation but I had to add these flags as well because events could come through before the animation started. I was a bit worried that I might get things mixed up if I was setting userInteractionEnabled from 2 places but it probably makes sense to consolidate all this safety code into a single place.

1
HappyToHelpAll•

You shouldn't need to be managing this at all in the way you are. It's all sounding a bit strange. Could you post some sample code?

1