'MBProgressHUD needs to be accessed on the main thread.' - Swift 3 - ios

I am getting 'MBProgressHUD needs to be accessed on the main thread.' error on hiding MBProgressHUD once page is loaded with data from web service.
Please help me to solve this. Stuck on this issue since hours now.
My code is :
DispatchQueue.main.async {
self.spinnerActivity.hide(animated: true)
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}
I started the spinner in viewDidLoad as follows :
spinnerActivity = MBProgressHUD.showAdded(to: self.view, animated: true);
spinnerActivity.label.text = "Loading";
spinnerActivity.detailsLabel.text = "Please Wait!!";
Please help.
Edit:
This is my whole code of that view controller :
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var refreshControl: UIRefreshControl!
var spinnerActivity: MBProgressHUD! = nil
override func viewDidLoad() {
super.viewDidLoad()
refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor.green
refreshControl.attributedTitle = NSAttributedString(string: "Refreshing", attributes: [NSForegroundColorAttributeName: UIColor.white])
refreshControl.addTarget(self, action: #selector(ViewController.refresh(sender:)), for: UIControlEvents.valueChanged)
tableView.addSubview(refreshControl)
spinnerActivity = MBProgressHUD.showAdded(to: self.view, animated: true);
spinnerActivity.label.text = "Loading";
spinnerActivity.detailsLabel.text = "Please Wait!!";
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
navigationItem.title = city.uppercased()
self.runWebService()
}
func runWebService(){
let url = URL(string: "")!// have removed my url
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in // URLSession.shared().dataTask(with: url) { (data, response, error) is now URLSession.shared.dataTask(with: url) { (data, response, error)
if error != nil {
self.spinnerActivity.hide(animated: true)
print(error)
} else {
if let urlContent = data {
do {
guard let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as? [AnyObject] else {
self.spinnerActivity.hide(animated: true)
return
}
//processing web service
//DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.main.async {
self.spinnerActivity.hide(animated: true)
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}
} catch {
self.spinnerActivity.hide(animated: true)
print("JSON Processing Failed")
}
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func back(sender: UIBarButtonItem) {
_ = navigationController?.popToRootViewController(animated: true)
}
func refresh(sender:AnyObject) {
//pull to refresh
self.runWebService()
}
//remaining are table view data source and delegate methods }

You're trying to hide the MBProgressHUD from the background in all of your error handling. Switch over to the main thread to fix:
func runWebService(){
let url = URL(string: "http://www.google.com")!// have removed my url
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in // URLSession.shared().dataTask(with: url) { (data, response, error) is now URLSession.shared.dataTask(with: url) { (data, response, error)
if error != nil {
DispatchQueue.main.async {
self.spinnerActivity.hide(animated: true)
}
print(error)
} else {
if let urlContent = data {
do {
guard let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as? [AnyObject] else {
DispatchQueue.main.async {
self.spinnerActivity.hide(animated: true)
}
return
}
//processing web service
//DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.main.async {
self.spinnerActivity.hide(animated: true)
}
} catch {
DispatchQueue.main.async {
self.spinnerActivity.hide(animated: true)
}
print("JSON Processing Failed")
}
}
}
}
task.resume()
}
** Note that I removed some of your code to test.

Related

Json data not showing on tableView swift 5

I fetched the data and it is showing when printing but when i try to display it on tableview.Nothing is coming
Am i placing tableview.reloadData in wrong place ?
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
tableView.reloadData()
}
func fetchData()
{
if let url = URL(string: urlConstant) {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, res, err) in
if err == nil
{
let decoder = JSONDecoder()
if let safeData = data
{
do{
let results = try decoder.decode(Results.self, from: safeData)
guard let array = results.Result as? [Products] else {return }
for product in array
{
self.productArray.append(product)
}
} catch {
print(error)
}
}
}
}
task.resume()
}
self.tableView.reloadData()
}
If you are getting correct response(check it once) from the server then next thing you need to reload tableView after getting the response from the server and populating the array.
func fetchData() {
if let url = URL(string: urlConstant) {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, res, err) in
if err == nil
{
let decoder = JSONDecoder()
if let safeData = data
{
do{
let results = try decoder.decode(Results.self, from: safeData)
guard let array = results.Result as? [Products] else {return }
for product in array {
self.productArray.append(product)
}
// Reload table view here
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
print(error)
}
}
}
}
task.resume()
}
}
Alternative, you can add completion handled in fetchData method.

Wkwebview 100% packet loss preset

