Skip to content

Instantly share code, notes, and snippets.

@yageek
Created July 5, 2016 08:44
Show Gist options
  • Save yageek/cd7f5b81c35b6bc4c80b5303523486d7 to your computer and use it in GitHub Desktop.
Save yageek/cd7f5b81c35b6bc4c80b5303523486d7 to your computer and use it in GitHub Desktop.
Operations Example
class GetStopsOperation: GroupOperation {
let completion: (inner: () throws -> Void) -> Void
init(context: NSManagedObjectContext, completion: (inner: () throws -> Void) -> Void) {
self.completion = completion
// Line Operations
let getLinesCall = API.GetLinesColors
let downloadLinesOp = DownloadOperation(call: getLinesCall)
let parseLinesOp = JSONUnmarshalOperation()
let importLinesOp = ImportStopOperation(context: context)
parseLinesOp.injectResultFromDependency(downloadLinesOp)
importLinesOp.injectResultFromDependency(parseLinesOp)
// Stops Operations
let getStopsCall = API.GetStops(stopCode: nil, stopName: nil, line: nil, latitude: nil, longitude: nil)
let downloadStopsOp = DownloadOperation(call: getStopsCall)
let parseStopsOp = JSONUnmarshalOperation()
let importStopsOp = ImportStopOperation(context: context)
parseStopsOp.injectResultFromDependency(downloadStopsOp)
importStopsOp.injectResultFromDependency(parseStopsOp)
// Synchronisation
importStopsOp.addDependency(importLinesOp)
super.init(operations: [])
name = "Get Stops operations"
addCondition(MutuallyExclusive<GetStopsOperation>())
addOperations(downloadLinesOp, parseLinesOp, importLinesOp, downloadStopsOp, parseStopsOp, importStopsOp)
}
override func operationDidFinish(errors: [ErrorType]) {
print("Errors: \(errors)")
self.completion {
if let error = errors.first {
throw error
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment