Accessing a variable outside a given function - swift

I tried the completion handler as suggested by Gurdev and got something. It does return the values of my array in the MAIN function. But the issue is the same: I have to use Sleep() function for 3 seconds for the HTTP request to complete. This is like hard coding as it could take less or more than 3 seconds to complete. When I remove the Sleep() command, I end up returning a VOID array.
The relevant code is pated below.
Thanks!
--------Web Class--------
import Foundation
import UIKit
class Web {
var ar1 = [Double]()
var ar2 = [Double]()
func getData(str: String, completion: (_ result: [[Double]]) -> Void) {
let request = NSMutableURLRequest(url: URL(string: str)!)
httpGet(request as URLRequest!){
(data, error) -> Void in
if error != nil {
print(error ?? "Error")
} else {
let delimiter = "\t"
let lines:[String] = data.components(separatedBy: CharacterSet.newlines) as [String]
for line in lines {
var values:[String] = []
if line != "" {
values = line.components(separatedBy: delimiter)
let str1 = (values[0])//FIRST COLUMN
let str2 = (values[1])//SECOND COLUMN
let db1 = NumberFormatter().number(from: str1)?.doubleValue
self.ar1.append(db1!)
let db2 = NumberFormatter().number(from: str2)?.doubleValue
self.ar2.append(db2!)
}
}
}
}//end of request
sleep(3) // This delay sends data to MAIN, Otherwise NOT
let dta = [self.ar1, self.ar2]
completion(dta)
}
func httpGet(_ request: URLRequest!, callback: #escaping (String, String?) -> Void) {
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {
(data, response, error) -> Void in
if error != nil {
callback("", error!.localizedDescription)
} else {
let result = NSString(data: data!, encoding:
String.Encoding.ascii.rawValue)!
callback(result as String, nil)
}
})
task.resume()
}
}
--------Web Class--------
-------Call In Class--------
Web().getData (str: "https://dl.dropboxusercontent.com/s/f6j7w7zeqaavzqw/s02.txt?dl=0")
{
(result: [[Double]]) in
let x = result[0]
let y = result[1]
}
-------Call In Class--------
Essentially, I am trying to access my variable "ar1" at a certain point in my code but I cannot. I am trying to return the value too but it returns NULL at that point.
What is wrong here ?
I tried like the below code :
import Foundation
import UIKit
class Web {
var ar1 = [Double]()
var ar2 = [Double]()
func getData(str: String) -> [Double] {
let request = NSMutableURLRequest(url: URL(string: str)!)
httpGet(request as URLRequest!){
(data, error) -> Void in
if error != nil {
print(error ?? "Error")
} else {
let delimiter = "\t"
let lines:[String] = data.components(separatedBy: CharacterSet.newlines) as [String]
for line in lines {
var values:[String] = []
if line != "" {
values = line.components(separatedBy: delimiter)
let str1 = (values[0])//FIRST COLUMN
let str2 = (values[1])//SECOND COLUMN
let db1 = NumberFormatter().number(from: str1)?.doubleValue
self.ar1.append(db1!)
let db2 = NumberFormatter().number(from: str2)?.doubleValue
self.ar2.append(db2!)
}
}
dump (self.ar1) // "ar1" accessible HERE (returns all elements)
}
}//end of request
//BUT I WANT TO RETURN "ar1" HERE !
// So that I can use it in my MAIN class
dump (self.ar1) // "ar1" not accessible here (returns ZERO elements)
return self.ar1
}
func httpGet(_ request: URLRequest!, callback: #escaping (String, String?) -> Void) {
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: {
(data, response, error) -> Void in
if error != nil {
callback("", error!.localizedDescription)
} else {
let result = NSString(data: data!, encoding:
String.Encoding.ascii.rawValue)!
callback(result as String, nil)
}
})
task.resume()
}
}

In your code you are returning the ar1 from the return statement of your method whereas the value to ar1 is being set in Aysnchronous callback completion handler. You should use the completion handler block where you are trying to access the value of ar1. HTTPGet function is running in Async mode and value in ar1 is set in the callback handler block.
Let me know if you need any further help on this.

Related

Function runs twice if nested async calls are executed and once otherwise. Need help pre-determining when this will happen

