This file contains hidden or 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
| fetchPromise().then { value in | |
| // do something | |
| }.catch { error in { | |
| // in case of error | |
| } |
This file contains hidden or 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
| Promise() { fulfill, reject in | |
| return "Hello World" //or other async code | |
| } |
This file contains hidden or 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
| Promise() { fulfill, reject in | |
| // code | |
| } |
This file contains hidden or 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 NetworkOperation: Operation { | |
| var targetURL: String? | |
| var resultData: Data? | |
| var _finished = false | |
| override var isFinished: Bool { | |
| get { | |
| return _finished | |
| } | |
| set (newValue) { | |
| willChangeValue(for: \.isFinished) |
This file contains hidden or 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 class ApiRequestDelegate:NSObject, URLSessionDelegate, URLSessionTaskDelegate, | |
| URLSessionDataDelegate{ | |
| // called once as soon as a response returns | |
| public func urlSession(session: URLSession, dataTask: URLSessionDataTask, | |
| didReceiveResponse response: URLResponse, | |
| completionHandler: (URLSession.ResponseDisposition) -> Void) { | |
| // store Response to further process it and call completion Handler to continue | |
| } | |
This file contains hidden or 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
| URLSession.shared.dataTask(with: backendurl!) { data, _, error in | |
| if error != nil { | |
| print(error) | |
| return | |
| } | |
| let imageurl = String(data: data!, encoding: String.Encoding.utf8) as String! // For simplicity response is only the URL | |
| URLSession.shared.dataTask(with: URL(string:imageurl!)!) { (data, response, error) in | |
| if error != nil { | |
| print(error) |
This file contains hidden or 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
| URLSession.shared.dataTask(with: url!) { data, _, error in | |
| if error != nil { | |
| print(error) | |
| return | |
| } | |
| DispatchQueue.main.async { | |
| imageView.image = UIImage(data: data!) | |
| } | |
| }.resume() |
This file contains hidden or 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 op1 = Operation() | |
| op1.name = "Operation1" | |
| OperationQueue.main.addOperations([op1], waitUntilFinished:false) | |
| let operations = OperationQueue.main.operations | |
| operations.map { op in | |
| if op.name == "Operation1" { | |
| op.cancel() | |
| } |
This file contains hidden or 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
| import Foundation | |
| class UIOperation: AsyncOperation { | |
| let viewController: UIViewcontroller! | |
| override func execute() { | |
| let alert = UIAlertController(title: "My Alert", message: @"This is an alert.", preferredStyle: .alert) | |
| alert.addAction(UIAlertAction(title: "OK", style: .`default`, handler: { _ in | |
| self.handleInput() | |
| })) |
This file contains hidden or 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 GroupOperation: AsyncOperation { | |
| let queue = OperationQueue() | |
| var operations: [AsyncOperation] = [] | |
| var errors: [Error] = [] | |
| override func execute() { | |
| print("group started") | |
| queue.addOperations(operations, waitUntilFinished: true) | |
| print("group done") | |
| } |