66607

Swift 3 - Hide Elements

Question:

Im trying to hide a element using swift 3. The element won't hide if its in session.dataTask, but if I move it outside session.dataTask the element hides fine. Is it possible to hide a element in session.dataTask?

@IBOutlet weak var login_box: UIStackView! let task = session.dataTask(with: request as URLRequest) { ( data, response, error) in guard let data = data, let _:URLResponse = response, error == nil else { print("error") return } //Following won't hide element self.login_box.isHidden = true } //If placed here element hides fine login_box.isHidden = true task.resume()

Answer1:

First of all you need to start URLSessionDataTask instance using resume and always perform UI changes on main thread.

let task = session.dataTask(with: request as URLRequest) { (data, response, error) in guard let data = data, let _:URLResponse = response, error == nil else { print("error") return } DispatchQueue.main.async { self.login_box.isHidden = true } } task.resume()

It will take some time get response from server (depends on your internet speed), but if error is not nil then it will not hide your login_box because it is return from the block.

Recommend

  • How do I center 4 uibuttons with equal distance from each other?
  • How do I respond to a POST from an iPhone app in PHP?
  • Swift: Bring View from Stack View to front
  • How to center both vertical and horizontal inside a UIStackView
  • How to update information on MKPinAnnotationView?
  • Sending post data nsurlsession
  • Passing values between SWFs
  • How do you close open files using Swift?
  • Error getting audio input device sample rate: '!obj'
  • Pattern combining type test and literal
  • Error Domain=NSCocoaErrorDomain Code=3840 “Invalid value around character 0.” UserInfo={NSDebugDescr
  • Dealing with XElement null value
  • converting text file into xml using php?
  • Unable to get column index with table.getColumn method using custom table Model
  • custom UITableViewCell with image for highlighting
  • Counter field in MS Access, how to generate?
  • Is there a amazon webstore API for customers?
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • How to extract text from Word files using C#?
  • vba code to select only visible cells in specific column except heading
  • Symfony2: How to get request parameter
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • KeystoneJS: Relationships in Admin UI not updating
  • log4net write single file for each call to log.info
  • trying to dynamically update Highchart column chart but series undefined
  • Getting error when using KSoap library to consume .NET web services
  • using HTMLImports.whenReady not working in chrome
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app