Last active
May 18, 2018 13:03
-
-
Save simme/1fcdbae7aeb68ae8400f0412c372745b to your computer and use it in GitHub Desktop.
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
private var didBeginImport = false // This is a hack until I've figured out why completion block runs twice.. | |
private func createImportOperations(data: [String: AnyObject]) { | |
guard !didBeginImport else { return } | |
didBeginImport = true | |
if let ingredients = data["ingredients"] as? [[String: AnyObject]] { | |
ingredients.forEach { | |
let op = IngredientOperation(data: $0, repository: repo, state: state) | |
if let id = op.identifier { | |
state.ingredientOperations.updateValue(op, forKey: id) | |
} | |
operationQueue.addOperation(op) | |
} | |
} | |
if let sources = data["sources"] as? [[String: AnyObject]] { | |
sources.forEach { | |
let op = SourceOperation(data: $0, repository: repo, state: state) | |
if let id = op.identifier { | |
state.sourceOperations.updateValue(op, forKey: id) | |
} | |
operationQueue.addOperation(op) | |
} | |
} | |
if let categories = data["categories"] as? [[String: AnyObject]] { | |
categories.forEach { | |
let op = CategoryOperation(data: $0, repository: repo, state: state) | |
if let id = op.identifier { | |
state.categoryOperations.updateValue(op, forKey: id) | |
} | |
op.addDependencies(dependencies: state.sourceOperations(in: op.sourceDependencies)) | |
operationQueue.addOperation(op) | |
} | |
} | |
if let recipes = data["recipes"] as? [[String: AnyObject]] { | |
recipes.forEach { | |
let op = RecipeOperation(data: $0, repository: repo, state: state) | |
op.addDependencies(dependencies: state.sourceOperations(in: op.sourceDependencies)) | |
op.addDependencies(dependencies: state.categoryOperations(in: op.categoryDependencies)) | |
op.addDependencies(dependencies: state.ingredientOperations(in: op.ingredientDependencies)) | |
operationQueue.addOperation(op) | |
} | |
} | |
} |
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
final class ImportState { | |
/// Pending and completed ingredient operations. | |
var ingredientOperations: [String: IngredientOperation] = [:] | |
/// Pending and completed source operations. | |
var sourceOperations: [String: SourceOperation] = [:] | |
/// Pending and completed category operations. | |
var categoryOperations: [String: CategoryOperation] = [:] | |
/// An array of errors that occured during import. | |
var errors: [Error] = [] | |
func ingredientOperations(in ingredients: Set<String>) -> [IngredientOperation] { | |
return ingredients.map { ingredientOperations[$0] }.flatMap { $0 } | |
} | |
func sourceOperations(in sources: Set<String>) -> [SourceOperation] { | |
return sources.map { sourceOperations[$0] }.flatMap { $0 } | |
} | |
func categoryOperations(in categories: Set<String>) -> [CategoryOperation] { | |
return categories.map { categoryOperations[$0] }.flatMap { $0 } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment