Quick one here. I have a class called NetworkService. It has a method that sets up an NSURLSession and completion block. My Object class, NewsItem, has a method downloadNewsItems that calls the first method and goes to a url, downloads JSON data, appends it to an array and returns it. up until that point everything works as i'd want. I create the object and append it then return it (the method is named downloadImage but it can work with any sort of data).
func downloadImage(completion: (NSData -> Void)) {
let request = NSURLRequest(URL: self.url)
let dataTask = session.dataTaskWithRequest(request) { (data, response, error) in
if error == nil {
if let httpResponse = response as? NSHTTPURLResponse {
switch (httpResponse.statusCode) {
case 200:
if let data = data {
completion(data)
}
default:
print(httpResponse.statusCode)
}
}
} else {
print("Error: \(error?.localizedDescription)")
}
}
dataTask.resume()
}
here's the implementation of the same method on my object class.
static func downloadNewsItems() -> [NewsItem] {
var newsItems = [NewsItem]()
let url = NSURL(string: "http://my-url.com.json")
let networkService = NetworkService(url: url!)
networkService.downloadImage { (data) -> Void in
let jsonData = JSON(data:data)
for item in jsonData["stories"].arrayValue {
// print(item["title"])
let newsArticle = NewsItem()
newsArticle.category = item["category"].string
newsArticle.titleText = item["title"].string
newsArticle.paragraph1 = item["paragraph1"].string
newsArticle.paragraph2 = item["paragraph2"].string
newsArticle.featureImage = NSURL(string: "\(item["headerImage"].string)")
newsArticle.date = item["date"].string
newsArticle.majorReference = item["majorReference"].string
newsArticle.fact = item["fact"].string
newsItems.append(newsArticle)
}
print(newsItems.count)
}
return newsItems
}
That print(newsItems.count) shows I have downloaded and updated my objects properly into a dictionary. Now here comes the problem. I have a CollectionViewController. I want to populate it with the data I get from the method call. I create an array and call the method that returns the objects on it inside of ViewDidLoad: but NO! when I print, I get 0 and my collectionView doesn't display any cells.
var newsItems = [NewsItem]()
then in viewDidLoad:
newsItems = NewsItem.downloadNewsItems()
print(newsItems.count)
collectionView.reloadData()
The objects are downloaded, get set up by my init() method and are added to the array in the method whilst inside of the NetworkService / NewsItem classes but when I call the method from the Collection View Controller, Nothing. Initially I tried the default JSONSerialisation route but i had the same problem. I thought maybe I'm not doing it right. Switched to a 3rd party JSON Library (SwiftyJSON)... Exact SAME PROBLEM. Please help. I have had 3 weeks of this. I.. I can't. Not anymore..
user2361090,
You are making a webservice call to fetch all the newsItems inside downloadNewsItems. Which is an asynchronous call. So it takes a little bit of time to fetch process and then populate the newsItems array. But you are not waiting for it to get populated even before it gets populated you have returned it Hence you will return empty array in newsItems = NewsItem.downloadNewsItems(). Use blocks to handover the data. Change your method as
static func downloadNewsItems(completionBlock block : ([NewsItem]) -> ()){
var newsItems = [NewsItem]()
let url = NSURL(string: "http://my-url.com.json")
let networkService = NetworkService(url: url!)
networkService.downloadImage { (data) -> Void in
let jsonData = JSON(data:data)
for item in jsonData["stories"].arrayValue {
// print(item["title"])
let newsArticle = NewsItem()
newsArticle.category = item["category"].string
newsArticle.titleText = item["title"].string
newsArticle.paragraph1 = item["paragraph1"].string
newsArticle.paragraph2 = item["paragraph2"].string
newsArticle.featureImage = NSURL(string: "\(item["headerImage"].string)")
newsArticle.date = item["date"].string
newsArticle.majorReference = item["majorReference"].string
newsArticle.fact = item["fact"].string
newsItems.append(newsArticle)
}
print(newsItems.count)
//if the control comes here in background thread because you know you have to reload the collection view which is UI operation change it main thread
dispatch_async(dispatch_get_main_queue(), { () -> Void in
block(newsItems)
})
}
}
Finally you can call it as,
NewsItem.downloadNewsItems{ (passedArray) -> () in
newsItems = passedArray
collectionView.reloadData()
}
Related
For some reason, the products array is coming back empty when I try and access it from another class. What am I doing wrong, and how can I get the products array to populate? Is it something related to the do/catch?
The print statement shown will give me what I'm looking for, but when I try and use the property in another class after the retrieve method has been called, it comes up empty.
For information, "Product" is a struct that has name, description, etc properties attached.
private let productListUrl = URL(string: "https://api/products.json")
var products = [Product]()
func retrieveProductList() {
if let productListUrl = productListUrl {
URLSession.shared.dataTask(with: productListUrl) { (data, response, error) in
if let data = data {
do {
let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
let tempArray: Array = jsonData["products"] as! [Any]
for product in tempArray {
let newProduct = Product(json: product as! [String : Any])
self.products.append(newProduct!)
}
print("In ProductService: \(self.products)")
}
catch {
print("An error occured while attempting to read data")
}
}
}.resume()
}
}
As maddy noted, this is because the URL call is asynchronous.
You basically have 3 options:
Use a semaphore approach and make your retrieveProductList method synchronous.
Change your class to have a delegate property that you can ping when the URL request finishes.
Add a completion handler to your retrieveProductList method that is called when the URL request finishes.
I personally would lean towards option 3:
func retrieveProductList(completion: #escaping ([Product])->())
{
// Right after you print the products...
completion(self.products)
}
I have a tableView which I want to fill with a list of items provided by a web service. The service returns a JSON object with status (success or failure) and shows (an array of strings).
In viewDidLoad I call the custom method getShowsFromService()
func getShowsFromService() {
// Send user data to server side
let myURL = NSURL(string: "https://myurl.com/srvc/shows.php")
// Create session instance
let session = NSURLSession.sharedSession()
var json:NSDictionary = [:]
// Create the task
let task = session.dataTaskWithURL(myURL!) { //.dataTaskWithRequest(request) {
(data, response, error) in
guard let data = data else {
print("Error: \(error!.code)")
print("\(error!.localizedDescription)")
return
}
do {
json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! NSDictionary
} catch {
print (error)
}
let sts = json["status"] as! NSString
print("\(sts)")
}
// Resume the task so it starts
task.resume()
let shows = json["shows"] as! NSArray
for show in shows {
let thisshow = show as! String
showsArray.append(thisshow)
}
// Here I get "fatal error: unexpectedly found nil while unwrapping an Optional value"
}
The method receives the JSON object and puts it into a dictionary. Then I want to use that dictionary to call json['shows'] in order to get to the array of shows which I want to store in an instance variable called showsArray. The idea is to use showsArray in tableView(cellForRowAtIndexPath) in order to fill in the data.
The problem is that I can't get the Dictionary into the variable. If I try to do it inside the task, I get an error that says I need to call self.showsArray and if I do, the data doesn't go inside the array. If I do it outside the task I get an error because it says I'm trying to force unwrap a nil value.
How can I get the Dictionary created within the task out into the showsArray var?
The dataTaskWithURL method makes an async call, so as soon as you do task.resume() it will jump to the next line, and json["shows"] will return nil as the dictionary is empty at this point.
I would recommend moving that logic to a completion handler somewhere in your class. Something along the lines of:
func getShowsFromService() {
let myURL = NSURL(string: "https://myurl.com/srvc/shows.php")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(myURL!, completionHandler: handleResult)
task.resume()
}
//-handle your result
func handleResult(data: NSData?, response: NSURLResponse?, error: NSError?) {
guard let data = data else {
print("Error: \(error!.code)")
print("\(error!.localizedDescription)")
return
}
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! NSDictionary {
if let shows = json["shows"] as! NSArray {
//- this is still in a separate thread
//- lets go back to the main thread!
dispatch_async(dispatch_get_main_queue(), {
//- this happens in the main thread
for show in shows {
showsArray.append(show as! String)
}
//- When we've got our data ready, reload the table
self.MyTableView.reloadData()
self.refreshControl?.endRefreshing()
});
}
}
} catch {
print (error)
}
}
The snippet above should serve as a guide (I dont have access to a playground atm).
Note the following:
as soon as the task completes (asynchronously -> different thread) it will call the new function handleResult which will check for errors and if not, it will use the dispatcher to perform your task on the main thread. I'm assuming showsArrays is a class property.
I hope this helps
EDIT:
As soon as you fetch your data you need to reload the table (updated code above). You can use a refresh control (declare it as a class property).
var refreshControl: UIRefreshControl!
Then when you finish getting your data you can refresh:
self.MyTableView.reloadData()
self.refreshControl?.endRefreshing()
This will call your delegate methods to populate the rows and sections.
I try to create an page-based navigation watch app with Swift 2.
My app is below:
For both interfaces I have unique controllers named InterfaceController and EconomyInterfaceController.
In each controller I read some JSON data with function in controller init() function
func setFeed(url: String) {
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let request = NSURLRequest(URL: NSURL(string: url)!)
let task: NSURLSessionDataTask = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let data = data {
do {
let _data: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
if let items = _data!["data"] as? NSArray{
self.tableOutlet.setNumberOfRows(items.count, withRowType: "row")
for (index, item) in items.enumerate() {
if let title = self.JSONString(item["title"]) {
if let spot = self.JSONString((item["spot"])) {
let news = newsItem(title: title, spot: spot)
let row = self.tableOutlet!.rowControllerAtIndex(index) as? RowController
row!.rowLabel!.setText(news.title) }
}
}
}
} catch {
}
}
}
task.resume()
}
As I know this is not best way for HTTP request because of all request run on main thread and on app startup. This is not my main question but if you have any offer I cannot refuse :)
My main question is if I call setFeed() method in willActivate() method and set table labels with
row!.rowLabel!.setText(news.title)
my app works but this is not a good choice because on each time page changed this updates content again and again.
If I call setFeed() method in init() method and set table labels with
row!.rowLabel!.setText(news.title)
Only app's first page being displayed and other pages not being displayed. What is wrong here?
I cant work out why my arrays (amountArray, interestArray and riskbandArray) return the full set of information if I print them just after the forloop, whereas if I print them right at the end of the viewDidload method it returns nothing. I understand that its probably occurring because the network request is being executed in the background so when I ask to print my arrays, nothing is returned because it is being called in the main thread. I have tried to counteract this by bringing in Grand Central Dispatch but my arrays are still empty. SO SO frustrating.
class CollectionViewController: UICollectionViewController {
var amountArray = [Int]()
var interestArray = [Double]()
var riskbandArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
let session = NSURLSession.sharedSession()
let Url = NSURL(string: "http://fc-ios-test.herokuapp.com/auctions")
let task: NSURLSessionDownloadTask = session.downloadTaskWithURL(Url!) { (url, response, error) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let data = NSData(contentsOfURL: url!)
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
if let array = jsonData["items"] as? [[NSObject:AnyObject]] {
for item in array {
self.amountArray.append(item["amount_cents"] as! Int)
self.interestArray.append(item["rate"] as! Double * 100)
self.riskbandArray.append(item["risk_band"] as! String)
}
}
self.collectionView?.reloadData()
} catch {
print(error)
}
})
}
task.resume()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
print(self.amountArray)
}
Here's a cut-down version showing the relevant parts of your code:
let task: NSURLSessionDownloadTask = session.downloadTaskWithURL(Url!) {...}
task.resume()
print(self.amountArray)
So you're creating a task and starting it, and then immediately printing amountArray. It's important to know that the task will run asynchronously, i.e. it will get processing time periodically until it completes, but execution of your code will continue from the task.resume() before the task even really gets going. So your print() executes long before the code that would add data to amountArray ever happens.
If you want to see what's in self.amountArray, move the print() statement so that it's the last thing that happens in the task's completion block. It will then happen after the array is changed.
I'm hoping someone an help me figure out a problem that has me scratching my brain! When I attempt this function using a NSData(contentsOfUrl... structure, this all works fine. However, I am attempting to use a NSURLSession for use on an Apple Watch app, and keep hitting an error;
...
class func fetchData() -> [Complication] {
var task: NSURLSessionDataTask?
let myURL = "http://www.myurl.com/sample.json"
let dataURL = NSURL(string: myURL)
let conf = NSURLSessionConfiguration.defaultSessionConfiguration()
conf.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
let session = NSURLSession(configuration: conf)
task = session.dataTaskWithURL(dataURL!) { (data, res, error) -> Void in
if let e = error {
print("dataTaskWithURL fail: \(e.debugDescription)")
return
}
var dataSet = [Complication]()
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
for item in json {
let name: String? = item["name"] as? String
let percent: Int? = item["percent"] as? Int
let timeFromNow: Int? = item["timeFromNow"] as? Int
let myData = Complication(
name: name!,
percent: percent!,
timeFromNow: timeFromNow!
)
dataSet.append(myData)
}
} catch {
print(error)
}
}
return dataSet
//THIS LINE THROWS THE ERROR
}
...
When attempting to return my dataSet array, I receive the error Instance member 'dataSet' cannot be used on type 'Complication'. As mentioned, however, this does seem to work if I were to use a NSData(contentsOfUrl... instead of a NSURLSession, which is where I am stuck!
The data task is a closure that is executed asynchronously. Its return statements returns from the closure, not from the outer function.
Since the closure is executed asynchronously it makes no sense to return data from it: the return type is Void.
You should organize your code differently, e.g. using a completion handler.
Hint: search for "swift return closure" in SO. You will find plenty of questions similar to yours and a number of good answers and suggestions.