{"id":51011264,"url":"https://github.com/feed0/appsettingsdemo","last_synced_at":"2026-06-21T03:01:43.106Z","repository":{"id":340240433,"uuid":"1165154436","full_name":"feed0/AppSettingsDemo","owner":"feed0","description":"Swift Singleton by Karoly Nyisztor","archived":false,"fork":false,"pushed_at":"2026-02-23T22:55:39.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-02-24T05:16:30.478Z","etag":null,"topics":["design-patterns","singleton-pattern","swift","swiftui"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/feed0.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-23T22:03:39.000Z","updated_at":"2026-02-23T22:55:42.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/feed0/AppSettingsDemo","commit_stats":null,"previous_names":["feed0/appsettingsdemo"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/feed0/AppSettingsDemo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feed0%2FAppSettingsDemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feed0%2FAppSettingsDemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feed0%2FAppSettingsDemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feed0%2FAppSettingsDemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/feed0","download_url":"https://codeload.github.com/feed0/AppSettingsDemo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feed0%2FAppSettingsDemo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34592057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-21T02:00:05.568Z","response_time":54,"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":["design-patterns","singleton-pattern","swift","swiftui"],"created_at":"2026-06-21T03:01:38.208Z","updated_at":"2026-06-21T03:01:43.091Z","avatar_url":"https://github.com/feed0.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AppSettingsDemo: Singleton design pattern in Swift\n\nAppSettings is a singleton class that offers:\n- a `settings` **dictionary**,\n- and **read** and **write** methods for its keys and values\n    - `int(forKey:) -\u003e Int`\n    - `string(forKey:) -\u003e String`\n- through its `shared` instance.\n\n## Concurrency issue\n\n\u003e When a thread tries to **read** some key's value using `.int(forKey:) -\u003e Int`,\n\u003cbr\u003e while other thread tries to **update** some key's value using `set(value:forKey:)`,\n\u003cbr\u003e an **_EXCUTION_BAD_ACCESS_** _crash_ happens.\n\n### Reproduce\n\n1. Clone the repo\n1. **Switch** to commit 4 _53f482271e975ee852059dfd35158ab9aadc5a38_\n1. **Run** only `testConcurrentUsage()` until a crash happens\n1. Take a **look** at each thread on Xcode's Debug Navigator _(cmd + 7)_\n\n### Understand\n\n#### The test\n\n- A concurrent queue for writes\n- A read op that might eventually happen during the write ops, resulting in a **data race**.\n\n```swift\n    func testConcurrentUsage() {\n        let concurrentQueue = DispatchQueue(\n            label: \"concurrentQueue\",\n            attributes: .concurrent)\n        \n        let expect = expectation(\n            description: \"Using AppSettings.shared from multiple threads shall succeed.\")\n        \n        let callCount = 100\n\n        /// write ops\n        for callIndex in 1...callCount {\n            concurrentQueue.async {\n                AppSettings.shared.set(\n                    value: callIndex,\n                    forKey: String(callIndex))\n            }\n        }\n\n        /// read op\n        while AppSettings.shared.int(forKey: String(callCount)) != callCount {\n            /// nop\n        }\n        \n        expect.fulfill()\n        \n        waitForExpectations(timeout: 5) { (error) in\n            XCTAssertNil(error, \"Test expectation failed\")}     \n    }\n```\n\n#### The crash\n\n- Yellow dashed boxes on the left panel highlight the threads accessing with `AppSettings.int(forKey:)` and threads updating with `AppSettings.set(value:forKey:)`.\n- Magenta dashed boxes on the right panel highlight one of the threads' call stack\n\n\u003cimg \n  width=\"1449\" \n  height=\"1161\" \n  alt=\"thread EXCUTION_BAD_ACCESS crash\" \n  src=\"https://github.com/user-attachments/assets/c9cd06ac-2a46-4bf6-9e88-1d8a49b90aa4\" /\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeed0%2Fappsettingsdemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffeed0%2Fappsettingsdemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeed0%2Fappsettingsdemo/lists"}