2014-06-05 7 views
16
var session = NSURLSession.sharedSession() 
session.dataTaskWithRequest(urlRequest, 
          completionHandler: {(data: NSData!, 
               response: NSURLResponse!,      
               error: NSError!) in 
                 println(data) 
                 println(response) 
                 println(error) 
               }) 

Così sto facendo questa richiesta, e il blocco di completamento non viene mai chiamato.Blocco completamento NSURLSession non chiamato

Cosa c'è che non va?

Inoltre ho provato uno synchronous and asynchronous form della stessa richiesta con NSURLConnection e ha funzionato perfettamente.

EDIT:

ho provato l'assegnazione di una variabile dataTask al session.dataTaskWithRequest e visualizzato subito dopo. Dice questo <__NSCFLocalDataTask: 0xb41bda0> { suspended } sospeso? Perché?

risposta

49

Così ho provato a chiamare in questo modo

session.dataTaskWithRequest(urlRequest, 
         completionHandler: {(data: NSData!, 
              response: NSURLResponse!,      
              error: NSError!) in 
                print(data) 
                print(response) 
                print(error) 
              }).resume() 

e ha funzionato.

Sembra che devo chiamare resume() su un'attività di sessione sospesa predefinita.

+9

Sì. Questo è lo stesso in Swift o Objective C; tutte le attività di NSURLSession iniziano in uno stato sospeso e devono essere "riprese" per eliminarle. È basato su Objective C, ma [questo tutorial di NSURLSession da raywenderlich.com] (http://www.raywenderlich.com/51127/nsurlsession-tutorial) è il miglior esempio non-Apple del codice NSURLSession che ho incontrato, quindi dare un'occhiata che potrebbe aiutarti a capire come usarlo al meglio. –

+0

resume() you star - grazie –

+0

non mi ha aiutato affatto :( –

2

È anche possibile utilizzarlo semplicemente: -

let url = "api url" 

let nsURL = NSURL 

let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL) { 
(data, response, error) in 
    // your condition on success and failure 
} 

task.resume() 
+0

Questo funziona per me –

7

Si sta utilizzando campi da gioco ??

Se si, si deve fare attenzione ad includere:

XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true) 

Al fine di rendere il parco giochi di attesa per la richiamata

+1

È necessario anche "importare XCPlayground". –

0

Sarà qualcosa di simile in 2.x Swift

NSURLSession.sharedSession().dataTaskWithRequest(theRequest) { (data, response , error) in 
    print(response) 
}.resume() 
1

Questo è un caso abbastanza unico, ma se si sta utilizzando un URLSession all'interno di un test di unità, è necessario impostare le aspettative. In caso contrario, il test case terminerà e sarà visualizzato che la richiesta non verrà mai restituita. Swift 3.0.1.

let expect = expectation(description: "dataTaskWithRequest - completes") 

    if let url = URL(string: "https://www.google.com/") { 

     let request = URLRequest(url: url) 

     URLSession.shared.dataTask(with: request) { (data, response, error) in 

      print(data.debugDescription) 
      print(response.debugDescription) 
      print(error.debugDescription) 

      expect.fulfill() 

     }.resume() 

     waitForExpectations(timeout: 10, handler: nil) 
    } 
1

io faccia lo stesso problema e ho risolto da

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) { 

    if (!error) 
    { 
     NSLog(@"Data is %@",data); 
     NSLog(@"Response is %@",response); 
     NSLog(@"Error is %@",error); 
    } 
}]; 

[dataTask resume]; 

e verificare che si sono aggiunte le App Transport Security Settings nel vostro info.plist.