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 ViewModel: ObservableObject { | |
func importProducts() { | |
ProductAPI.getAll { result in | |
switch result { | |
case .success(let products): | |
self.persistenceController.container.performBackgroundTask { context in | |
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy | |
let batchInsert = NSBatchInsertRequest(entityName: "Product", objects: products) | |
do { | |
let result = try context.execute(batchInsert) as! NSBatchInsertResult | |
print(result) | |
} | |
catch { | |
let nsError = error as NSError | |
// TODO: handle errors | |
} | |
DispatchQueue.main.async { | |
objectWillChange.send() | |
// TODO: handle errors | |
try? resultsController.performFetch() | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment