Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Polyglot/Polyglot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public class Polyglot {
}

/**
Translates a given piece of text.
Translates a given piece of text asynchronously. Switch to the main thread within the callback
if you want to update your UI by using `dispatch_async(dispatch_get_main_queue()) { /* code */ }`.

- parameter text: The text to translate.
- parameter callback: The code to be executed once the translation has completed.
Expand Down Expand Up @@ -169,9 +170,7 @@ public class Polyglot {
translation = self.translationFromXML(xmlString)

defer {
dispatch_async(dispatch_get_main_queue()) {
callback(translation: translation)
}
callback(translation: translation)
}
}
task.resume()
Expand Down
6 changes: 4 additions & 2 deletions PolyglotTests/PolyglotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ class PolyglotTests: XCTestCase {

let polyglot: Polyglot = Polyglot(clientId: "myClientId", clientSecret: "myClientSecret")
polyglot.translate("Ik weet het niet") { translation in
XCTAssertEqual(translation, "I don't know")
expectation.fulfill()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
XCTAssertEqual(translation, "I don't know")
expectation.fulfill()
})
}

waitForExpectationsWithTimeout(1, handler: nil)
Expand Down