-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use 'weak' for references captured by closures (#222)
* Weak-ref in all closures * Spawn many tasks
- Loading branch information
1 parent
b4016d6
commit 971fd08
Showing
8 changed files
with
88 additions
and
14 deletions.
There are no files selected for viewing
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
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,64 @@ | ||
import UIKit | ||
import Statsig | ||
|
||
class ManyUpdatesViewController: UIViewController { | ||
|
||
var queues: [DispatchQueue] = [] | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
view.backgroundColor = UIColor.white | ||
|
||
let user = StatsigUser(userID: "a-user") | ||
let opts = StatsigOptions(enableCacheByFile: true) | ||
|
||
Statsig.start( | ||
sdkKey: Constants.CLIENT_SDK_KEY, | ||
user: user, | ||
options: opts | ||
) { [weak self] err in | ||
if let err = err { | ||
print("Error \(err)") | ||
} | ||
|
||
self?.addButton() | ||
} | ||
} | ||
|
||
private func addButton() { | ||
let button = UIButton(type: .system) | ||
button.frame = CGRect(x: 100, y: 100, width: 200, height: 50) | ||
button.setTitle("Click", for: .normal) | ||
button.addTarget(self, action: #selector(runUpdates), for: .touchUpInside) | ||
view.addSubview(button) | ||
} | ||
|
||
@objc private func runUpdates() { | ||
let numberOfTasks = 10 | ||
|
||
if (queues.isEmpty) { | ||
for i in 0..<numberOfTasks { | ||
queues.append(DispatchQueue(label: "com.statsig.task_\(i)")) | ||
} | ||
} | ||
|
||
for i in 0..<numberOfTasks { | ||
let queue = queues[i] | ||
|
||
queue.async { | ||
let user = self.getRandomUser() | ||
print("Updating user \(user.userID ?? "")...") | ||
Statsig.updateUser(user) { err in | ||
print("Updated user \(user.userID ?? "")") | ||
print("Gate check \(Statsig.checkGate("partial_gate"))") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func getRandomUser() -> StatsigUser { | ||
StatsigUser(userID: "user_\(Int.random(in: 1...100))") | ||
} | ||
} | ||
|
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
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
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
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
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
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