0

Just learning GCD and wanted some guidance. I'd like to set up a debounce function so that a logoff notification for users I'm following doesn't appear for 5 minutes in case that user log right back in. In this case I'd like to have a delayed logoff UI notification get added to a timeout queue to execute 5 minutes from now - unless that user logs back in in which case I would cancel that specific notification in the queue. (note user in these examples is not me - the currently logged in active user).

Which type of GCD queue should I use?

  • Main
  • QOS_CLASS_USER_INTERACTIVE
  • QOS_CLASS_USER_INITIATED,
  • QOS_CLASS_UTILITY,QOS_CLASS_BACKGROUND

example

How can I debounce a method call?

I also found an example using a timer - is that different or same as a GCD call under the covers?

Search as you type Swift

Community
  • 1
  • 1
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

1 Answers1

0

GCD is best at fire-and-forget tasks where you set up some work to be done and don't make any adjustments after that.

A simple version of what you want would be to use a NSTimer. If the user logs in, you invalidate the timer. If the timer survives and fires, you send your notification.

If, for some reason, you need to involve extra threading in this, look at NSOperationQueue instead of GCD. That lets you examine the queue and apply cancellation logic.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57