作爲我應用中身份驗證過程的一部分,用戶可以使用他們的Facebook帳戶登錄 - 我正在使用Facebook iOS SDK來處理此過程。一旦身份驗證完成,我向Facebook圖形API發出請求以獲取用戶配置文件數據(這是第一個異步請求)。第二個異步請求也是通過Facebook圖形API來請求安裝了應用程序的用戶朋友列表。Swift:按順序發出多個異步請求。如何等待以前的請求完成?
此函數中的最後和第三個請求向我開發的API發出異步POST請求,以發佈從Facebook收集的所有數據。最後,一旦完成,用戶被允許進入該應用程序。但事實並非如此,似乎Facebook請求在向API發出POST請求之前未完成,因此推送空白數據。我不介意Facebook前兩個請求以什麼順序完成,但是我需要在允許用戶訪問應用程序之前將數據成功發佈到API。我試過使用信號量和調度組,但是在查看控制檯時,事情並沒有以正確的順序運行,我可以從API數據庫中看到正在插入空值。
認證控制器
// Successful login, fetch faceook profile
let group = DispatchGroup()
group.enter()
// Redirect to tab bar controller should not happen until fetchProfile() has finished
// Redirect should not happen if fetchProfile() errors
self.fetchProfile()
group.leave()
// Redirect to tab bar controller
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController
self.present(tabBarController, animated: true, completion: nil)
更新的Facebook抓取簡介
// Facebook Profile Request
func fetchProfile() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
let user = appDelegate.user
var facebookFriends = [String?]()
do {
let results = try managedContext?.fetch(fetchRequest)
fetchedUser = results![0] as? NSManagedObject
}
catch {
print("Error fetching User entity")
return
}
let group = DispatchGroup()
print("Starting Step 1")
group.enter()
// Facebook Profile
let parameters = ["fields": "id, email, first_name, last_name, picture.width(500).height(500), birthday, gender"]
FBSDKGraphRequest(graphPath: "me", parameters: parameters).start { (connection, result, error) -> Void in
if error != nil {
print(error)
return
}
let result = result as? NSDictionary
if let providerID = result?["id"] as? String {
user.provider_id = providerID
self.fetchedUser!.setValue(providerID, forKey: "provider_id")
}
if let firstName = result?["first_name"] as? String {
user.first_name = firstName
self.fetchedUser!.setValue(firstName, forKey: "first_name")
}
if let lastName = result?["last_name"] as? String {
user.last_name = lastName
self.fetchedUser!.setValue(lastName, forKey: "last_name")
}
if let email = result?["email"] as? String {
user.email = email
self.fetchedUser!.setValue(email, forKey: "email")
}
if let picture = result?["picture"] as? NSDictionary, let data = picture["data"] as? NSDictionary, let url = data["url"] as? String {
user.avatar = url
self.fetchedUser!.setValue(url, forKey: "avatar")
}
if let birthday = result?["birthday"] as? String {
user.birthday = birthday
self.fetchedUser!.setValue(sqlDate, forKey: "birthday")
}
if var gender = result?["gender"] as? String {
user.gender = gender
self.fetchedUser!.setValue(gender, forKey: "gender")
}
group.leave()
print("Step 1 Done")
group.enter()
print("Starting Step 2")
// Facebook Friends Request
FBSDKGraphRequest(graphPath: "me/friends", parameters: ["fields": "id, first_name, last_name, picture"]).start { (connection, result, error) -> Void in
if error != nil {
print(error)
return
}
let result = result as! [String:AnyObject]
for friend in result["data"] as! [[String:AnyObject]] {
let id = friend["id"] as! String
facebookFriends.append(id)
}
group.leave()
print("Step 2 Done")
// User POST Request
var dictionary = self.fetchedUser?.dictionaryWithValues(forKeys: ["provider", "provider_id", "first_name", "last_name", "email", "avatar", "birthday", "gender"])
if facebookFriends.count > 0 {
dictionary?["friends"] = facebookFriends
}
let data = NSMutableDictionary()
data.setValuesForKeys(dictionary!)
//let semaphore = DispatchSemaphore(value: 2)
group.enter()
print("Starting Step 3")
do {
// Here "jsonData" is the dictionary encoded in JSON data
let jsonData = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
// Here "decoded" is of type `Any`, decoded from JSON data
let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
// Final dict
if let dictFromJSON = decoded as? [String:String] {
let endpoint = "http://endpoint.com/user"
let url = URL(string: endpoint)
let session = URLSession.shared
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.httpBody = try JSONSerialization.data(withJSONObject: dictFromJSON, options: [])
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
if error != nil {
//semaphore.signal()
group.leave()
print(error)
return
}
do {
// Save response
let json = try(JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? [String: AnyObject])
if let userID = json?["user_id"] {
user.user_id = userID as? Int
self.fetchedUser!.setValue(userID, forKey: "user_id")
}
if let friends = json?["friends"] , !(friends is NSNull){
user.friends = friends as? [String]
self.fetchedUser!.setValue(friends, forKey: "friends")
}
group.leave()
//semaphore.signal()
} catch let jsonError {
print(jsonError)
return
}
}).resume()
}
} catch {
print(error.localizedDescription)
}
// Wait to async task to finish before moving on
//_ = semaphore.wait(timeout: DispatchTime.distantFuture)
print("Step 3 Done")
}
}
}
讓我的一天,謝謝。謝謝你的解釋。 –
上面的實現工作得很好,但是在將數據發佈到API的步驟3中,我有一段可以捕獲任何錯誤(如果錯誤!= nil)。我一直在測試,看來即使在這個階段出現錯誤,代碼也會繼續。我添加了一些代碼,說明如果錯誤然後註銷用戶並顯示並提醒,但是這似乎不運行。我會用相關的代碼更新我的問題 - 如果您有任何建議。 –
@LeeRowbotham看起來你用新的代碼更新了你的問題,對嗎?爲了其他人來到這篇文章,你是否也可以在問題中包含原始代碼?最後,請您檢查控制檯是否有任何消息?我認爲你的錯誤可能在那裏。如果不是,請告訴我錯誤信息是什麼。 – Coder256