![](/img/trans.png)
[英]iOS Swift - NSURLConnection.sendAsynchronousRequest cancel request
[英]NSURLConnection.sendAsynchronousRequest issue
抱歉,標題不太清楚,但是很難用一句話來恢復問題。
我正在使用Facebook SDK來檢索用戶信息。 我的目的是實例化一個我自己編寫的名為Profile的類,其中包含一些用戶信息,包括頭像圖片的二進制文件( NSData
)。
為此,我首先使用FBRequestConnection.startWithGraphPath
,然后在完成處理程序中創建一個Profile實例,並使用NSURLConnection.sendAsynchronousRequest:queue:completionHandler:
請求化身二進制數據NSURLConnection.sendAsynchronousRequest:queue:completionHandler:
一旦獲得完整的配置文件,我就會使用didReceiveProfile:
通知委托。 現在,委托是LoginViewController
,它是UIViewController
的子類。 然后,此方法將配置文件數據保存到磁盤,然后執行以下操作:
var app: UIApplication = UIApplication.sharedApplication();
var delegate: AppDelegate = app.delegate as AppDelegate;
delegate.application(app, didReceiveProfile: profile);
因此,其想法是通知AppDelegate
一個Profile現在可用,因此可以顯示LoginViewController
任何視圖控制器。 這是該代碼:
var storyboard: UIStoryboard = UIStoryboard(name: MainStoryboardIdentifier, bundle: nil);
var profileVC: ProfileViewController! = storyboard.instantiateViewControllerWithIdentifier(ProfileViewControllerIdentifier) as ProfileViewController;
NSLog("Everything OK");
self.navigationController!.pushViewController(profileVC, animated: false);
我確實得到了“一切正常”的日志,但是視圖未顯示。
還有更多細節。
首次加載應用程序時,它將檢查配置文件是否已保存到磁盤。 如果包含的話,它將從文件中加載配置文件並調用didReceiveProfile:
它立即跳轉到第二個屏幕。 因此,調用了相同的方法並且它起作用。 不能是代碼。
另外,請注意,在FBRequestConnection.startWithGraphPath
的完成處理程序內部調用了NSURLConnection.sendAsynchronousRequest:queue:completionHandler:
FBRequestConnection.startWithGraphPath
如果我在 sendAsynchronousRequest
之前 (即,在Facebook的完成處理程序中)調用委托的sendAsynchronousRequest
didReceiveProfile:
方法,則該方法有效並且視圖已正確顯示,但是我沒有頭像二進制。 但是,如果我在sendAsynchronousRequest
的完成處理程序中調用它,則視圖不會顯示。 它只是返回到登錄視圖。
我是iOS開發的新手,但根據我的經驗,似乎在另一個線程中調用了完成處理程序? 都是異步的。
那么,如何顯示視圖?
謝謝。
好的,我意識到我遺漏了一些非常重要的東西。 一經發現,我也意識到了問題所在。 我這樣打電話給sendAsynchronousRequest
:
var request = NSURLRequest(URL: url);
let queue = NSOperationQueue();
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ /* RESPONSE CODE */ });
顯然,隊列不對。 它應該是:
let queue = NSOperationQueue.mainQueue();
那解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.