2014-06-18 50 views
1

我有以下的代碼,但不能確定爲什麼它不運行:NSURLSession performSegueWithIdentifier沒有運行

NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *dataTask = [session dataTaskWithURL: 
[NSURL  URLWithString:myURLString] completionHandler:^ 
(NSData *data, NSURLResponse *response, NSError *error) 
{NSDictionary *json = 
    [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

[self performSegueWithIdentifier:@"login" sender:self];}]; 


[dataTask resume]; 

的preformSegueWithIdentifier工作外而不是括號內。但我使用NSUrlConnection做了類似的工作,並且工作正常。我是NSURLSession的新手,爲什麼performSegueWithIdentifier沒有在調用中運行。

任何想法?

+0

看看這個問題。 http://stackoverflow.com/questions/5023566/objective-c-calling-self-methodname-from-inside-a-block – Michael

回答

2

如果按照自己的方式創建NSURLSession對象,它將創建一個新的不是主隊列的串行操作隊列。 dataTaskWithURL:方法的完成處理程序在會話的隊列中運行,因此它不起作用,因爲您正嘗試在後臺隊列上執行UI任務。要修復它,請創建你的會話,

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; 
+0

感謝您的解釋,多數民衆贊成在工作。 – tau