I have an asynchronous call to refresh my cookies in a new wkwebview.
public override func viewDidLoad() {
super.viewDidLoad()
let cookies = cookieService.getCookies(forDomain: urlService.getTopLevelDomain())
authenticationService.authenticateIfNeeded { [weak self] error in
if let error = error {
print(failed)
} else {
self?.identityService.storeCookies(cookies) {
DispatchQueue.main.async {
self?.loadRequest()
}
}
}
}
public func authenticateIfNeeded(completion: #escaping (Error?) -> Void) {
let domain = urlService.getTopLevelDomain()
identityService.refreshCookies(for: domain, force: true, completion: completion)
}
I have put my network in a 100% packet loss preset.
The logic which has setcookies in identity services has retry options and it takes 60 seconds in total to complete this retry calls.
func storeCookies(_ cookies: [AnyObject], completion: (() -> Void)? = nil) {
let group = DispatchGroup()
let httpCookies = cookies.compactMap { $0 as? HTTPCookie }
for httpCookie in httpCookies {
self.cookieStorageService.setCookie(httpCookie)
group.enter()
wkCookieStorage.setCookie(httpCookie) {
group.leave()
}
}
group.notify(queue: .main) {
completion?()
}
}
func refreshCookies(for domain: String, force: Bool, completion: #escaping VoidResultHandler) {
Retry<Void>.call(
shouldRetry: self.shouldRetryIdentityOperation,
handler: completion,
method: { completion in
self.identityOperations.refreshCookies(force: force, domain: domain, handler: { result in
switch result {
case .value(let cookies):
self.storeCookies(cookies) {
completion(.value(()))
}
case .error(let error):
completion(.error(error))
}
})
})
}
Till then I see a blank screen and then I get a retry option. How to reduce this delay to have a better user experience.
import UIKit
import WebKit
class ViewController: UIViewController {
private weak var webView: WKWebView!
public override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
view.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: webView.rightAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: webView.bottomAnchor).isActive = true
self.webView = webView
checkNetworkSpeed(timeout: 5) { [weak self] error in
guard let self = self else { return }
if let error = error { print("!!!! ERROR: \(error)"); return }
self.webView.load(URLRequest(url: URL(string: "http://google.com")!))
}
}
enum CheckNetworkErrors: Error {
case noResponse
case wrongStatusCode(Int)
}
public func checkNetworkSpeed(timeout: TimeInterval = 30, completion: ((Error?) -> Void)?) {
DispatchQueue.global(qos: .default).async {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = timeout
configuration.timeoutIntervalForResource = timeout
let session = URLSession(configuration: configuration)
let url = URL(string: "https://apple.com")
session.dataTask(with: url!, completionHandler: { data, response, error in
var responseError: Error?
defer { DispatchQueue.main.async { completion?(error) } }
if let error = error { responseError = error; return }
guard let httpResponse = response as? HTTPURLResponse else {
responseError = CheckNetworkErrors.noResponse
return
}
guard (200...299).contains(httpResponse.statusCode) else {
responseError = CheckNetworkErrors.wrongStatusCode(httpResponse.statusCode)
return
}
}).resume()
}
}
}

ios 10+, Swift 3+ - Cannot dismiss UIAlertController from Singleton instance

I have created an overlay to run while I run an async data grab to the server so that users won't continue pressing buttons in the UI until the data grab is done. I have put the function into a global singleton class and I call it while passing in a bool to say whether or not I want to show or hide. I can get it to show but I cannot get it to hide. Here is the code:
class DataModel {
static let sharedInstance = DataModel()
func accessNetworkData(vc: UIViewController, params: [String:Any], wsURLPath: String, completion: #escaping (_ response: AnyObject) -> ()) {
DataModel.sharedInstance.toggleModalProgess(show: true)
// SHOW THE MODAL HERE ONCE THE DATA IS REQUESTED.
let url = URL(string: wsURLPath)!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
do { request.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) } catch let error { print(error.localizedDescription) }
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
DataModel.sharedInstance.toggleModalProgess(show: false)
// NOW SINCE THE NETWORK ACTIVITY IS DONE, HIDE THE UIALERTCONTROLLER
guard error == nil else {
print("WEB SERVICE ERROR <----------------------------<<<<<< " + wsURLPath)
print(error!)
let resp: [String: String] = [ "conn": "failed" ]
DispatchQueue.main.async { completion(resp as NSDictionary) }
return
}
guard let data = data else {
print("WEB SERVICE ERROR <----------------------------<<<<<< " + wsURLPath)
return
}
do {
if let parsedJSON = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print("WEB SERVICE SUCCESS <----------------------------<<<<<< "+wsURLPath+" "+String(describing:params))
if let parsedResponseDict = parsedJSON["d"] as? NSDictionary {
DispatchQueue.main.async {
completion(parsedResponseDict)
}
}else if let parsedResponseArr = parsedJSON["d"] as? NSArray {
DispatchQueue.main.async {
completion(parsedResponseArr)
}
}else {
print("INVALID KEY <----------------------------<<<<<< " + wsURLPath)
DispatchQueue.main.async {
completion(parsedJSON as AnyObject)
}
}
}
} catch let error {
print("Error with JSON Serialization")
print(error.localizedDescription)
}
})
task.resume()
}
HERE IS WHERE I SET UP THE UIALERTCONTROLLER
let modalAlert = UIAlertController(title: "Please Wait...", message: "Loading Data...", preferredStyle: UIAlertControllerStyle.alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
func toggleModalProgess(show: Bool) -> Void {
print("toggleModalProgess: show = " + String(describing: show))
if (show) {
print("let's turn it on")
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating()
modalAlert.view.addSubview(loadingIndicator)
modalAlert.show()
}else {
print("let's turn it off")
modalAlert.hide()
}
}
private init() { }
}
NOW THE EXTENSION WHERE THE MAGIC HAPPENS
public extension UIAlertController {
func show() {
let win = UIWindow(frame: UIScreen.main.bounds)
let vc = UIViewController()
vc.view.backgroundColor = .clear
win.rootViewController = vc
win.windowLevel = UIWindowLevelAlert + 1
win.makeKeyAndVisible()
vc.present(self, animated: true, completion: nil)
}
func hide() {
// HERE IS WHERE I NEED TO HIDE IT BUT I AM HAVING ISSUES
}
}
In order to dismiss the UIAlertController (which is a subclass of UIViewController), it should be sufficient to call the dismiss method:
func hide() {
dismiss(animated: true, completion: nil)
}
It works fine in my sample project.
You should do...
self.presentingViewController?.dismiss(animated: true, completion: nil)
Hope it helps

