Swift:按顺序多个异步请求。如何等待上一个请求完成?
作为我应用程序中身份验证过程的一部分,用户可以使用他们的Facebook帐户登录-我正在使用Facebook iOS SDK来处理此过程。身份验证完成后,我向Facebook graph api发出请求,以获取用户配置文件数据(这是第一个异步请求)。第二个异步请求也是向Facebook graph api请求安装了应用程序的用户好友列表 此函数中的最后一个和第三个请求向我开发的API发出异步POST请求,该API用于发布从Facebook收集的所有数据。最后,完成此操作后,允许用户进入应用程序。然而事实并非如此,Facebook的请求似乎没有在向API发送POST请求之前完成,因此它推送了空白数据。我不介意前两个Facebook请求的完成顺序,但是我需要在允许用户进入应用程序之前将数据成功发布到API。我尝试过使用信号量和调度组,但是当查看控制台时,事情并没有以正确的顺序运行,我可以从API数据库中看到正在插入空值 身份验证控制器Swift:按顺序多个异步请求。如何等待上一个请求完成?,swift,api,http,facebook-graph-api,asynchronous,Swift,Api,Http,Facebook Graph Api,Asynchronous,作为我应用程序中身份验证过程的一部分,用户可以使用他们的Facebook帐户登录-我正在使用Facebook iOS SDK来处理此过程。身份验证完成后,我向Facebook graph api发出请求,以获取用户配置文件数据(这是第一个异步请求)。第二个异步请求也是向Facebook graph api请求安装了应用程序的用户好友列表 此函数中的最后一个和第三个请求向我开发的API发出异步POST请求,该API用于发布从Facebook收集的所有数据。最后,完成此操作后,允许用户进入应用程序。
// 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")
}
}
}
//Facebook个人资料请求
func fetchProfile(){
让appDelegate=UIApplication.shared.delegate为!appDelegate
让managedContext=appDelegate.managedObjectContext
let fetchRequest=NSFetchRequest(entityName:“用户”)
让user=appDelegate.user
var facebookFriends=[String?]()
做{
让结果=尝试managedContext?.fetch(fetchRequest)
fetchedUser=results![0]作为?NSManagedObject
}
抓住{
打印(“获取用户实体时出错”)
返回
}
let group=DispatchGroup()
打印(“开始步骤1”)
group.enter()
//Facebook个人资料
let parameters=[“fields”:“id、电子邮件、名字、姓氏、图片、宽度(500)、高度(500)、生日、性别”]
FBSDKGraphRequest(graphPath:“me”,参数:parameters)。开始{(连接、结果、错误)->Void in
如果错误!=nil{
打印(错误)
返回
}
让结果=结果为?NSDictionary
如果让providerID=result?[“id”]作为字符串{
user.provider\u id=providerID
self.fetchedUser!.setValue(providerID,forKey:“provider\u id”)
}
如果让firstName=result?[“first_name”]作为?字符串{
user.first\u name=firstName
self.fetchedUser!.setValue(firstName,forKey:“first_name”)
}
如果让lastName=result?[“last_name”]作为?字符串{
user.last_name=lastName
self.fetchedUser!.setValue(lastName,forKey:“last_name”)
}
如果让email=result?[“email”]作为?字符串{
user.email=电子邮件
self.fetchedUser!.setValue(电子邮件,forKey:“电子邮件”)
}
如果让picture=result?[“picture”]作为NSDictionary,让data=picture[“data”]作为NSDictionary,让url=data[“url”]作为字符串{
user.avatar=url
self.fetchedUser!.setValue(url,forKey:“化身”)
}
如果让生日=结果?[“生日”]作为?字符串{
user.birth=生日
self.fetchedUser!.setValue(sqlDate,forKey:“生日”)
}
如果变量性别=结果?[“性别”]为?字符串{
user.gender=性别
self.fetchedUser!.setValue(性别,forKey:“性别”)
}
小组请假()
打印(“步骤1完成”)
group.enter()
打印(“开始步骤2”)
//Facebook好友请求
FBSDKGraphRequest(graphPath:“我/朋友”,参数:[“字段”:“id,名字,姓氏,图片”])。开始{(连接,结果,错误)->在
如果错误!=nil{
打印(错误)
返回
}
让result=结果为![字符串:AnyObject]
对于结果中的好友[“数据”]为![[String:AnyObject]]{
让id=friend[“id”]作为!字符串
facebookFriends.append(id)
}
小组请假()
打印(“第2步完成”)
//用户POST请求
var dictionary=self.fetchedUser?.dictionaryWithValue(forKeys:[“提供商”、“提供商id”、“名字”、“姓氏”、“电子邮件”、“化身”、“生日”、“性别”])
如果facebookFriends.count>0{
字典?[“朋友”]=facebookFriends
}
let data=NSMutableDictionary()
data.setValuesForKeys(字典!)
//让信号量=分派信号量(值:2)
group.enter()
打印(“开始步骤3”)
做{
//这里的“jsonData”是用JSON数据编码的字典
让jsonData=try JSONSerialization.data(使用jsonObject:data,选项:。预打印)
//这里的“decoded”是“Any”类型,从JSON数据解码
let decoded=尝试JSONSerialization.jsonObject(使用:jsonData,选项:[])
//最后的格言
如果让dictFromJSON=解码为?[String:String]{
让端点=”http://endpoint.com/user"
让url=url(字符串:端点)
让session=URLSession.shared
var request=URLRequest(url:url!)
request.httpMethod=“POST”
request.httpBody=try JSONSerialization.data(使用JSONObject:dictFromJSON,选项:[])
request.addValue(“application/json”,forHTTPHeaderField:“Accept”)
request.addValue(“应用程序/json”,forHTTPHeaderField:“内容类型”)
dataTask(with:request,completionHandler:{(数据,响应,错误)->中的Void
如果错误!=n
// 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")
}
}
}