I have a recursive function with an async request. I want to save in an array if the requests were successful, but i don't know how. Concrete it is a function to upload files and if the function get a folder than the files inside this folder should also be uploaded.
I thought about implement this with a completionHandler, something about this:
func uploadFiles(pathToFile: NSURL) ->[Bool]{
var suc: [Bool] = [Bool]()
self.uploadFileRec(pathToFile, suc: &suc){
(result: [Bool]) in
println(result)
}
return suc
}
func uploadFilesRec(pathToFile: NSURL, inout suc: [Bool], completionHandler: (result: [Bool]) -> Void){
var isDir: ObjCBool = ObjCBool(true)
var manager: NSFileManager = NSFileManager.defaultManager()
manager.fileExistsAtPath(pathToFile.path!, isDirectory: &isDir)
if(isDir){
var error: NSError? = nil
let contents = manager.contentsOfDirectoryAtPath(pathToFile.path!, error: &error) as! [String]
for fileName in contents {
if(fileName != ".DS_Store"){
var pathString = pathToFile.path! + "/" + fileName
var updatePathtoFile = NSURL(fileURLWithPath: pathString)
self.uploadFilesRec(updatePathtoFile!, suc: &suc, completionHandler: completionHandler)
completionHandler(result: suc)
}
}
}
else{
asyncFileUpload(...){
...
suc.append(/*successful or not*/)
}
}
}
But the problem is that println get call not only one time but as many as uploadFileRec get called inside. So if i would call an another function instead of println, the function would be also called many times. So i think the idea with completionHandler was wrong. How else can i realize this?