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
Related
On a slow connection when I click on back button on a VC it crashes while accessing navigation controller. VC is already deallocated but setNavBarTitle is executed after going back to another view. I understand that function is executing while VC is already deallocated but Im not sure what's the best way to handle such scenario?
override func viewWillAppear(_ animated: Bool){
super.viewWillAppear(animated)
fetchProfile(clientId: clientId) { (result, error) in
if result?.data != nil {
if (result?.success)! {
self.clientProfile = result!.data!
// Avatar
let clientImageView = UIImageView()
if let url = URL(string: result!.data!.pic_url!) {
clientImageView.image = UIImage(named: "System/defaultAvatar")
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
// WARNING: UIImageView.image must be used from main thread only
clientImageView.image = UIImage(data: data)
self.setNavBarTitle(image: clientImageView.image!)
}
task.resume()
}
}
}
}
}
}
private func setNavBarTitle(image: UIImage) {
// Crashes here -> Thread 11: Fatal error: Unexpectedly found nil while unwrapping an Optional value
let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
}
You can try using [weak self] in your method callback, so even though the method is called after the VC deallocated it wont cause a crash, since self wont be existing:
override func viewWillAppear(_ animated: Bool){
super.viewWillAppear(animated)
fetchProfile(clientId: clientId) { [weak self] (result, error) in
if result?.data != nil {
if (result?.success)! {
self?.clientProfile = result!.data!
// Avatar
let clientImageView = UIImageView()
if let url = URL(string: result!.data!.pic_url!) {
clientImageView.image = UIImage(named: "System/defaultAvatar")
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async {
clientImageView.image = UIImage(data: data)
self?.setNavBarTitle(image: clientImageView.image!)
}
}
task.resume()
}
}
}
}
}
}
private func setNavBarTitle(image: UIImage) {
// Crashes here -> Thread 11: Fatal error: Unexpectedly found nil while unwrapping an Optional value
guard let navigationBarHeight: CGFloat = self.navigationController?.navigationBar.frame.height else {
return
}
}
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
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.
I am attempting to transfer an image downloaded from facebook but for some reason my transferFile method has stopped working. The same code seemed to be working an hour ago and I cannot find what I have changed in order to cause it to stop working.
I am not getting an error from the delegate method nor is my log being called on the watch's side.
Here is how I am downloading my image and transfering.
func downloadImageToFile(urlString : String, completion: (NSURL?, NSError?) -> Void) {
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession.init(configuration: config)
let imageURL = NSURL(string: urlString)
session.downloadTaskWithURL(imageURL!, completionHandler:{(locationURL : NSURL?, response: NSURLResponse?, error: NSError?) in
if (error != nil) {
print("Error: " + (error?.description)!)
completion(nil, error)
}
else {
if let data = NSData(contentsOfURL: locationURL!) {
let documentsDirect = self.documentDirectory()
let saveLocation = documentsDirect.URLByAppendingPathComponent("GCD.png")
let saveWasSuccessfull : Bool = data.writeToURL(saveLocation, atomically: true)
dispatch_async(dispatch_get_main_queue(),{
if (saveWasSuccessfull) {
print("Save Was Successful")
completion(saveLocation, nil)
}
else {
completion(nil, error)
}
})
}
else {
print("Data Download Error")
}
}
}).resume()
}
func documentDirectory() -> NSURL {
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
if let documentDirectory: NSURL = urls.first! as NSURL {
return documentDirectory
}
}
ViewController.swift
let downloader = WebImageDownloader()
downloader.downloadImageToFile(string, completion: { (transferLocation, error) in
if (error != nil) {
}
else {
self.session.transferFile(transferLocation!, metadata: nil)
}
})
func session(session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: NSError?) {
if error != nil {
print(error?.description)
}
else{
print("Finished File Transfer Successfully")
}
}
Watch
func session(session: WCSession, didReceiveFile file: WCSessionFile) {
print("File Recieved on Watch")
dispatch_async(dispatch_get_main_queue()) { [weak self] in
if let data = NSData(contentsOfURL: file.fileURL) {
self?.photos.append(data)
self?.updateTable()
}
else {
print(file.fileURL)
}
}
}
Any ideas what is going on here? I have no errors logging to work with anything.
In my app I have this class to get data from my server:
class Api{
func loadOffers(completion:(([Offers])-> Void), offer_id: String, offerStatus:String){
let myUrl = NSURL(string: "http://www.myServer.php/api/v1.0/offers.php")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let postString = "offer_id=\(offer_id)&offerStatus=\(dealStatus)&action=show"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{ data, response, error in
if error != nil {
println("error\(error)")
}else{
var err:NSError?
let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil)
if let dict = jsonObject as? [String: AnyObject] {
if let myOffers = dict["offers"] as? [AnyObject] {
var offers = [Offers]()
for offer in myOffers{
let offer = Offers(dictionary: offer as! NSDictionary)
offers.append(offer)
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0 )){
dispatch_async(dispatch_get_main_queue()){
completion(offers)
}
}
}
}
}
}
}
task.resume()
}
}
then in my View Controller I load the model:
class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var offers: [Offers]!
func loadModel() {
let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.Indeterminate
loadingNotification.labelText = "updating your offers..."
offers = [Offers]()
let api = Api()
api.loadOffers(didLoadOffers , offer_id: dealIdent!, offerStatus: "open")
}
func didLoadOffers(offers:[Offers]){
self.offers = offers
self.tableView.reloadData()
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
self.refreshControl.endRefreshing()
}
override func viewWillAppear(animated: Bool) {
loadModel()
}
}
Everything works except that when the JSON dictionary is empty, meaning that there no offers the MBProgressHUD keep spinning.
I would like stop the activity indicator adding a subview instead which says that there are no offers. Any Suggestion would be greatly appreciated.
I tried:
if offers.isEmpty{
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
}
and also
if offers == 0 {
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
}
but it's not working
Thanks
This is happening because you set the HUD in main queue but are trying to remove from another one. All UI related changes should be done in main_queue()
Try using this code
dispatch_async(dispatch_get_main_queue(), {
// your code to modify HUD here
});
I recommend small redesign of code. I modified your original code a little. Here is API class:
class Api {
func loadOffers(offerID : String, offerStatus : String, completionHandler : ([Offer]?, NSError?) -> Void) {
let myURL = NSURL(string: "http://www.myServer.php/api/v1.0/offers.php")!
var request = NSMutableURLRequest(URL: myURL)
request.HTTPMethod = "POST"
let postString = "offer_id=\(offerID)&offerStatus=\(offerStatus)&action=show"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (result : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
if let existingError = error {
NSLog("error \(existingError.code) - \(existingError.localizedDescription)")
completionHandler(nil, existingError)
} else {
var parseError : NSError?
if let dictionary = NSJSONSerialization.JSONObjectWithData(result, options: .allZeros, error: nil) as? [String : AnyObject] {
if let myOffers = dictionary["offers"] as? [NSDictionary] {
var parsedOffers = [] as [Offer]
for jsonOffer in myOffers {
parsedOffers.append(Offer(dictionary: jsonOffer))
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
completionHandler(parsedOffers, nil)
}
}
} else {
NSLog("JSON parsing failed")
parseError = NSError(domain: "MyApp", code: 1, userInfo : nil)
completionHandler(nil, parseError)
}
}
}).resume()
}
}
Change is added calling of completion handler even in case error with network communication and in case of failure of JSON parsing.
Corresponding implementation of VC follows:
class ViewController: UITableViewController {
private var _offers : [Offer]?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
loadModel()
}
private func loadModel() {
var hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hud.mode = MBProgressHUDMode.Indeterminate
hud.labelText = "updating your offers"
Api().loadOffers("1", offerStatus: "1") { (offers : [Offer]?, error : NSError?) -> Void in
self._offers = offers
// TODO: Correct handling of error state ;)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
hud.hide(true)
}
}
}
}
From my point of view is using closure better, but depends on your implementation. You can use your implementation with method instead of closure and just define hud variable on instance level.
I hope it helps you to solve your problem (I've tested on simulator and iPhone and works well with some testing stub framework).
if myOffers = nil cannot do complete(offers). so HUD could not stop. You can try this:
if let myOffers = dict["offers"] as? [AnyObject] {
var offers = [Offers]()
for offer in myOffers{
let offer = Offers(dictionary: offer as! NSDictionary)
offers.append(offer)
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0 )){
dispatch_async(dispatch_get_main_queue()){
completion(offers)
}
}
}
}
} else {
dispatch_async(dispatch_get_global_queue(priority, 0 )){
dispatch_async(dispatch_get_main_queue()){
completion([Offers]())
}
}
Every path in loadOffers needs to call the completion closure. If you have an if let statement, you need to consider the else case. If the optional is nil, you still need to call completion. You can pass an empty array in that case, or you can add an error parameter to your completion block so the view controller will know more information about why it's not getting anything back.