forked from codecat15/Youtube-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
DispatchGroupVsNestedClosure/DispatchGroupVsNestedClosure.playground/Contents.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Foundation | ||
|
||
/**I hope the video was helpful, please feel free to ask questions and if you are new to the channel then please subscribe, like and share the video with your iOS group | ||
|
||
Regards, | ||
Ravi (CodeCat15) | ||
*/ | ||
|
||
struct Example | ||
{ | ||
func work() | ||
{ | ||
var arr: [String] = [] | ||
let startTime = Date() | ||
|
||
// callApiA { (responseFromA) in | ||
// callApiB { (responseFromB) in | ||
// callApiC { (responseFromC) in | ||
// arr.append(responseFromA) | ||
// arr.append(responseFromB) | ||
// arr.append(responseFromC) | ||
// debugPrint(Date().timeIntervalSince(startTime)) | ||
// } | ||
// } | ||
// } | ||
|
||
let dispatchGroup = DispatchGroup() | ||
dispatchGroup.enter() | ||
callApiA { (responseFromA) in | ||
arr.append(responseFromA) | ||
dispatchGroup.leave() | ||
} | ||
|
||
dispatchGroup.enter() | ||
callApiB { (responseFromB) in | ||
arr.append(responseFromB) | ||
dispatchGroup.leave() | ||
} | ||
|
||
dispatchGroup.enter() | ||
callApiC { (responseFromC) in | ||
arr.append(responseFromC) | ||
dispatchGroup.leave() | ||
} | ||
|
||
dispatchGroup.notify(queue: .main) { | ||
debugPrint(Date().timeIntervalSince(startTime)) | ||
} | ||
} | ||
|
||
func callApiA(completion:@escaping(String)->Void) { | ||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1), execute: { | ||
completion("data from service A") | ||
}) | ||
} | ||
|
||
func callApiB(completion:@escaping(String)->Void) { | ||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2), execute: { | ||
completion("data from service B") | ||
}) | ||
} | ||
|
||
func callApiC(completion:@escaping(String)->Void) { | ||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(3), execute: { | ||
completion("data from service C") | ||
}) | ||
} | ||
} | ||
|
||
let obj = Example() | ||
obj.work() |
4 changes: 4 additions & 0 deletions
4
DispatchGroupVsNestedClosure/DispatchGroupVsNestedClosure.playground/contents.xcplayground
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios' buildActiveScheme='true'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
7 changes: 7 additions & 0 deletions
7
...e/DispatchGroupVsNestedClosure.playground/playground.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+16.8 KB
...d/playground.xcworkspace/xcuserdata/ravirdixit.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.