Last active
September 9, 2019 05:02
-
-
Save foresta/1164682b8647ef977c5496293b557f41 to your computer and use it in GitHub Desktop.
Async Request Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getFriends() { | |
let numberOfParallel: Int = 2 | |
var completionCount: Int = 0 | |
// 呼ばれるたびに 1ずつ足していって並列実行数を超えたら何かのメソッドを呼ぶ | |
completion = []() { | |
completionCount += 1 | |
if (numberOfParallel >= comletionCount) { | |
// 突合する処理など | |
doSomethingAfterFetchFriends() | |
} | |
} | |
// メソッドの内部で終了時に、completionコールバックを呼ぶ | |
getFollows(completion: completion) | |
getFollowers(completion: completion) | |
} | |
private func getFollows(completion: @escaping () -> Void) { | |
doAsyncSomething() { | |
completion() | |
} | |
} | |
private func getFollowers(completion: @escaping () -> Void) { | |
doAsyncSomething() { | |
completion() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment