I need to call func fillFields after that func getJsonData is over.
The func getJsonData is a Async Task for get data on server for URLRequest.
func getAPIData() {
let initial = URL(string: "http://10.0.0.2/Blower/app/api/inicial.php")
DispatchQueue.main.async {
_ = URLSession.shared.dataTask(with: initial!) { (dados, requisicao, erro) in
if requisicao != nil {}
if let dados = dados {
do {
let json = try JSONSerialization.jsonObject(with: dados, options: []) as! [String: Any]
/*
*
*/
} catch {
print(erro as Any)
}
}
}.resume()
}
}
How can I know if the function getAPIData is finished?
You can Identify with Completion handler when the task is complete like this.
func getAPIData(complition:#escaping (AnyObject?, Error?) -> Void) {
let initial = URL(string: "http://10.0.0.2/Blower/app/api/inicial.php")
DispatchQueue.main.async {
_ = URLSession.shared.dataTask(with: initial!) { (dados, requisicao, erro) in
if requisicao != nil {}
if let dados = dados {
do {
let json = try JSONSerialization.jsonObject(with: dados, options: []) as! [String: Any]
complition(json as AnyObject, nil) // When Complete task
// Call next function Here
} catch {
print(erro as Any)
complition(nil, erro)
}
} else {
complition(nil, erro)
}
}.resume()
}
}
Call like this
self.getAPIData { (response,error) in
print(response) // Your response is here after complete task
}
Related
I am starting UIActivityIndicatorView in a func and want to stop it on success, for some reason it dosent happening:
func imageCaller(url: String , success: #escaping (UIImage) -> Void, failure: #escaping () -> Void) {
self.imageLoaderIndicator.startAnimating()
let handler = AuthenticateHandler()
self.urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: handler, delegateQueue: OperationQueue.main)
self.imageThumbnailTask = urlSession?.dataTask(with: URL(string:url)!) { data, res, err in
if err != nil {
guard let dataResponse = data,
err == nil else {
print("error from dataResponse:\(err?.localizedDescription ?? "Response Error")")
return
}
if let imageData = data {
if let image = UIImage(data: imageData) {
success(image)
}
}
}
self.imageThumbnailTask?.resume()
}
the code above is the network call.
This one is the method calling:
func imageThumbnailcall() {
self.imageCaller( url: self.isShowingThermal ? self.thermalUrl : self.visualUrl, success: { (image) in
self.imageLoaderIndicator.stopAnimating()
self.backGroundImageView.image = image
if self.isInVC {
self.imageThumbnailcall()
}
}) {
self.imageLoaderIndicator.stopAnimating()
}
}
Also in the storyboard i checked "hide when stopped" and also tried wrapping it with DispatchQueue.main.async {}
You have a lot of branches in your function that doesn't call either the success or the failure blocks and you also need to stop the activity indicator in the failure callback. You should also make sure that you dispatch all UI related activity to the main thread.
func imageCaller(url: String , success: #escaping (UIImage) -> Void, failure: #escaping () -> Void) {
self.imageLoaderIndicator.startAnimating()
let handler = AuthenticateHandler()
self.urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: handler, delegateQueue: OperationQueue.main)
self.imageThumbnailTask = urlSession?.dataTask(with: URL(string:url)!) { data, res, err in
if err != nil {
guard let dataResponse = data, err == nil else {
print("error from dataResponse:\(err?.localizedDescription ?? "Response Error")")
failure()
return
}
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with: dataResponse, options: [])
print("erro after parsing data:\(jsonResponse)") //Response result
failure()
} catch let parsingError {
failure()
print("Error", parsingError)
}
} else if let imageData = data, let image = UIImage(data: imageData) {
success(image)
} else {
failure()
}
}
self.imageThumbnailTask?.resume()
}
And then call stopAnimating from the failure block as well:
func imageThumbnailcall() {
self.imageCaller(url: self.isShowingThermal ? self.thermalUrl : self.visualUrl, success: { image in
DispatchQueue.main.async{
self.imageLoaderIndicator.stopAnimating()
self.backGroundImageView.image = image
}
if self.isInVC {
self.imageThumbnailcall()
}
}, failure: { _ in
DispatchQueue.main.async{
self.imageLoaderIndicator.stopAnimating()
}
})
}
Not sure what is isInVC or how it is set, but you call the same method again based on its value, which might result in an infinite loop of imageThumbnailcall calling itself from the success completion handler.
Try;
func imageThumbnailcall() {
self.imageCaller( url: self.isShowingThermal ? self.thermalUrl : self.visualUrl, success: { (image) in
self.imageLoaderIndicator.stopAnimating()
self.backGroundImageView.image = image
if self.isInVC {
self.imageThumbnailcall()
}
}) {
self.imageLoaderIndicator.stopAnimating()
}
}
Callback of URLSession.dataTask is in background queue so
DispatchQueue.main.async {
self.imageLoaderIndicator.stopAnimating()
}
and make sure to handle both success and failure
I am trying to implement a retry mechanism and i saw that alamofire has one.
I am trying to implement a simple mechanism of retry with number of times for a request , yet something is wrong.
class OAuth2Handler: RequestAdapter, RequestRetrier {
func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
return urlRequest
}
var defaultRetryCount = 4
private var requestsAndRetryCounts: [(Request, Int)] = []
private var lock = NSLock()
private func index(request: Request) -> Int? {
return requestsAndRetryCounts.index(where: { $0.0 === request })
}
func addRetryInfo(request: Request, retryCount: Int? = nil) {
lock.lock() ; defer { lock.unlock() }
guard index(request: request) == nil else { print("ERROR addRetryInfo called for already tracked request"); return }
requestsAndRetryCounts.append((request, retryCount ?? defaultRetryCount))
}
func deleteRetryInfo(request: Request) {
lock.lock() ; defer { lock.unlock() }
guard let index = index(request: request) else { print("ERROR deleteRetryInfo called for not tracked request"); return }
requestsAndRetryCounts.remove(at: index)
}
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: #escaping RequestRetryCompletion){
lock.lock() ; defer { lock.unlock() }
guard let index = index(request: request) else { completion(false, 0); return }
let (request, retryCount) = requestsAndRetryCounts[index]
if retryCount == 0 {
completion(false, 0)
} else {
requestsAndRetryCounts[index] = (request, retryCount - 1)
completion(true, 0.5)
}
}
}
this is the class that i am trying to use this:
let sessionManager = SessionManager()
override init() {
sessionManager.adapter = RequestAdapter.self as? RequestAdapter
sessionManager.retrier = OAuth2Handler()
}
func sendRequest(url: String,meth: HTTPMethod,parameters: [String: AnyObject]?, success: #escaping (String, Data) -> Void, failure: #escaping (Error) -> Void) {
self.asyncSerialWorker.enqueueWork { (done) in
self.sessionManager.request(url, method:meth).responseJSON { (responseObject) -> Void in
if responseObject.result.isSuccess {
print("Generic succsess")
let value = responseObject.result.value
let json = JSON(value!)
guard let result = responseObject.data else {return}
success(self.parser.parseMaiden(json: json), result)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
print("login failed")
failure(error)
}
done()
}
}
}
if there are any other suggestions i would love to hear them
thanks
sessionManager.adapter = RequestAdapter.self as? RequestAdapter seems very wrong. You should be setting it to an instance of your OAuth2Handler.
So the issue her was to add the request to the retry, so first i did this:
let sessionManager = SessionManager()
var retrier = OAuth2Handler()
override init() {
sessionManager.retrier = retrier
}
and in the call itself i did as follow:
func sendRequest(url: String,meth: HTTPMethod,parameters: [String: AnyObject]?, success: #escaping (String, Data) -> Void, failure: #escaping (Error) -> Void) {
let request = sessionManager.request(url, method: meth, parameters: parameters, encoding: JSONEncoding.default)
retrier.addRetryInfo(request: request)
self.asyncSerialWorker.enqueueWork { (done) in
self.sessionManager.request(url, method:meth).responseJSON { (responseObject) -> Void in
if responseObject.result.isSuccess {
print("Generic succsess")
let value = responseObject.result.value
let json = JSON(value!)
guard let result = responseObject.data else {return}
success(self.parser.parseMaiden(json: json), result)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
print("login failed")
failure(error)
}
done()
}
}
}
as you can see i have add to the retry a request :
retrier.addRetryInfo(request: request)
maybe i should do a remove in success(will check and update)
I'm downloading remote JSON data and want my loading screen to stay up until the download is complete. Once my parse method finishes running, a segue should be called to move to the next view automatically.
I've verified that my data is properly downloading and parsing. My performSegue function is even being called when I throw up a breakpoint. But the application is still not moving to the next view.
Here's where I'm calling my parse method and then immediately calling the desired segue:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
downloadSources(atURL: "https://newsapi.org/v1/sources?language=en")
performSegue(withIdentifier: "loadingFinished", sender: self)
}
For reference, if you need it, here is my parse method in its entirety:
func downloadSources(atURL urlString: String) {
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
if let validURL = URL(string: urlString) {
var request = URLRequest(url: validURL)
request.setValue("49fcb8e0fa604e7aa461ee4f22124177", forHTTPHeaderField: "X-Api-Key")
request.httpMethod = "GET"
let task = session.dataTask(with: request) { (data, response, error) in
if error != nil {
assertionFailure()
return
}
guard let response = response as? HTTPURLResponse,
response.statusCode == 200,
let data = data
else {
assertionFailure()
return
}
do {
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
guard let sources = json["sources"] as? [[String: Any]]
else {
assertionFailure()
return
}
for source in sources {
guard let id = source["id"] as? String,
let name = source["name"] as? String,
let description = source["description"] as? String
else {
assertionFailure()
return
}
self.sources.append(Source(id: id, name: name, description: description))
}
}
}
catch {
print(error.localizedDescription)
assertionFailure()
}
}
task.resume()
}
}
Thanks in advance.
Sounds like a closure callback is what you want.
typealias CompletionHandler = ((_ success:Bool) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
downloadSources(atURL: "www.example.com", completion: {
if success {
performSegue(withIdentifier: "loadingFinished", sender: self)
return
}
// otherwise deal with failure
})
}
func downloadSources(atURL urlString: String, completion: CompletionHandler) {
if error != nil {
completion?(false)
return
}
// finish downlaod
completion?(true)
}
My moto is Auto search, I have been trying with URLSession, When i am trying to search slowly the requests are handled as expected(when there is no text the response is empty i mean the placesarray) but when i am trying to clear the text or hit any searchtext speedily then the previous request response are being appended in the placesarray. I tried with cancelling the previous request yet i am not getting the result(i.e previous response not be appended)
func autoSearch(text:String){
let urlRequest = URLRequest(url: self.getQueryFormedBingURL()!)
let session = URLSession.shared
session.getTasksWithCompletionHandler
{
(dataTasks, uploadTasks, downloadTasks) -> Void in
// self.cancelTasksByUrl(tasks: dataTasks as [URLSessionTask])
self.cancelTasksByUrl(tasks: uploadTasks as [URLSessionTask])
self.cancelTasksByUrl(tasks: downloadTasks as [URLSessionTask])
}
let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) -> Void in
print("response \(response)")
if let data = data {
let json = try? JSONSerialization.jsonObject(with: data, options: [])
if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {
if let jsonDic = json as? NSDictionary {
let status = jsonDic.returnsObjectOrNone(forKey: "statusCode") as! Int
if status == 200 {
if let resourceSetsArr = jsonDic.returnsObjectOrNone(forKey: "resourceSets") as? NSArray {
if let placesDict = resourceSetsArr.object(at: 0) as? NSDictionary {
if let resourceArr = placesDict.object(forKey: "resources") as? NSArray, resourceArr.count > 0 {
if let _ = self.placesArray {
self.placesArray!.removeAll()
}
for loopCounter in 0...resourceArr.count - 1 {
let modalClass:BingAutoCompletePlace = BingAutoCompletePlace(responseDict: resourceArr[loopCounter] as! NSDictionary)
self.placesArray?.append(modalClass)
}
completion(self.placesArray!)
}
else { //URL Success, where there no places with the given search string
completion([])
}
}
}
}
}
}
else if let response = response as? HTTPURLResponse , 400...499 ~= response.statusCode {// When url fails
if let _ = error {
print("error=\(error!.localizedDescription)")
}
completion([])
}
else {
if let _ = error {
print("error=\(error!.localizedDescription)")
}
completion([])
}
}
})
task.resume()
}
//Request cancellation
private func cancelTasksByUrl(tasks: [URLSessionTask]) {
for task in tasks
{
task.cancel()
}
}
Unfortunately, the framework does not guarantee any order in which tasks finish -- because this depends on the running time. It could also be that you're in a completion handler of a currently cancelled task.
To circumvent this, you could do the following:
Create a private instance variable to store the most-recent task
Cancel everything else as before
In the completion handler
check if the task is still the most recent task (like if (task !== self.currentTask) {return})
create a local Array to store the data
Update the view controllers array in the main thread (DispatchQueue.main.async(...))
I cleaned up you code a litte (using guard statments to minimize the nesting). Maybe you should also
Empty the array in all the error / empty cases (instead of simple return from the guard statement)
hand-in the task to the completion call and check there again if the task is still the currentTask. This would also be a good way to reset currentTask to nil.
Just adopt it to your needs :-)
var currentTask:URLSessionDataTask?
func autoSearch(text:String){
let completion:(_ x:[AnyObject])->() = {_ in }
let urlRequest = URLRequest(url: self.getQueryFormedBingURL()!)
let session = URLSession.shared
session.getTasksWithCompletionHandler
{
(dataTasks, uploadTasks, downloadTasks) -> Void in
// self.cancelTasksByUrl(tasks: dataTasks as [URLSessionTask])
self.cancelTasksByUrl(tasks: uploadTasks as [URLSessionTask])
self.cancelTasksByUrl(tasks: downloadTasks as [URLSessionTask])
}
var task:URLSessionDataTask!
task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) -> Void in
print("response \(response)")
if (task !== self.currentTask) {
print("Ignore this task")
return
}
if let error = error {
print("response error \(error)")
}
guard let data = data else { return }
let json = try? JSONSerialization.jsonObject(with: data, options: [])
var newPlacesArray = [AnyObject]() // Empty array of whichever type you want
if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {
guard let jsonDic = json as? NSDictionary else { return }
let status = jsonDic.returnsObjectOrNone(forKey: "statusCode") as! Int
if status == 200 {
guard let resourceSetsArr = jsonDic.returnsObjectOrNone(forKey: "resourceSets") as? NSArray else { return }
guard let placesDict = resourceSetsArr.object(at: 0) as? NSDictionary else { return }
guard let resourceArr = placesDict.object(forKey: "resources") as? NSArray, resourceArr.count > 0 else {
//URL Success, where there no places with the given search string
DispatchQueue.main.async {completion(newPlacesArray)}
return
}
for loopCounter in 0...resourceArr.count - 1 {
let modalClass:BingAutoCompletePlace = BingAutoCompletePlace(responseDict: resourceArr[loopCounter] as! NSDictionary)
newPlacesArray.append(modalClass)
}
DispatchQueue.main.async {completion(newPlacesArray)}
}
}
else if let response = response as? HTTPURLResponse , 400...499 ~= response.statusCode {// When url fails
if let _ = error {
print("error=\(error!.localizedDescription)")
}
DispatchQueue.main.async {completion(newPlacesArray)}
}
else {
if let _ = error {
print("error=\(error!.localizedDescription)")
}
DispatchQueue.main.async {completion(newPlacesArray)}
}
})
self.currentTask = task
task.resume()
}
I'm experimenting with async calls but I'm a little lost. The print(json) in the viewDidLoad outputs an empty dictionary, but the one within the function prints correctly. This is unsurprising; it gets to that print before the async is completed. I can't figure out how to fix it; I tried putting the return within the completion handler, but I got an error that Unexpected non-void return value in void function. I tried changing the completion handler to expect a return value, but either that's not the right approach or I was doing it wrong.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let json = getJson("https://maps.googleapis.com/maps/api/geocode/json?address=WashingtonDC&sensor=false")
print(json)
}
func getJson(url: String) -> AnyObject {
var json:AnyObject = [:]
let urlPath = NSURL(string: url)
let urlRequest = NSURLRequest(URL: urlPath!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
(data, response, error) in
if error != nil {
print("Error")
} else {
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
print(json)
} catch {
print("json error")
}
}
})
task.resume()
return json
}
}
You will need to have a completion handler based interface to your async API.
func getJson(url: String, completion : (success: Bool, json: AnyObject? ) ->Void ) -> Void {
var json:AnyObject = [:]
let urlPath = NSURL(string: url)
let urlRequest = NSURLRequest(URL: urlPath!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
(data, response, error) in
if error != nil {
print("Error")
} else {
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
print(json)
//Call the completion handler here:
completion(success : true, json :json )
} catch {
print("json error")
completion(success : false, json :nil )
}
}
})
task.resume()
}
}
Now you call call this API as follows-
override func viewDidLoad() {
super.viewDidLoad()
getJson("https://maps.googleapis.com/maps/api/geocode/json?address=WashingtonDC&sensor=false") { (success, json) -> Void in
if success {
if let json = json {
print(json)
}
}
}
}