I am writing a translator app that connects with an online translation API. Due to size limitations in the API, I have written my program to send text one sentence at a time, and then join the translations together. I have looped
let serialQueue = DispatchQueue(label: "translationQueue")
for line in lines {
serialQueue.async {
print("line is: " + line)
var jpText = String(line)
if jpText.isEmpty {
jpText = "\n"
}
let escapedStr = jpText.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))
let urlStr:String = ("https://api.mymemory.translated.net/get?q="+escapedStr!+"&langpair="+langStr!)
let url = URL(string: urlStr)
// Creating Http Request
let request = NSURLRequest(url: url!)
// If empty, don't feed to translator.
if escapedStr!.isEmpty {
//translatedLines.append("\n")
self.enTextView.text = translatedLines
}
else {
let configuration = URLSessionConfiguration.default
configuration.waitsForConnectivity = true
let defaultSession = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
var dataTask: URLSessionDataTask?
let group = DispatchGroup()
group.enter()
dataTask?.cancel()
dataTask = defaultSession.dataTask(with: request as URLRequest) { [weak self] data, response, error in
if let error = error {
// self?.errorMessage += "DataTask error: " + error.localizedDescription + "\n"
print("DataTask error: " + error.localizedDescription + "\n")
} else if
let data = data,
let response = response as? HTTPURLResponse,
response.statusCode == 200 {
let jsonDict: NSDictionary!=((try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSDictionary)
// if(jsonDict.value(forKey: "responseStatus") as! NSNumber == 200){
let responseData: NSDictionary = jsonDict.object(forKey: "responseData") as! NSDictionary
group.leave()
var translatedString = String()
translatedString = responseData.object(forKey: "translatedText") as! String
let data = translatedString.data(using: .utf8)
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
guard let attributedString = try? NSAttributedString(data: data!, options: options, documentAttributes: .none) else {
return
}
let decodedString = attributedString.string
print("decoded: " + decodedString)
translatedLines.append(decodedString)
translatedLines.append("\n")
DispatchQueue.main.async {
self?.enTextView.text = translatedLines
}
}
}
dataTask?.resume()
group.wait()
}
}
}
But the translation output comes out in a random order. I broadly understand that there are concurrent requests being sent. But what can I do in my for-loop to make sure the entire send/receive happens before moving to the next iteration?
I would use the following technique:
Result
type comes in very handy for this use case.DispatchGroup
.Here's a quick playground code loosely based on yours that uses the tecnhique I described above:
A few notes:
translationResults
array on the same queue, that's why I'm callingDispatchQueue.main.async
every time. Not doing it may result in weird crashes because arrays are not thread-safe in Swift.