简体   繁体   English

方法始终返回Nil

[英]Method Always Returning Nil

I'm trying to get JSON data from the fitbit servers and print it. 我正在尝试从fitbit服务器获取JSON数据并进行打印。 When I run the first function I ALWAYS get nil. 当我运行第一个函数时,我总是得到nil。 However when I run the second function it prints the data correctly. 但是,当我运行第二个函数时,它将正确打印数据。 I'm guessing it has something to do with the time it takes for the iPhone to grab the JSON from the server, but am completely clueless on how to solve it. 我猜测这与iPhone从服务器获取JSON所花费的时间有关,但对于如何解决它完全一无所知。 My end goal is to return the data but I cannot obviously do it from a completion handler. 我的最终目标是返回数据,但是我显然不能从完成处理程序中返回数据。

static func getFitbitData(resource: ResourcePath, date: Date) -> Int? {
        var rawData: NSData?
        let request = oauth.request(forURL: NSURL(string: "https://api.fitbit.com/1/user/-/\(resource.rawValue)/date/\(date.toString())/1d.json")!)
        let task = oauth.session.dataTaskWithRequest(request) { data, response, error in
            if error != nil {
                print("[ERROR] An error occured during request: \(error)")
            }
            else {
                print("[SUCCESS] Data retrieved successfully")
                rawData = data!
            }
        }
        task.resume()
        return ParsingEngine.parseFitbitData(ResourcePath.calories, data: rawData!)
    }



static func getFitbitData(resource: ResourcePath, date: Date) -> Int? {
        let request = oauth.request(forURL: NSURL(string: "https://api.fitbit.com/1/user/-/\(resource.rawValue)/date/\(date.toString())/1d.json")!)
        let task = oauth.session.dataTaskWithRequest(request) { data, response, error in
            if error != nil {
                print("[ERROR] An error occured during request: \(error)")
            }
            else {
                print("[SUCCESS] Data retrieved successfully")
                print(ParsingEngine.parseFitbitData(ResourcePath.calories, data: data!))
            }
        }
        task.resume()
        return nil
    }

You're right that the function is returning before your data populates from the server. 没错,在从服务器填充数据之前,该函数正在返回。 Why can't you do it from a completion handler? 您为什么不能从完成处理程序中执行此操作? This should work: 这应该工作:

static func getFitbitData(resource: ResourcePath, date: Date, completion: (data: Int?) -> Void) {
        var rawData: NSData?
        let request = oauth.request(forURL: NSURL(string: "https://api.fitbit.com/1/user/-/\(resource.rawValue)/date/\(date.toString())/1d.json")!)
        let task = oauth.session.dataTaskWithRequest(request) { data, response, error in
            if error != nil {
                print("[ERROR] An error occured during request: \(error)")
            }
            else {
                print("[SUCCESS] Data retrieved successfully")
                let endData = ParsingEngine.parseFitbitData(ResourcePath.calories, data: data!)
                completion(endData)
            }
        }
        task.resume()
    }

You can call this like so: 您可以这样称呼:

FitbitService.getFitbitData(myResource, date: myDate) { data in
   //work with your data here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM