diff --git a/Polyglot/Polyglot.swift b/Polyglot/Polyglot.swift index 159f1a1..617c644 100644 --- a/Polyglot/Polyglot.swift +++ b/Polyglot/Polyglot.swift @@ -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. @@ -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() diff --git a/PolyglotTests/PolyglotTests.swift b/PolyglotTests/PolyglotTests.swift index b36c9ab..3b37ff9 100644 --- a/PolyglotTests/PolyglotTests.swift +++ b/PolyglotTests/PolyglotTests.swift @@ -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)