Skip to content

Commit

Permalink
Ensure Examples' finish is called only once. (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Rex authored Mar 5, 2021
1 parent f02328e commit 1ab38db
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Examples/Examples/Models/Examples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public protocol ExampleProtocol {
func finish()
}

private struct ExampleProtocolFinish {
static var postNotification: Void = {
// Lazy initialization to ensure that this is called once only.
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
let center = CFNotificationCenterGetDarwinNotifyCenter()
CFNotificationCenterPostNotification(center, CFNotificationName(Example.finishNotificationName as CFString), nil, nil, true)
}
}()
}

extension ExampleProtocol {
public func resourceOptions() -> ResourceOptions {
guard let accessToken = AccountManager.shared.accessToken else {
Expand All @@ -30,10 +40,7 @@ extension ExampleProtocol {
}

public func finish() {
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
let center = CFNotificationCenterGetDarwinNotifyCenter()
CFNotificationCenterPostNotification(center, CFNotificationName(Example.finishNotificationName as CFString), nil, nil, true)
}
_ = ExampleProtocolFinish.postNotification
}
}

Expand Down

0 comments on commit 1ab38db

Please sign in to comment.