A completion Handler in iOS takes a thread to asynchronously get it done. So it means that we can’t know an exact point which the tasks finish. For example, if we use a for loop to execute several tasks in the background, how could we know all the task are done?
In this case you can use dispatch_group.
- Create a dispatch group which graps all the tasks in a for loop with
dispatch_group_create()
. - You point out a position which your code enters the dispatch group with
dispatch_group_enter(groupname)
. - You point out a position which your code leave the dispatch group with
dispatch_group_leave(groupname)
. - Lastly you can get a notification when all the tasks you assigned to a dispatch group is done with
dispatch_group_notify
.
If you want to see a concrete example for it, please refer this.
0 κ°μ λκΈ:
Post a Comment