2017-01-18 2 views
5

Es ist mir bewusst, dass Alamofire-Anforderungen standardmäßig in einem Hintergrundthread ausgeführt werden.Alamofire-Netzwerkaufrufe werden nicht im Hintergrundthread ausgeführt

Als ich versuchte mit diesem Code:

let productsEndPoint: String = "http://api.test.com/Products?username=testuser" 

     Alamofire.request(productsEndPoint, method: .get) 
      .responseJSON { response in 
       // check for errors 
       guard response.result.error == nil else { 
        // got an error in getting the data, need to handle it 
        print("Inside error guard") 
        print(response.result.error!) 
        return 
       } 

       // make sure we got some JSON since that's what we expect 
       guard let json = response.result.value as? [String: Any] else { 
        print("didn't get products as JSON from API") 
        print("Error: \(response.result.error)") 
        return 
       } 

       // get and print the title 
       guard let products = json["products"] as? [[String: Any]] else { 
        print("Could not get products from JSON") 
        return 
       } 
       print(products) 

     } 

Die Benutzeroberfläche war nicht mehr reagiert, bis alle Elemente aus dem Netzanruf Druckvorgang abgeschlossen haben; Also habe ich versucht, GCD mit Alamofire zu verwenden:

und die Benutzeroberfläche reagiert immer noch nicht wie zuvor.

Mache ich hier irgendetwas falsch oder liegt der Fehler bei Alamofire?

Antwort

8

Ich habe mich geirrt, dass Alamofire standardmäßig auf einem Hintergrundthread läuft. Es wird standardmäßig in der Hauptwarteschlange ausgeführt. Ich habe ein Problem auf Alamofire der Github Seite geöffnet und es wird hier gelöst: https://github.com/Alamofire/Alamofire/issues/1922

Der richtige Weg, um mein Problem zu lösen war, um anzugeben, welche Art von Schlange mag ich meine Anfrage zur Verwendung der „Warteschlange“ ausgeführt werden Parameter auf der .responseJSON Methode (

.responseJSON(queue: queue) { response in 
... 
} 

)

Dies ist die gesamte korrigierte Version meines Code:

let productsEndPoint: String = "http://api.test.com/Products?username=testuser" 

    let queue = DispatchQueue(label: "com.test.api", qos: .background, attributes: .concurrent) 

    Alamofire.request(productsEndPoint, method: .get) 
     .responseJSON(queue: queue) { response in 
      // check for errors 
      guard response.result.error == nil else { 
       // got an error in getting the data, need to handle it 
       print("Inside error guard") 
       print(response.result.error!) 
       return 
      } 

      // make sure we got some JSON since that's what we expect 
      guard let json = response.result.value as? [String: Any] else { 
       print("didn't get products as JSON from API") 
       print("Error: \(response.result.error)") 
       return 
      } 

      // get and print the title 
      guard let products = json["products"] as? [[String: Any]] else { 
       print("Could not get products from JSON") 
       return 
      } 
      print(products) 
    } 
+4

Ich denke, Alamofire Anfrage läuft standardmäßig auf einem Hintergrund-Thread, und der Antwort-Handler wird auf dem Haupt-Thread ausgeführt .. Beachten Sie, weil es jetzt scheint, Sie Ihren Antwort-Handler auf einem Hintergrund-Thread (und wenn Sie irgendwelche sind Änderungen an der Benutzeroberfläche müssen Sie auf den Haupt-Thread wechseln) –

+0

@ FidelEduardoLópez das ist richtig, aber was ist, wenn wir jedes Mal auf Antwort neu laden wollen .. manchmal zeigt Index out of bound? –

Verwandte Themen