I am currently in the process of writing code that does the following things:
- Sends a request to the web service that starts compressing the database into a .zip file
- Repeatedly invokes a web service function for checking whether the database has been compressed and is ready for download
- Downloads the database using a NSURLSessionDownloadTask
- Unzips the file and stores it into the documents directory
- Renames the unzipped file into the desired name
Besides that, the entire process takes care of error handling to using callbacks i.e. closures.
The so far code is as follows:
func continueByRepeatedlyCheckingDatabaseZipReady() -> Void {
let conditionClosure : ((x : String) -> Bool) = {x in x == "OK"}
let repetitionClosure : ((x : String) -> String) = {result in String(result)}
RequestTask(method: DatabaseZipReady()).apply(5, c: conditionClosure, f:repetitionClosure,
onSuccess: {success in
self.startDownloadDatabase()
}, onFailure: {error in
self.failure(error: error)
})
}
func startDownloadDatabase() -> Void {
let databaseSourceURL : NSURL = RequestDatabaseURLForDownload().requestURL
DownloadTask(url: databaseSourceURL,
progressCallback: {received, total in
}, successCallback: {location in
self.unzippingDatabaseFile(location)
}, failureCallback: {error in
self.failure(error: error)
}).resume()
}
func unzippingDatabaseFile(location : NSURL) {
UnZip(sourceURL: location, destinationURL: FileManagement().documentsDirectory()).execute({
self.renameDatabaseNameIntoCRMDB()
self.deleteTemporalyDatabaseFileOnTheServer()
}, onFailure: {error in
self.failure(error: error)
})
}
And so forth...
However, what I want to avoid (for the sake of code testability and simplicity) is having a pipeline of functions as such. As a first step, I've taken a look at GoF and tried to find a pattern that would suit this problem. But, unfortunately none of them do fit this problem.
Therefore, does anyone have a good piece of advice onto how to break out the dependencies between these functions i.e. the behaviour? Also keep in mind that I'm trying to have my code as functional as possible.
Aucun commentaire:
Enregistrer un commentaire