{"id":20564082,"url":"https://github.com/clintjang/sample-swift-dispatchgroup","last_synced_at":"2025-10-13T19:12:45.693Z","repository":{"id":99120072,"uuid":"168614763","full_name":"ClintJang/sample-swift-dispatchgroup","owner":"ClintJang","description":"It is a repository showing a simple example using \"DispatchGroup\".","archived":false,"fork":false,"pushed_at":"2019-02-01T08:54:01.000Z","size":3278,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-06T08:32:04.523Z","etag":null,"topics":["dispatchgroup","dispatchqueue","enter","gcd","leave","notify","wait"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ClintJang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-31T23:50:15.000Z","updated_at":"2020-09-01T13:37:51.000Z","dependencies_parsed_at":"2023-06-04T00:15:29.284Z","dependency_job_id":null,"html_url":"https://github.com/ClintJang/sample-swift-dispatchgroup","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ClintJang/sample-swift-dispatchgroup","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClintJang%2Fsample-swift-dispatchgroup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClintJang%2Fsample-swift-dispatchgroup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClintJang%2Fsample-swift-dispatchgroup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClintJang%2Fsample-swift-dispatchgroup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ClintJang","download_url":"https://codeload.github.com/ClintJang/sample-swift-dispatchgroup/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClintJang%2Fsample-swift-dispatchgroup/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279016883,"owners_count":26085884,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dispatchgroup","dispatchqueue","enter","gcd","leave","notify","wait"],"created_at":"2024-11-16T04:23:51.360Z","updated_at":"2025-10-13T19:12:45.675Z","avatar_url":"https://github.com/ClintJang.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Swift Dispatchgroup Sample\nIt is a repository showing a simple example using \"DispatchGroup\". \u003cbr /\u003e\nJobs already in group via DispatchGroup\nWait for it to finish and use it when you expect it to be called when it's all done.\n\n- \"notify\" works asynchronously.\n- \"wait\" works synchronously.\n- \"queue, notify\" works synchronously\n\n## Result Image (GIF)\n\u003cimg width=\"500\" src=\"/Image/resultInfo00.gif\"\u003e\n\n## Case 01 (notify)\n\u003e asynchronous\n\n```swift\n\nfunc dispatchGroupTestCase1Notify() {\n    writeLog(\"=====================\\n== asynchronous\")\n    writeLog(\"\\(#function), start\")\n    let dispatchGroup = DispatchGroup()\n\n    dispatchGroup.enter()\n    DispatchQueue.global().async { [weak self] in\n        guard let self = self else { return }\n        if self.roof20_000_000() { dispatchGroup.leave() }\n    }\n    \n    dispatchGroup.enter()\n    DispatchQueue.global().async { [weak self] in\n        guard let self = self else { return }\n        if self.roof10_000_000() { dispatchGroup.leave() }\n    }\n    \n    dispatchGroup.enter()\n    DispatchQueue.global().async { [weak self] in\n        guard let self = self else { return }\n        if self.roof15_000_000() { dispatchGroup.leave() }\n    }\n    \n    dispatchGroup.notify(queue: .main) { [weak self] in\n        guard let self = self else { return }\n        self.writeLog(\"dispatchGroup.notify OK\")\n        self.writeLog(\"\\(#function), end\")\n    }\n}\n\n```\n\n## Case 02 (wait)\n\u003e synchronous\n\n```swift\n\t\nfunc dispatchGroupTestCase2Wait() {\n    writeLog(\"=====================\\n== synchronous\")\n    writeLog(\"\\(#function), start\")\n    let dispatchGroup = DispatchGroup()\n    \n    dispatchGroup.enter()\n    DispatchQueue.global().async { [weak self] in\n        guard let self = self else { return }\n        if self.roof20_000_000() { dispatchGroup.leave() }\n    }\n    \n    dispatchGroup.enter()\n    DispatchQueue.global().async { [weak self] in\n        guard let self = self else { return }\n        if self.roof10_000_000() { dispatchGroup.leave() }\n    }\n    \n    dispatchGroup.enter()\n    DispatchQueue.global().async { [weak self] in\n        guard let self = self else { return }\n        if self.roof15_000_000() { dispatchGroup.leave() }\n    }\n    \n    let result = dispatchGroup.wait(timeout: .distantFuture)\n    self.writeLog(\"dispatchGroup.wait OK(result::\\(result))\")\n    self.writeLog(\"\\(#function), end\")\n}\n\n```\n\n## Case 03 (queue, notify)\n\u003e asynchronous, Do not use enter and leave.\n\n```swift\nfunc dispatchGroupTestCase3QueueNotify() {\n    writeLog(\"=====================\\n== asynchronous\")\n    writeLog(\"=====================\\n== Do not use enter and leave.\")\n    writeLog(\"\\(#function), start\")\n    let dispatchGroup = DispatchGroup()\n    let dispatchQueueGlobal = DispatchQueue.global()\n    \n    dispatchQueueGlobal.async(group: dispatchGroup, execute: { [weak self] in\n        guard let self = self else { return }\n        let _ = self.roof20_000_000()\n    })\n    \n    dispatchQueueGlobal.async(group: dispatchGroup, execute: { [weak self] in\n        guard let self = self else { return }\n        let _ = self.roof10_000_000()\n    })\n    \n    dispatchQueueGlobal.async(group: dispatchGroup, execute: { [weak self] in\n        guard let self = self else { return }\n        let _ = self.roof15_000_000()\n    })\n    \n    dispatchGroup.notify(queue: dispatchQueueGlobal) { [weak self] in\n        guard let self = self else { return }\n        self.writeLog(\"dispatchGroup.notify OK\")\n        self.writeLog(\"\\(#function), end\")\n    }\n}\n```\n\n## Result Log\n\n### case 1 (notify, asynchronous)\n\n```\n2019-02-01 08:15:42 +0000::=====================\n== asynchronous\n2019-02-01 08:15:42 +0000::dispatchGroupTestCase1Notify(), start\n2019-02-01 08:15:42 +0000::roof20_000_000(), start\n2019-02-01 08:15:42 +0000::roof15_000_000(), start\n2019-02-01 08:15:42 +0000::roof10_000_000(), start\n2019-02-01 08:15:45 +0000::roof10_000_000(), end\n2019-02-01 08:15:47 +0000::roof15_000_000(), end\n2019-02-01 08:15:48 +0000::roof20_000_000(), end\n2019-02-01 08:15:48 +0000::dispatchGroup.notify OK\n2019-02-01 08:15:48 +0000::dispatchGroupTestCase1Notify(), end\n\n```\n\n### case 2 (wait, synchronous)\n\n```\n2019-02-01 08:15:49 +0000::=====================\n== synchronous\n2019-02-01 08:15:49 +0000::dispatchGroupTestCase2Wait(), start\n2019-02-01 08:15:49 +0000::roof20_000_000(), start\n2019-02-01 08:15:49 +0000::roof10_000_000(), start\n2019-02-01 08:15:49 +0000::roof15_000_000(), start\n2019-02-01 08:15:52 +0000::roof10_000_000(), end\n2019-02-01 08:15:54 +0000::roof15_000_000(), end\n2019-02-01 08:15:55 +0000::roof20_000_000(), end\n2019-02-01 08:15:55 +0000::dispatchGroup.wait OK(result::success)\n2019-02-01 08:15:55 +0000::dispatchGroupTestCase2Wait(), end\n```\n\n### case 3 (queue, notify, asynchronous)\n\n```\n2019-02-01 08:15:57 +0000::=====================\n== asynchronous\n2019-02-01 08:15:57 +0000::=====================\n== Do not use enter and leave.\n2019-02-01 08:15:57 +0000::dispatchGroupTestCase3QueueNotify(), start\n2019-02-01 08:15:57 +0000::roof20_000_000(), start\n2019-02-01 08:15:57 +0000::roof15_000_000(), start\n2019-02-01 08:15:57 +0000::roof10_000_000(), start\n2019-02-01 08:16:00 +0000::roof10_000_000(), end\n2019-02-01 08:16:01 +0000::roof15_000_000(), end\n2019-02-01 08:16:03 +0000::roof20_000_000(), end\n2019-02-01 08:16:03 +0000::dispatchGroup.notify OK\n2019-02-01 08:16:03 +0000::dispatchGroupTestCase3QueueNotify(), end\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclintjang%2Fsample-swift-dispatchgroup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclintjang%2Fsample-swift-dispatchgroup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclintjang%2Fsample-swift-dispatchgroup/lists"}