Swift Convert string into UIIMAGE

I would like to load an image from an api web service asynchronously into a uitableview with swift for iOS 9. Below is the code from my Playlist controller. Thanks in advance.
import UIKit
class PlaylistViewController: UITableViewController {
var playlists = [[String: String]]()
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "http://xxxxxxx.xxx/api/v1/players/1/playlists?api_key=xxxxxxxxxxxx"
if let url = NSURL(string: urlString) {
if let data = try? NSData(contentsOfURL: url, options: []) {
let json = JSON(data: data)
if json != nil {
parseJSON(json)
} else {
showError()
}
} else {
showError()
}
} else {
showError()
}
}
func showError() {
let ac = UIAlertController(title: "Loading error", message: "There was a problem loading the feed; please check your connection and try again.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
func parseJSON(json: JSON) {
for result in json["playlists"].arrayValue {
let title = result["title"].stringValue
let id = result["id"].stringValue
let cover_url = result["cover_url"].stringValue
let obj = ["title": title, "id": id, "cover_url" : cover_url]
playlists.append(obj)
}
tableView.reloadData()
}
Use NSURLSession dataTaskWithURL for asynchronously task:
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "http://xxxxxxx.xxx/api/v1/players/1/playlists?api_key=xxxxxxxxxxxx"
if let url = NSURL(string: urlString) {
let session = NSURLSession.sharedSession()
var task = session.dataTaskWithURL(url) { (data, response, error) -> Void in
if let err = error {
showError(err)
} else {
let json = NSString(data: data, encoding: NSUTF8StringEncoding)
// json is a String, you should handle this String as JSON
parseJSON(json)
}
}
}
Your tableView.reloadData() should be executed in main thread (because the NSURLSession dataTaskWithUrl result is in background thread)
dispatch_async(dispatch_get_main_queue(), {
tableView.reloadData()
})

Best practice to hide the HUD added to view

Suppose that I have the following code:
#IBAction func signInButtonPressed(sender: AnyObject) {
MBProgressHUD.showHUDAddedTo(self.view, animated: true)
if let url = NSURL(string: someURL) {
// ...
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data, response, error) in
if let httpError = error {
dispatch_async(dispatch_get_main_queue()) {
self.alert("Error", message: "Unable to sign in: \(httpError.localizedDescription)")
}
return
}
var deserializationError: NSError?
if let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &deserializationError) as? [String: AnyObject] {
// ...
if let error = customer.error {
dispatch_async(dispatch_get_main_queue()) {
self.alert("Error", message: error)
}
} else {
// Show other view controller
}
} else {
if let unwrappedError = deserializationError {
dispatch_async(dispatch_get_main_queue()) {
self.alert("Error", message: "Unable to sign in: \(deserializationError)")
}
}
}
}
task.resume()
} else {
if let unwrappedError = serializationError {
self.alert("Error", message: "Unable to sign in: \(serializationError)")
}
}
}
}
What is the proper way to hide the HUD added to self.view? Is there any more elegant way to do this than adding the
dispatch_async(dispatch_get_main_queue()) {
MBProgressHUD.hideHUDForView(self.view, animated: true)
return
}
code to every if and else branches?
Thanks in advance.
first show your hud after your url initialized and right before your task get started
if let url = NSURL(string: someURL) {
MBProgressHUD.showHUDAddedTo(self.view, animated: true)
// start the request here
then hide it right after callback block started
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data, response, error) in
dispatch_async(dispatch_get_main_queue()) {
MBProgressHUD.hideHUDForView(self.view, animated: true)
}
// here goes other logic
you don't have to call return after hud gets hidden

Resources