solve core data conflict caused by updating password entity image
This commit is contained in:
parent
f1b14d794d
commit
83b9762122
2 changed files with 26 additions and 1 deletions
|
|
@ -178,7 +178,9 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
self?.passwordImage = image
|
||||
self?.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
|
||||
let imageData = UIImageJPEGRepresentation(image, 1)
|
||||
self?.passwordEntity?.setValue(imageData, forKey: "image")
|
||||
if let entity = self?.passwordEntity {
|
||||
PasswordStore.shared.updateImage(passwordEntity: entity, image: imageData)
|
||||
}
|
||||
case .failure(let error):
|
||||
print(error)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -364,6 +364,29 @@ class PasswordStore {
|
|||
}
|
||||
}
|
||||
|
||||
func updateImage(passwordEntity: PasswordEntity, image: Data?) {
|
||||
if image == nil {
|
||||
return
|
||||
}
|
||||
let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
||||
privateMOC.parent = context
|
||||
privateMOC.perform {
|
||||
passwordEntity.image = NSData(data: image!)
|
||||
do {
|
||||
try privateMOC.save()
|
||||
self.context.performAndWait {
|
||||
do {
|
||||
try self.context.save()
|
||||
} catch {
|
||||
fatalError("Failure to save context: \(error)")
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
fatalError("Failure to save context: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func erase() {
|
||||
Utils.removeFileIfExists(at: storeURL)
|
||||
Utils.removeFileIfExists(at: tempStoreURL)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue