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 HelloWorldOperation: GroupOperation { | |
override init() { | |
super.init() | |
let op = TextOperation(text: "Hello") | |
let op2 = TextOperation(text: "World") | |
op2.addDependency(op) |
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 OperationObserver: NSObject { | |
init(operation: AsyncOperation) { | |
super.init() | |
operation.addObserver(self, forKeyPath: "finished", options: .new, context: nil) | |
} | |
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
guard let key = keyPath else { |
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 = Operation1() | |
let op2 = Operation2() | |
let adapter = BlockOperation() { [unowned op1, unowned op2] in | |
op2.data = op1.data | |
} | |
adapter.addDependency(op1) | |
op2.addDependency(adapter) |
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") | |
} |
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
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
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
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
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
class NetworkOperation: Operation { | |
var targetURL: String? | |
var resultData: Data? | |
var _finished = false | |
override var isFinished: Bool { | |
get { | |
return _finished | |
} | |
set (newValue) { | |
willChangeValue(for: \.isFinished) |