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
// Custom RawRepresentable type for BestEnum | |
struct BestEnumRaw: ExpressibleByStringLiteral{ | |
let rawString:String? | |
init(){ | |
rawString = nil | |
} | |
init(stringLiteral value: String){ | |
rawString = value |
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
func testDownloaderCallsCompletionOnResponse(){ | |
// Arange | |
let url = URL(string: "https://address.com")! | |
let completionExpectation = expectation(description: "completionExpectation") | |
// Act | |
let downloader = Downloader(url:url, completionHandler:{ | |
completionExpectation.fulfill() | |
}) | |
networkLayer.responseWithJSON(json:{}) |
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
class ReferenceObserver{ | |
private let deinitHandler:()->() | |
init(deinitHandler:@escaping ()->()) { | |
self.deinitHandler = deinitHandler | |
} | |
deinit { | |
deinitHandler() | |
} | |
} |
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
// Arange | |
let url = URL(string: "https://address.com")! | |
let deinitExpectation = expectation(description: "deinit completionHandler") | |
var reference:ReferenceObserver? = ReferenceObserver(deinitHandler: deinitExpectation.fulfill) | |
// Act | |
let downloader = Downloader(url:url, completionHandler:{[capturedReference = reference] in | |
_ = capturedReference | |
XCTFail() | |
}) |
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
func testDownloaderDoesNotCallCompletionOnCancelledDownload(){ | |
// Arange | |
let url = URL(string: "https://address.com")! | |
let deinitExpectation = expectation(description: "deinit completionHandler") | |
var reference:ReferenceObserver? = ReferenceObserver(deinitHandler: deinitExpectation.fulfill) | |
let downloader = Downloader(url:url, completionHandler:{[capturedReference = reference] in | |
_ = capturedReference | |
XCTFail() | |
}) | |
reference = nil |
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
class ReferenceObserver{ | |
private let deinitHandler:()->() | |
private var allowedToDealloc = false | |
init(deinitHandler:@escaping ()->()) { | |
self.deinitHandler = deinitHandler | |
} | |
deinit { | |
guard allowedToDealloc else { | |
XCTFail("ReferenceObserver deallocated too early!") |
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
func testDownloaderDoesNotCallCompletionOnCancelledDownload(){ | |
// Arange | |
let url = URL(string: "https://address.com")! | |
let deinitExpectation = expectation(description: "deinit completionHandler") | |
let downloader:Downloader | |
do { | |
let reference = ReferenceObserver(deinitHandler: deinitExpectation.fulfill) | |
downloader = Downloader(url:url, completionHandler:{[capturedReference = reference] in | |
_ = capturedReference | |
XCTFail() |
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
let error = // some error received in a completion handler | |
switch (error) { | |
case .invalidValue: | |
// highlight textfield that user has to modify | |
case .noInternet: | |
// present an alert | |
case .sessionInvalidated: | |
// pass error to the parent layer (e.g. by delegate) | |
delegate.didObserveInvalidatedSession() | |
} |
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
public typealias HandleAction<T> = (T) throws -> () | |
public protocol ErrorHandleable: class{ | |
func `throw`(_ :Error, finally: @escaping (Bool)->Void) | |
func `catch`(action: @escaping HandleAction<Error>) -> ErrorHandleable | |
} |
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
let previousErrorHandler:ErrorHandleable = /// existing handler | |
// setup a handler | |
let errorHandler = previousErrorHandler.catch {error in | |
// Log all errors into console and pass it further | |
print(error) | |
throw error | |
}.catch { | |
// Stop propagation for all errors(no throwing) | |
} | |
... |