ios - NSNotification - observer with multiple events to trigger -


as stands, nsnotifications allow target-action mechanism in response 1 post / event.

i have notification triggers action (runs function) after 2 events have been triggered.

the scenario have 2 asynchronous processes need complete before can call function. perhaps i'm missing something, haven't found way this. or maybe i'm not thinking of obvious reason why bad idea?

also, of terminology may off, please feel free edit , fix it.

there many possibilities on how can implement this. center around keeping track of processes finished. best way depends on how background processes implemented.

if using nsoperationqueue add third operation has other 2 operations dependency. way won't have take care of notifications @ all.

otherwise can can count how many operations have finished , execute code when counter reaches right value. gcd has dispatch groups nice abstraction this.

first create dispatch group:

let group = dispatch_group_create() 

then enter group each background process:

dispatch_group_enter(group) 

finally can register block gets called when group becomes empty, when each dispatch_group_enter balanced dispatch_group_leave:

dispatch_group_notify(group, dispatch_get_main_queue()) {    // processes done. } 

after each of processes finish leave group again:

dispatch_group_leave(group) 

it's important call dispatch_group_enter before calling dispatch_group_notify or block scheduled group empty.

after notify block executed can reuse queue or discard it.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -