i'm new on programming and of course swift. and i'm wondering how can i send back my request response to my viewDidLoad to to use there.
for requesting, i'm using alamofire and for json , swiftyJSON.
what i'm trying to do is to Get data(JSON) from server which contains titles and image urls. after that i'm trying to Get images from image urls.
in viewDidLad, i call a function which is a Get method.
override func viewDidLoad() {
super.viewDidLoad()
getData()
//use myStruct
in getData() i do this:
func getData()
{
Alamofire.request(.GET,"heregoesurl")
.responseJSON { response in
if let Json = response.result.value {
let json = JSON(Json)
for (_,subJson):(String, JSON) in json {
//get image url
//pass image url to imageGet() method
//add image to an array of myStruct
}
}
}
}
and in imageGet() method:
func getImages(url:String,type:String)
{
Alamofire.request(.GET, url)
.responseImage { response in
if let image = response.result.value {
//add image to myStruct
}
}
}
the problem is because of async nature of request, myStruct isn't ready in viewDidLoad. i used completionHandler for getData() method and it works fine. but still i don't have images and i just have image urls there. i don't know what should i do then.
any help on how to do things like this would be appreciated.
Try this:
public func getImages(url:String,type:String){
Alamofire.request(.GET, url).response { (request, response, data, error) in
//For sure that you will have the main queue and you will see the view
dispatch_sync(dispatch_get_main_queue(), {
self.myImageView.image = UIImage(data: data, scale:1) // or store it to your `myStruct`
})
}
}
call it inside of getData() function
use this:
extension UIImageView {
func downloadImage(from url : String){
let urlRequest = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: urlRequest){(data,response,error)in
if error != nil {
print(error)
}
DispatchQueue.main.async {
self.image = UIImage(data:data!)
}
}
task.resume()
}
}
Related
I am attempting to take a string from JSON data and set it to a variable. My problem is that the variable shows as empty. I am using JSONDecoder to retrieve the JSON data and setting the string to a variable outside of the function. I then want to use that variable inside of another function
When I print the variable it still shows up as blank even after the function has loaded. Within the function the string appears correctly.
Code:
var filmTitle = ""
override func viewDidLoad() {
super.viewDidLoad()
loadFilms()
print(self.filmTitle) //Prints as an empty string
}
func loadFilms() {
let id = filmId
let apiKey = "97a0d64910120cbeae9df9cb675ad235"
let url = URL(string: "https://api.themoviedb.org/3/movie/\(id)?api_key=\(apiKey)&language=en-US")
let request = URLRequest(
url: url! as URL,
cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData,
timeoutInterval: 10 )
let session = URLSession (
configuration: URLSessionConfiguration.default,
delegate: nil,
delegateQueue: OperationQueue.main
)
let task = session.dataTask(with: request, completionHandler: { (dataOrNil, response, error) in
if let data = dataOrNil {
do { let details = try! JSONDecoder().decode(Details.self, from: data)
self.filmTitle = details.title
print(self.filmTitle) //string prints correctly
}
}
})
task.resume()
}
What am I missing to correctly set the string to the variable?
Loading data from the internet is an asynchronous method. The print statement is being called before loadFilms() has completed.
Use a callback to get the data after it has completed.
func loadFilms(completion: #escaping (Details?, Error?) -> Void) {
//...
let task = session.dataTask(with: request, completionHandler: { (dataOrNil, response, error) in
if let data = dataOrNil {
do { let details = try JSONDecoder().decode(Details.self, from: data)
completion(details, nil)
} catch {
completion(nil, error)
}
})
}
At the call site:
override func viewDidLoad() {
loadFilms { details, error in
if error { //* Handle Error */ }
self.filmTitle = details.title
print(filmTitle)
}
}
Web request are asynchronous and from the CP's perspective, take a long time to complete. When you call this:
override func viewDidLoad() {
super.viewDidLoad()
loadFilms()
print(self.filmTitle) // loadFilms() hasn't finished so `filmTitle` is empty
}
It's better to set a property observer on filmTitle:
var filmTitle: String? = nil {
didSet {
print(filmTitle)
Dispatch.main.async {
// update your GUI
}
}
}
The solution to this problem was to reload the collection view that the array was being sent to within the decoder function after the data was set to the array.
The JSON file I am using: https://api.myjson.com/bins/49jw2
I am using SwiftyJSON for parsing.
The variable chores wont be populated outside the method parseJson
var chores: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tfWhat.delegate = self
tfHowMuch.delegate = self
loadJson()
// wont even print
for chore in self.chores {
print("beschrijving: " + chore)
}
// prints 0
print(chores.count)
}
func loadJson() -> Void {
let url = NSURL(string: "https://api.myjson.com/bins/49jw2")
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
} else {
if let data = data {
let json = JSON(data: data)
self.parseJson(json["appdata"]["klusjes"][])
} else {
print("no data")
}
}
}).resume()
}
func parseJson(jsonObject : JSON) -> Void {
for (_, value) in jsonObject {
self.chores.append(value["beschrijving"].stringValue)
}
// prints:
// beschrijving: Heg knippen bij de overburen
// beschrijving: Auto van papa wassen
for chore in self.chores {
print("beschrijving: " + chore)
}
// prints:
// 2
print(chores.count)
}
When you call an asynchronous method like NSURLSession.sharedSession().dataTaskWithURL it gets executed in the background, so whatever you launch just after this instruction is actually executed while the background task is running, so your array is not populated yet when you look at it.
A simple way to overcome this mistake is to use a "callback": a closure that will be executed once the data is available.
For example, let's add a callback
(json: JSON)->()
to the loadJson method:
func loadJson(completion: (json: JSON)->())
and place the call where the data will be available:
func loadJson(completion: (json: JSON)->()) {
let url = NSURL(string: "https://api.myjson.com/bins/49jw2")
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
} else {
if let data = data {
// the callback executes *when the data is ready*
completion(json: JSON(data: data))
} else {
print("no data")
}
}
}).resume()
}
and use it with a trailing closure like this:
loadJson { (json) in
// populate your array, do stuff with "json" here, this is the result of the callback
}
I am stuck in a problem. I think it is all due to my weak basics. I am sure someone can help me easily and put me in the right direction.
I have different segues and all get the data from JSON via remote URL.
So in-short all segues need to open URL and parse JSON and make them into an ARRAY
I have made the first segue and it is working fine.
Now i plan to use the functions where it download JSON and turns it into ARRAY as a common function
I read in another page on stackoverflow that I can declare all common functions outside the class in ViewController
I hope everyone is with me this far.
now in ViewController i declare a function
getDataFromJson(url: String)
This function code looks like following
func getJsonFromURL(url: String)
{
// some class specific tasks
// call the common function with URL
// get an array
let arrJSON = getJsonArrFromURL(url)
for element in arrJSON
{
// assign each element in json to ur table
print("Element: \(element)")
}
// some class specific tasks
}
and this will call the common function declared outside the score of class
getArrFromJson(url: String) -> NSArray
This common function is just very generic.
Take a URL, call it, open it, parse its data into ARRAY and return it back.
The problem i am stuck is where to put the return
It returns empty array as the task is not finished and i am clueless
func getJsonArrFromURL(var url: String) -> NSArray
{
var parseJSON : NSArray?
if ( url == "" )
{
url = self.baseURLHomepage
}
print("Opening a JSON URL \(url)")
let myUrl = NSURL(string: url);
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "GET";
let postString = "";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{
data, response, error in
if ( error != nil )
{
print("Error open JSON url \n\(error)")
return
}
do
{
parseJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray
}
catch
{
self.showAlert("Error", msg: "Error occurred while trying to process the product information data")
print("Error occured in JSON = \(error)")
}
}
task.resume()
return parseJSON!
}
You can probably add a method like below in any of your class
func post(url: String, info: String, completionHandler: (NSString?, NSError?) -> ()) -> NSURLSessionTask {
let URL = NSURL(string: url)!
let request = NSMutableURLRequest(URL:URL)
request.HTTPMethod = "GET"
let bodyData = info
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
dispatch_async(dispatch_get_main_queue()) {
guard data != nil else {
print("response String is nil")
completionHandler(nil, error)
return
}
if let dataNew = data {
completionHandler(NSString(data: (NSData(base64EncodedData: dataNew, options: NSDataBase64DecodingOptions([])))!, encoding: NSASCIIStringEncoding), nil)
}
}
}
task.resume()
return task
}
and access it anywhere like
let url = "your URL String"
let info = "The data you would like to pass"
yourClassName.post(url, info: info) { responseString, error in
guard responseString != nil else {
print("response String is nil")
print(error)
return
}
do {
if !(responseString as? String)!.isEmpty {
let json = try NSJSONSerialization.JSONObjectWithData((responseString as! String).data, options: NSJSONReadingOptions.init(rawValue: 0))
//process your json here
}
} catch {
print("Error\n \(error)")
return
}
}
Extend your string like follows
extension String {
var data:NSData! {
return dataUsingEncoding(NSUTF8StringEncoding)
}
}
I am very new to swift. I am trying to return back the JSON and view it in a list view, I cant get the JSON from my AppApi class to return back to my viewDidLoad(). Any help would be appreciated.
Thank you in advanced.
Teli
override func viewDidLoad() {
super.viewDidLoad()
let api = AppAPI(token:self.toPassToken)
var test = api.getOrders()
println("why does test come back as an empty array")
println(test)
println(test.count)
}
class AppAPI {
var token: String
let apiEndPoint = "endpoint"
let apiUrl:String!
let consumerKey:String!
let consumerSecret:String!
var returnData = [:]
init(token:String){
self.apiUrl = “hidden-for-security”
self.consumerKey = "token"
self.consumerSecret = "my consumer secret"
self.token = token
}
func getOrders() -> [JSON] {
return makeCall("contacts")
}
func makeCall(section:String) -> [JSON] {
let params = ["token":"\(self.token)"]
Alamofire.request(.POST, "\(self.apiUrl)", parameters: params)
.responseJSON { (request, response, json, error) -> Void in
println("error \(request)")
self.returnData = json! as! NSDictionary
}
return results!
}
}
In your makeCall(section:String) -> [JSON] function you are returning results!. Where is results ever set in this function?
Did you mean to return returnData instead?
Alamofire.request performs an asynchronous request; responseJSON will execute that closure when the request completes at some point in the future.
You almost certainly do not want to block your viewDidLoad method until this request finishes as that would block your main thread and leave the app unresponsive. Instead start the request in viewDidLoad and react whenever the request finishes.
One way you might do this is by passing a closure to getOrders which you could execute when the request finishes.
I've been creating a function which retrieve objects from a JSON script. I've chosen for this to use alamofire for async request and swiftyJSON for easy parsing. However i seem to have a problem with it blocking the UI? How come it does that when it is async request? Do i need to run it on a separate thread or what could the explanation be?
Basically what i mean by blocking UI is that it does not react on other buttons before the below function is finished executing.
func getRecent() {
var url = "http://URL/recent.php?lastid=\(lastObjectIndex)&limit=\(numberOfRecordsPerAPICall)"
isApiCalling = true
request(.GET, url, parameters: nil)
.response { (request, response, data, error) in
if error == nil {
let data: AnyObject = data!
let jsonArray = JSON(data: data as! NSData)
if jsonArray.count < self.numberOfRecordsPerAPICall {
self.recentCount = 0
self.tableVIew.tableFooterView = nil
} else {
self.recentCount = jsonArray.count
self.tableVIew.tableFooterView = self.footerView
}
for (key: String, subJson: JSON) in jsonArray {
// Create an object and parse your JSON one by one to append it to your array
var httpUrl = subJson["image_url"].stringValue
let url = NSURL(string: httpUrl)
let data = NSData(contentsOfURL: url!)
if UIImage(data: data!) != nil {
// Create an object and parse your JSON one by one to append it to your array
var newNewsObject = News(id: subJson["id"].intValue, title: subJson["title"].stringValue, link: subJson["url"].stringValue, imageLink: UIImage(data: data!)!, summary: subJson["news_text"].stringValue, date: self.getDate(subJson["date"].stringValue))
self.recentArray.append(newNewsObject)
}
}
self.lastObjectIndex = self.lastObjectIndex + self.numberOfRecordsPerAPICall
self.isApiCalling = false
self.tableVIew.reloadData()
self.refreshControl?.endRefreshing()
}
}
}
The response closure is executed on the main thread. If you are doing your JSON parsing there (and you have a large amount of data) it will block the main thread for a while.
In that case, you should use dispatch_async for the JSON parsing and only when you are completed update the main thread.
Just do your parsing like this
func getRecent() {
var url = "http://URL/recent.php?lastid=\(lastObjectIndex)&limit=\(numberOfRecordsPerAPICall)"
isApiCalling = true
request(.GET, url, parameters: nil)
.response { (request, response, data, error) in
if error == nil {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// Parse stuff here
let data: AnyObject = data!
let jsonArray = JSON(data: data as! NSData)
if jsonArray.count < self.numberOfRecordsPerAPICall {
self.recentCount = 0
self.tableVIew.tableFooterView = nil
} else {
self.recentCount = jsonArray.count
self.tableVIew.tableFooterView = self.footerView
}
for (key: String, subJson: JSON) in jsonArray {
// Create an object and parse your JSON one by one to append it to your array
var httpUrl = subJson["image_url"].stringValue
let url = NSURL(string: httpUrl)
let data = NSData(contentsOfURL: url!)
if UIImage(data: data!) != nil {
// Create an object and parse your JSON one by one to append it to your array
var newNewsObject = News(id: subJson["id"].intValue, title: subJson["title"].stringValue, link: subJson["url"].stringValue, imageLink: UIImage(data: data!)!, summary: subJson["news_text"].stringValue, date: self.getDate(subJson["date"].stringValue))
self.recentArray.append(newNewsObject)
}
}
dispatch_async(dispatch_get_main_queue()) {
// Update your UI here
self.lastObjectIndex = self.lastObjectIndex + self.numberOfRecordsPerAPICall
self.isApiCalling = false
self.tableVIew.reloadData()
self.refreshControl?.endRefreshing()
}
}
}
}
}
Swift5
An update to Stefan Salatic's answer, if you are parsing a large amount of json data from the Alamofire response it is better you use a global dispatch Queue and if for any reason you need to update the UI in the main thread switch to DispatchQueue.main.async.
So a sample code will look like this.
AF.request(UrlGetLayers, method: .post, parameters: parameters, headers: headers)
.responseJSON { response in
DispatchQueue.global(qos: .background).async {
//parse your json response here
//oops... we need to update the main thread again after parsing json
DispatchQueue.main.async {
}
}
}