'Execute closure Swift when File Manager finish delate
I'm having a problem with the following code because the closure is async.
I need to get my "project" data after FileManager finish delete all my stuff in order to update my view.
When I run the code, I'm getting project but is not updated.
func delatePicture(imageToDel: ImageCustom, pp:Project?, closure: @escaping(Bool, _ imm: Project?)->()){
guard let project = pp else {
return
}
if let ind = variables.storage.firstIndex(where: { proj in
proj.id == project.id
}){
if let i = variables.storage[ind].images.firstIndex(where: { imm in
imm.id == imageToDel.id
}){
// WAIT File manager to remove item before use closure
do {
let xmlurl = self.variables.storage[ind].images[i].linkXml
let picturl = self.variables.storage[ind].images[i].linkImmage
try FileManager.default.removeItem(at: xmlurl)
try FileManager.default.removeItem(at: picturl)
DispatchQueue.main.async { // THIS CLOSURE NEED TO BE EXECUTE WHEN FILE MANAGER FINISH DEL
print("Closure 1")
let proj = self.variables.storage[ind]
closure(true, proj)
print("Closure 2")
}
}
catch let err {
print(err.localizedDescription)
print("err try")
closure(false, nil)
}
} else {
print("not find i")
closure(false, nil)
}
}else {
print("not find ind")
closure(false, nil)
}
print("Closure 3")
salvaJson()
}
usage:
.onLongPressGesture {
am.delatePicture(imageToDel: item, pp: project) { isDone,proj in
print("Closure 4")
self.project = proj
print("Closure 5")
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
