Completion handler in For/In loop - swift

笑着哭i 提交于 2019-12-06 13:44:13

Looks as if checkIfPhoneExistsInDatabase is doing an asynchronous job. So the rest of your code can run at any time before, after, or in-between your completion handler.

So if your DataService instance doesn't provide synchronous operation or some synchronization, you have to manually do that job in your completion block. Your main point of interest will be to check if all items have been processed. Be careful about race conditions.

Another idea would be to serialize the queries, basically pulling the loop into the completion handler: just do the query on the first element, and within the block, query the data for the next element and so on.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!