{"id":18976549,"url":"https://github.com/ahmedelserafy7/throttle","last_synced_at":"2026-04-09T10:30:19.252Z","repository":{"id":121105661,"uuid":"337523448","full_name":"ahmedelserafy7/Throttle","owner":"ahmedelserafy7","description":"Throttle is a simple iOS app to hinder incoming requests that causes overloading in the application, enhance user experience design, and improve performance.","archived":false,"fork":false,"pushed_at":"2021-08-24T11:06:10.000Z","size":1050,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-01T09:26:27.990Z","etag":null,"topics":["swift","throttle","throttle-function","throttle-requests","throttler"],"latest_commit_sha":null,"homepage":"","language":"Makefile","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/ahmedelserafy7.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}},"created_at":"2021-02-09T20:10:20.000Z","updated_at":"2021-08-29T14:20:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"4d6b71a9-1287-4f61-96a0-a33e3fba2e42","html_url":"https://github.com/ahmedelserafy7/Throttle","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmedelserafy7%2FThrottle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmedelserafy7%2FThrottle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmedelserafy7%2FThrottle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahmedelserafy7%2FThrottle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahmedelserafy7","download_url":"https://codeload.github.com/ahmedelserafy7/Throttle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239978059,"owners_count":19728271,"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","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":["swift","throttle","throttle-function","throttle-requests","throttler"],"created_at":"2024-11-08T15:25:17.253Z","updated_at":"2026-04-09T10:30:19.197Z","avatar_url":"https://github.com/ahmedelserafy7.png","language":"Makefile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Throttle\n\n- Simply put, it means to execute your block/operation/task once in a period of time (for instance once every 10 seconds).\n\n\u003cimg src=\"https://imgur.com/rzzZNFm.gif\" height=\"560\" alt=\"repetition\"\u003e\u003cimg src=\"https://imgur.com/FSezVSS.gif\" height=\"560\" alt=\"throttle\"\u003e\n\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\n**Without Throttle**\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\n\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\n**With Throttle**\n\nIn effect, there is a hodgepodge of different patterns you can use to execute your function for once like:\n* [dispatch_once](#dispatch_once)\n* [Throttle](#throttle)\n\n## `dispatch_once`\n\n`dispatch_once` is one of the solutions that you can use when you have more than one thread kicks off its function at the same time. so in order to solve \nthe repetition that could occur you can use `dispatch_once`.\nNow you're waiting for the `dispatch_once` code. Unfortunately it's deprecated, but likely you can use `lazy var` instead.\n\n`lazy var` or nominally `dispatch_once` used to execute your operation once.\n\n\u003cblockquote\u003e\nThe lazy initializer for a global variable (also for static members of structs and enums) is run the first time that global is accessed, and is launched as dispatch_once to make sure that the initialization is atomic. This enables a cool way to use dispatch_once in your code: just declare a global variable with an initializer and mark it private.\n\u003c/blockquote\u003e\n\nFrom [here](https://developer.apple.com/swift/blog/?id=7)\n\n## How it works\n\nThe initial setup phase wrapped in the `dispatch_once` block.\nSo when the second thread starts, it reaches the `dispatch_once` block and it's prevented from continuing because that section of code is currently running, \nIt just like let dispatch block's thread 2 until `dispatch_once` has been completed in thread 1.\nWhen thread 3 comes along, it too blocks until the thread one has completed the `dispatch_once` block.\nOnce the `dispatch_once` block has completed, then all three threads able to continue with the reminder of the function.\n\n## How to create it\n```swift\nlazy var showOnce: Void = {\n   return setupNotificationMessage()\n}()\n```\n\n## Usage\n```swift\n@objc func handleCheckOut() {\n   return showOnce\n}\n```\n\n### Note\n- In that window of time your app is running, the function will be executed for once. So it's not the best option to use for user experience design.\n- If you're looking for getting your user experience design stable and better, you can use **Throttle**.\n\n## Throttle\n - To throttle a function means to ensure the function is called at most once in a specified time period.\n\n## A simple way of creating throttle\n```swift\n class Throttle {\n\n var workItem = DispatchWorkItem(block: {})\n\n func throttle(_ block: @escaping ()-\u003e()) {\n\n // Cancel any operation comes along, and initializes the workItem to execute the block of code after two seconds.\n workItem.cancel()\n workItem = DispatchWorkItem {\n   block()\n }\n      \n // After two seconds we'll start executing the workItem.\n DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: workItem)\n }\n}\n```\n## Usage\n```swift\nvar throttler = Throttle()\n@objc func handleCheckOut() {\n    throttler.throttle({ [weak self] in\n        self?.setupNotificationMessage()\n    })\n}\n```    \n\n- Check out the annotation of [`throttle`](https://github.com/ahmedelserafy7/Throttle/blob/master/Throttle/Throttle.swift) function to manage to grasp the main points better.\n## Requirements\n\nThis project requires: \n* **Xcode 11+** \n* **iOS 13+**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmedelserafy7%2Fthrottle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahmedelserafy7%2Fthrottle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmedelserafy7%2Fthrottle/lists"}