func handleGetAllPhotoURLs is called from the line below and I have confirmed that the line of code only executes once with breakpoints.
_ = FlickrClient.getAllPhotoURLs(currentPin: self.currentPin, fetchCount: fetchCount, completion: self.handleGetAllPhotoURLs(pin:urls:error:))
According to output from my print statements, the function runs twice because it prints two lines of output if urls.count is non-zero. However, if urls.count is zero then I only get one print statement that states "urls.count ---> 0"
handleGetAllPhotoURLs ---> urls.count ---> 0 //this line is always printed
handleGetAllPhotoURLs ---> urls.count ---> 21 //this line is only printed if the urls parameter is not empty
func handleGetAllPhotoURLs(pin: Pin, urls: [URL], error: Error?){
print("handleGetAllPhotoURLs ---> urls.count ---> \(urls.count)")
let backgroundContext: NSManagedObjectContext! = dataController.backGroundContext
if let error = error {
print("func mapView(_ mapView: MKMapView, didSelect... \n\(error)")
return
}
let pinId = pin.objectID
backgroundContext.perform {
let backgroundPin = backgroundContext.object(with: pinId) as! Pin
backgroundPin.urlCount = Int32(urls.count)
try? backgroundContext.save()
}
for (index, currentURL) in urls.enumerated() {
URLSession.shared.dataTask(with: currentURL, completionHandler: { (imageData, response, error) in
guard let imageData = imageData else {return}
connectPhotoAndPin(dataController: self.dataController, currentPin: pin , data: imageData, urlString: currentURL.absoluteString, index: index)
}).resume()
}
}
In addition, I have a UILabel that only reveals itself when urls.count is zero and I only want to reveal it when urls is empty.
Right now, if urls is not empty, the app is very quickly flashing the empty message UILabel. Which now makes sense to me because print statement shows that urls array is temporarily empty.
Is there a way for me to determine to avoid flashing the empty message UILabel to user when urls.count is non-zero?
edit: Added code below based on request. The function below is called to obtain [URL] in completion handler. Then the completion handler is fed into:
func handleGetAllPhotoURLs(pin: Pin, urls: [URL], error: Error?)
class func getAllPhotoURLs(currentPin: Pin, fetchCount count: Int, completion: #escaping (Pin, [URL], Error?)->Void)-> URLSessionTask?{
let latitude = currentPin.latitude
let longitude = currentPin.longitude
let pageNumber = currentPin.pageNumber
let url = Endpoints.photosSearch(latitude, longitude, count, pageNumber).url
var array_photo_URLs = [URL]()
var array_photoID_secret = [[String: String]]()
var array_URLString = [String]()
var array_URLString2 = [String]()
var count = 0
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let dataObject = data, error == nil else {
DispatchQueue.main.async {
completion(currentPin, [], error)
}
return
}
do {
let temp = try JSONDecoder().decode(PhotosSearch.self, from: dataObject)
temp.photos.photo.forEach{
let tempDict = [$0.id : $0.secret]
array_photoID_secret.append(tempDict)
let photoURL = FlickrClient.Endpoints.getOnePicture($0.id, $0.secret)
let photoURLString = photoURL.toString
array_URLString.append(photoURLString)
getPhotoURL(photoID: $0.id, secret: $0.secret, completion: { (urlString, error) in
guard let urlString = urlString else {return}
array_URLString2.append(urlString)
array_photo_URLs.append(URL(string: urlString)!)
count = count + 1
if count == temp.photos.photo.count {
completion(currentPin, array_photo_URLs, nil)
}
})
}
completion(currentPin, [], nil)
return
} catch let conversionErr {
DispatchQueue.main.async {
completion(currentPin, [], conversionErr)
}
return
}
}
task.resume()
return task
}
In the do block, you are calling completion twice. Please see the correction,
do {
let temp = try JSONDecoder().decode(PhotosSearch.self, from: dataObject)
if temp.photos.photo.isEmpty == false {
temp.photos.photo.forEach{
let tempDict = [$0.id : $0.secret]
array_photoID_secret.append(tempDict)
let photoURL = FlickrClient.Endpoints.getOnePicture($0.id, $0.secret)
let photoURLString = photoURL.toString
array_URLString.append(photoURLString)
getPhotoURL(photoID: $0.id, secret: $0.secret, completion: { (urlString, error) in
guard let urlString = urlString else {return}
array_URLString2.append(urlString)
array_photo_URLs.append(URL(string: urlString)!)
count = count + 1
if count == temp.photos.photo.count {
completion(currentPin, array_photo_URLs, nil)
}
})
}
} else {
completion(currentPin, [], nil)
}
return
}

Swift calling completion handler in from another file fails

