-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add observer pattern and closure to update tableView after getting data.
Cleaning some code
- Loading branch information
Showing
8 changed files
with
79 additions
and
11 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
Binary file modified
BIN
+3.72 KB
(110%)
...codeproj/project.xcworkspace/xcuserdata/benoit.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// DynamicValue.swift | ||
// TemplateProject | ||
// | ||
// Created by Benoit PASQUIER on 13/01/2018. | ||
// Copyright © 2018 Benoit PASQUIER. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
typealias CompletionHandler = (() -> Void) | ||
class DynamicValue<T> { | ||
|
||
var value : T { | ||
didSet { | ||
self.notify() | ||
} | ||
} | ||
|
||
private var observers : [NSObject : CompletionHandler] = [NSObject : CompletionHandler]() | ||
|
||
init(_ value: T) { | ||
self.value = value | ||
} | ||
|
||
public func addObserver(_ observer: NSObject, completionHandler: @escaping CompletionHandler) { | ||
|
||
observers[observer] = completionHandler | ||
} | ||
|
||
public func addAndNotify(observer: NSObject, completionHandler: @escaping CompletionHandler) { | ||
self.addObserver(observer, completionHandler: completionHandler) | ||
self.notify() | ||
} | ||
|
||
private func notify() { | ||
observers.forEach({ $0.value() }) | ||
} | ||
|
||
deinit { | ||
observers.removeAll() | ||
} | ||
} |
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