My main code is to long to put here so i'll try with a simpler explanation. Let's say i have a global variable called "count" that gets used by functions. If count was to get called twice for we reason as shown in the code below... how can i make it so the output "10 , 10" rather then "10 , 20".
var count = 0
override func viewDidLoad() {
helloWorld()
}
override func viewWillAppear(_ animated: Bool) {
helloWorld()
}
func helloWorld(){
var counter = 0
for i in 1...10{
count = count + 1
counter = counter + 1
}
print(count)
}
Use DispatchQueue with static token (cached through app lifecycle), here's a useful extension:
extension DispatchQueue {
private static var tokens: [String] = [] // static, so tokens are cached
class func once(executionToken: String, _ closure: () -> Void) {
// objc_sync_enter/objc_sync_exit is a locking mechanism
objc_sync_enter(self)
defer { objc_sync_exit(self) }
// if we previously stored token then do nothing
if tokens.contains(executionToken) { return }
tokens.append(executionToken)
closure()
}
}
func doSomething() {
DispatchQueue.once(executionToken: "doSomething") {
print("This should be executed once")
}
}
doSomething()
doSomething()
doSomething()
"This should be executed once" will be printed only once.
You can also use the lazy mechanism:
class SomeClass {
lazy var executeOnce: () -> Void = {
doSomething()
return {}
}()
func doSomething() {
print("This should be executed once")
}
}
let someClass = SomeClass()
someClass.executeOnce()
someClass.executeOnce()
someClass.executeOnce()
It's thread-safe out of the box so you don't need to worry about locking.
Change
func helloWorld(){
var counter = 0
for i in 1...10{
count = count + 1
counter = counter + 1
}
print(count)
}
to
func helloWorld(){
var counter = self.count
for i in 1...10{
count = count + 1
}
print(count)
self.count = counter
}
This way you store into counter the count value before it changes, then make it equal to the previous one again.
NOTE: This way you will always get 10 when you call helloWorld(), no matter how much time you call the function.
You can change the function as shown below:
func helloWorld(_ countValue: Int){
var counter = self.count
for i in 1..<countValue{
count = count + 1
}
print(count)
self.count = counter
}
So you can choose the upper element of the counter simply by calling:
helloWorld(10)
helloWorld(20)
ADVICE: it's not a good practice to have a function that changes an external value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.