I am calling a funciton with completio=n handler from one calss to another class
Called class:
class PVClass
{
var avgMonthlyAcKw:Double = 0.0
var jsonString:String!
func estimateMonthlyACkW (areaSqFt:Float, completion: #escaping(Double) -> () ){
var capacityStr:String = ""
let estimatedCapacity = Float(areaSqFt/66.0)
capacityStr = String(format: "%.2f", estimatedCapacity)
// Build some Url string
var urlString:String = "https://developer.nrel.gov/"
urlString.append("&system_capacity=")
urlString.append(capacityStr)
let pvURL = URL(string: urlString)
let dataTask = URLSession.shared.dataTask(with: pvURL!) { data, response, error in
do {
let _ = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
self.jsonString = String(data: data!, encoding: .utf8)!
print("JSON String:\(String(describing: self.jsonString))")
if self.jsonString != nil {
let decoder = JSONDecoder()
let jsonData = try decoder.decode(PVClass.Top.self, from: data!)
// do some parsing here
var totalAcKw: Double = 0.0
let cnt2: Int = (jsonData.Outputs?.ACMonthly.count)!
for i in 0..<(cnt2-1) {
totalAcKw = totalAcKw + (jsonData.Outputs?.ACMonthly[i])!
}
self.avgMonthlyAcKw = Double(totalAcKw)/Double(cnt2)
// prints value
print("updated estimate: ", self.avgMonthlyAcKw)
}
} catch {
print("error: \(error.localizedDescription)")
}
}
dataTask.resume()
completion(self.avgMonthlyAcKw)
}
Calling Class:
func estimate() {
var estimatedSolarkWh:Double = 0.0
let aPVClass = PVClass()
aPVClass.estimateMonthlyACkW(areaSqFt: 100.0, completion: { (monthlyAckW) -> Void in
estimatedSolarkWh = monthlyAckW
self.view.setNeedsDisplay()
})
return
}
}
When I call the function estimate() the estimateMonthlyACkW function in the other PVClass is executed but it returns after the calling estimate() function is executed. So even though in the called function the URLsession is executed, json is parsed, and value is printed correctly - the value never gets gets transferred to the completion handler and the value never comes back to calling class. How can I fix this?
You need to move completion(self.avgMonthlyAcKw) just after print statement like below:
// prints value
print("updated estimate: ", self.avgMonthlyAcKw)
completion(self.avgMonthlyAcKw)
Hope this will helps you :)

Array is null after setting data in it

I have a JSON request that gets data from the Darksky API, I get the data properly and it is showing on the screen. However, When i'm trying to set the data from the array I get from the JSON call in another array, it stays empty.
This is my code:
just declaring the array:
var mForecastArray = [Weather]()
this is the function that calls the API:
func getForecast(){
Weather.forecast(withLocation: "37.8267,-122.4233") { (arr) in
DispatchQueue.main.async {
self.mForecastArray = arr
self.mTodayWeather = arr[0]
self.mCollectionView.reloadData()
}
}
}
The weird part is that it does work, and the data do shows on screen, but still, mForecastArray seems null.
This is the API call itself:
static func forecast(withLocation location: String, completion: #escaping ([Weather]) -> ()){
let url = basePath + location
let request = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
var forecastArray: [Weather] = []
if let data = data{
do{
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]{
if let dailyForecast = json["daily"] as? [String:Any]{
if let dailyData = dailyForecast["data"] as? [[String:Any]]{
for dataPoint in dailyData{
if let weatherObject = try? Weather(json: dataPoint){
forecastArray.append(weatherObject)
}
}
}
}
}
}catch{
print(error.localizedDescription)
}
completion(forecastArray)
}
}
task.resume()
}
It's a visual asynchronous illusion.
The static method forecast works asynchronously.
Most likely your code looks like
getForecast()
print(self.mForecastArray)
This cannot work because the array is populated much later.
Move the print line into the completion handler of the static method
func getForecast(){
Weather.forecast(withLocation: "37.8267,-122.4233") { (arr) in
DispatchQueue.main.async {
self.mForecastArray = arr
print(self.mForecastArray)
self.mTodayWeather = arr[0]
self.mCollectionView.reloadData()
}
}
}

Swift http request use urlSession

