Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Facebook FBSDK iOS 4.x(使用Swift)获取值?_Ios_Swift_Facebook_Closures_Fbsdk - Fatal编程技术网

从Facebook FBSDK iOS 4.x(使用Swift)获取值?

从Facebook FBSDK iOS 4.x(使用Swift)获取值?,ios,swift,facebook,closures,fbsdk,Ios,Swift,Facebook,Closures,Fbsdk,如何让所有函数在访问facebookData变量之前等待一段时间?由于网络调用是异步的,facebookData在从Facebook获取值之前被访问 请从信息中查找以下代码 func graphRequestToReturnUserData(graphParameters: Dictionary<String, String>) { let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath:

如何让所有函数在访问facebookData变量之前等待一段时间?由于网络调用是异步的,facebookData在从Facebook获取值之前被访问

请从信息中查找以下代码

func graphRequestToReturnUserData(graphParameters: Dictionary<String, String>) {

        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: graphParameters)
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
            if ((error) != nil) {
                print("Error: \(error.localizedDescription)") /*Error Handling*/
            }else {
                self.facebookData = result as! NSDictionary
            }
        })
    }

因为我已经找到了一个在我的情况下很有效的解决方案,所以我将把它贴在这里以供更多人阅读,并可能对其进行评论以进一步改进

这里的简单解决方案是使用属性观察器,并在属性更改后立即调用函数。不需要弄乱线程,也不需要使用信号量

解决方案代码现在如下所示:-

class facebookGraphRequest {

    static var facebookData = NSDictionary()

//Property Observer, willSet-didSet

    var facebookD = NSDictionary() {

        didSet {
            facebookGraphRequest.facebookData = facebookD           //Assigning values to the static class variable
            facebookUserSignUpLogIn().checkUserExistsOrNot()        //Calling a function from the other class
        }
    }


//Mark: GraphRequestForFetchingUserData

    func graphRequestToReturnUserData(graphParameters: Dictionary<String, String>) {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: graphParameters)
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
            if ((error) != nil) {
                print("Error: \(error.localizedDescription)")       //Error Handling
            } else {
                self.facebookD = result as! NSDictionary
            }
        })
    }
}
类facebookGraphRequest{
静态变量facebookData=NSDictionary()
//属性观察者,willSet-didSet
var facebookD=NSDictionary(){
迪塞特{
facebookGraphRequest.facebookData=facebookD//为静态类变量赋值
facebookUserSignUpLogIn().checkUserExistsOrNot()//从其他类调用函数
}
}
//Mark:GraphRequestForFetchingUserData
func graphRequestToReturnUserData(graphParameters:字典){
让graphRequest:FBSDKGraphRequest=FBSDKGraphRequest(graphPath:“me”,参数:graphParameters)
startWithCompletionHandler({(连接、结果、错误)->中的Void
如果((错误)!=nil){
打印(“错误:\(Error.localizedDescription)”//错误处理
}否则{
self.facebookD=结果为!NSDictionary
}
})
}
}
这里,第一部分是属性observer(),然后是获取用户数据的GraphAPI调用。很快,
facebookD
变量被设置为一个值;函数
checkUserExistsOrNot()
从类facebookUserSignUpLogIn调用,以执行进一步的操作

class facebookGraphRequest {

    static var facebookData = NSDictionary()

//Property Observer, willSet-didSet

    var facebookD = NSDictionary() {

        didSet {
            facebookGraphRequest.facebookData = facebookD           //Assigning values to the static class variable
            facebookUserSignUpLogIn().checkUserExistsOrNot()        //Calling a function from the other class
        }
    }


//Mark: GraphRequestForFetchingUserData

    func graphRequestToReturnUserData(graphParameters: Dictionary<String, String>) {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: graphParameters)
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
            if ((error) != nil) {
                print("Error: \(error.localizedDescription)")       //Error Handling
            } else {
                self.facebookD = result as! NSDictionary
            }
        })
    }
}