Skip to content

Instantly share code, notes, and snippets.

@kidap
Last active January 22, 2018 21:00
Show Gist options
  • Select an option

  • Save kidap/fc04643fee1bd0535451088f5b26d464 to your computer and use it in GitHub Desktop.

Select an option

Save kidap/fc04643fee1bd0535451088f5b26d464 to your computer and use it in GitHub Desktop.
createOperation
`
fileprivate func createOperation(for job: Job) {
let jobUUID = job.objectID
guard let printer = job.printer else { return }
guard let operationQueue = operationQueue(of: printer) else { return }
let operation = BlockOperation.init {
let operationContext = FalconRecord.container.newBackgroundContext()
var operationJobDidChange = false
operationContext.performAndWait {
defer {
if operationContext.hasChanges || operationJobDidChange {
IncrementalStoreServerManager.sharedManager.incrementalStoreServer.saveAndSendNotifications(contextWithChanges: operationContext)
}
}
operationContext.automaticallyMergesChangesFromParent = true
guard let operationJob = operationContext.object(with: jobUUID) as? Job else {
print("Cannot find job with UUID: \(jobUUID)")
return
}
guard let operationPrinter = operationJob.printer else {
if operationJob.isDeleted {
print("Job with UUID \(jobUUID) has been deleted")
} else {
print("Printer is missing for job with UUID: \(jobUUID)")
}
operationJob.status = .failed
if operationJob.status == .failed && operationJob.deleteOnFailure {
operationJob.delete()
}
return
}
operationJob.status = .printing
IncrementalStoreServerManager.sharedManager.incrementalStoreServer.saveAndSendNotifications(contextWithChanges: operationContext)
operationPrinter.printManager?.printJob(job: operationJob, using: operationPrinter) {
if operationJob.status == .printed || (operationJob.status == .failed && operationJob.deleteOnFailure) {
operationJob.delete()
} else if operationJob.status == .failed {
if let emergencyPrinter = operationPrinter.emergencyRedirectPrinter {
operationJob.status = .created
operationJob.printer = emergencyPrinter
}
}
operationJobDidChange = operationJob.isUpdated || operationJob.isDeleted
}
}
}
operation.queuePriority = job.priority
operationQueue.addOperation(operation)
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment