Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ahmedelserafy7/throttle
Throttle is a simple iOS app to hinder incoming requests that causes overloading in the application, enhance user experience design, and improve performance.
https://github.com/ahmedelserafy7/throttle
swift throttle throttle-function throttle-requests throttler
Last synced: 9 days ago
JSON representation
Throttle is a simple iOS app to hinder incoming requests that causes overloading in the application, enhance user experience design, and improve performance.
- Host: GitHub
- URL: https://github.com/ahmedelserafy7/throttle
- Owner: ahmedelserafy7
- Created: 2021-02-09T20:10:20.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-08-24T11:06:10.000Z (over 3 years ago)
- Last Synced: 2024-11-08T15:31:29.741Z (2 months ago)
- Topics: swift, throttle, throttle-function, throttle-requests, throttler
- Language: Makefile
- Homepage:
- Size: 1 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Throttle
- Simply put, it means to execute your block/operation/task once in a period of time (for instance once every 10 seconds).
**Without Throttle**
**With Throttle**In effect, there is a hodgepodge of different patterns you can use to execute your function for once like:
* [dispatch_once](#dispatch_once)
* [Throttle](#throttle)## `dispatch_once`
`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
the repetition that could occur you can use `dispatch_once`.
Now you're waiting for the `dispatch_once` code. Unfortunately it's deprecated, but likely you can use `lazy var` instead.`lazy var` or nominally `dispatch_once` used to execute your operation once.
The 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.From [here](https://developer.apple.com/swift/blog/?id=7)
## How it works
The initial setup phase wrapped in the `dispatch_once` block.
So 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,
It just like let dispatch block's thread 2 until `dispatch_once` has been completed in thread 1.
When thread 3 comes along, it too blocks until the thread one has completed the `dispatch_once` block.
Once the `dispatch_once` block has completed, then all three threads able to continue with the reminder of the function.## How to create it
```swift
lazy var showOnce: Void = {
return setupNotificationMessage()
}()
```## Usage
```swift
@objc func handleCheckOut() {
return showOnce
}
```### Note
- 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.
- If you're looking for getting your user experience design stable and better, you can use **Throttle**.## Throttle
- To throttle a function means to ensure the function is called at most once in a specified time period.## A simple way of creating throttle
```swift
class Throttle {var workItem = DispatchWorkItem(block: {})
func throttle(_ block: @escaping ()->()) {
// Cancel any operation comes along, and initializes the workItem to execute the block of code after two seconds.
workItem.cancel()
workItem = DispatchWorkItem {
block()
}
// After two seconds we'll start executing the workItem.
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: workItem)
}
}
```
## Usage
```swift
var throttler = Throttle()
@objc func handleCheckOut() {
throttler.throttle({ [weak self] in
self?.setupNotificationMessage()
})
}
```- 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.
## RequirementsThis project requires:
* **Xcode 11+**
* **iOS 13+**