I want to write func for HTTP Request to my server and get some data, when i print it (print(responseString)) it looks good, but when i try to return data, its always empty
public func HTTPRequest(dir: String, param: [String:String]?) -> String{
var urlString = HOST + dir + "?"
var responseString = ""
if param != nil{
for currentParam in param!{
urlString += currentParam.key + "=" + currentParam.value + "&"
}
}
let url = URL(string: urlString)
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print("ERROR: HTTP REQUEST ERROR!")
return
}
guard let data = data else {
print("ERROR: Empty data!")
return
}
responseString = NSString(data: data,encoding: String.Encoding.utf8.rawValue) as! String
print(responseString)
}
task.resume()
return responseString
}
As mentioned in Rob's comments, the dataTask closure is run asynchronously. Instead of returning the value immediately, you would want to provide a completion closure and then call it when dataTask completes.
Here is an example (for testing, can be pasted to Xcode Playground as-is):
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let HOST = "http://example.org"
public func HTTPRequest(dir: String, param: [String: String]?, completion: #escaping (String) -> Void) {
var urlString = HOST + dir + "?"
if param != nil{
for currentParam in param! {
urlString += currentParam.key + "=" + currentParam.value + "&"
}
}
let url = URL(string: urlString)
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print("ERROR: HTTP REQUEST ERROR!")
return
}
guard let data = data else {
print("ERROR: Empty data!")
return
}
let responseString = NSString(data: data,encoding: String.Encoding.utf8.rawValue) as! String
completion(responseString)
}
task.resume()
}
let completion: (String) -> Void = { responseString in
print(responseString)
}
HTTPRequest(dir: "", param: nil, completion: completion)
You need to use completion block instead of returning value because the dataTask closure is run asynchronously, i.e. later, well after you return from your method. You don't want to try to return the value immediately (because you won't have it yet). You want to (a) change this function to not return anything, but (b) supply a completion handler closure, which you will call inside the dataTask closure, where you build responseString.
For example, you might define it like so:
public func HTTPRequest(dir: String, param: [String:String]? = nil, completionHandler: #escaping (String?, Error?) -> Void) {
var urlString = HOST + dir
if let param = param {
let parameters = param.map { return $0.key.percentEscaped() + "=" + $0.value.percentEscaped() }
urlString += "?" + parameters.joined(separator: "&")
}
let url = URL(string: urlString)
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard let data = data, error == nil else {
completionHandler(nil, error)
return
}
let responseString = String(data: data, encoding: .utf8)
completionHandler(responseString, nil)
}
task.resume()
}
Note, I'm percent escaping the values in the parameters dictionary using something like:
extension String {
/// Percent escapes values to be added to a URL query as specified in RFC 3986
///
/// This percent-escapes all characters besides the alphanumeric character set and "-", ".", "_", and "~".
///
/// http://www.ietf.org/rfc/rfc3986.txt
///
/// - Returns: Returns percent-escaped string.
func percentEscaped() -> String {
let allowedCharacters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~")
return self.addingPercentEncoding(withAllowedCharacters: allowedCharacters)!
}
}
And then you'd call it like so:
HTTPRequest(dir: directory, param: parameterDictionary) { responseString, error in
guard let responseString = responseString else {
// handle the error here
print("error: \(error)")
return
}
// use `responseString` here
DispatchQueue.main.async {
// because this is called on background thread, if updating
// UI, make sure to dispatch that back to the main queue.
}
}
// but don't try to use `responseString` here

Swift: Ensure urlSession.dataTask is completed in my function before passing result

Hello I have this function:
func planAdded(id:Int, user_id:Int) -> Int {
let locationURL = "myurl"
var planResult: Int = 0
let request = URLRequest(url: URL(string: locationURL)!)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: request, completionHandler:{
(data, response, error) -> Void in
DispatchQueue.main.async {
if let error = error {
print (error)
return
}
if let data = data {
let responseString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
planResult = responseString!.integerValue
}
}
})
task.resume()
print(planResult)
return planResult
}
What I am trying to do is to ensure that I got the result for planResult in tableView cellforrow at indexpath function.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
case 4:
if (result == 1){
...
} else if (result == 2){
...
} else {
...
}
default:
cell.fieldLabel.text = ""
}
return cell
}
Here is my viewDidLoad function
override func viewDidLoad() {
super.viewDidLoad()
self.result = self.planAdded(1, 2)
}
For some reasons, this keeps returning 0; however, the print line is actually printing correct value. I did some research and I believe this is because of asychonous call of the dataTask. Is there a way I ensure that my function is actually completed and return the value for the indexpath function?
Thanks
The reason is, you are doing it in a wrong way! Because, once you intialize the class the UIViewController lifecycle starts. Once the viewDidLoad() is called it the UITableView is also updated with no data.
Also, you are calling API to get the data, you need to notify UITableViewDataSource to update data and here is how you can do that!
func planAdded(id:Int, user_id:Int) {
let locationURL = "myurl"
var planResult: Int = 0
let request = URLRequest(url: URL(string: locationURL)!)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: request, completionHandler:{
(data, response, error) -> Void in
DispatchQueue.main.async {
if let error = error {
print (error)
return
}
if let data = data {
let responseString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
self.result = responseString!.integerValue
self.tableView.reloadData()
}
}
})
task.resume()
}
And you are getting zero value because it's an async method. So get the data you need to use completionCallback.
func planAdded(id:Int, user_id:Int, completion: (result: Int) -> ()) {
let locationURL = "myurl"
var planResult: Int = 0
let request = URLRequest(url: URL(string: locationURL)!)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: request, completionHandler:{
(data, response, error) -> Void in
DispatchQueue.main.async {
if let error = error {
print (error)
return
}
if let data = data {
let responseString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
planResult = responseString!.integerValue
completion(planResult)
}
}
})
task.resume()
}
Usage:
override func viewDidLoad() {
super.viewDidLoad()
planAdded(1, 2){(value) in
self.result = value
self.tableView.reloadData()
}
}