Faizan Mubasher
Faizan Mubasher

Reputation: 4569

Showing hidden view is very slow

As per these SO threads, I know UI components should be updated through Main UI thread, however I am still unable to resolve issue. My hidden view still shows after more than 20 seconds. Really slow!

iOS SWIFT Showing hidden view really slow

Swift/iOS : UIPicker display is not refreshed until tapped after changing the datasource programmatically

To elaborate more, here is what I am doing:

ViewController

override func viewDidLoad() {

  let campusTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleCampusOpenTap))

        campusTapGesture.numberOfTapsRequired = 1 // Default
        campusSelect.isUserInteractionEnabled = true
        campusSelect.addGestureRecognizer(campusTapGesture)

  let networkController = NetworkController()
        networkController.fetchCampusList(empId: empId, terminal: "IOS-011", handler: self)
}

While campusSelect is UIView and tapping on it will show hidden view.

My NetworkController has a protocol method func onCampusResponseSuccess(response: CampusListResponse) which my ViewController conforms in that way:

func onCampusResponseSuccess(response: CampusListResponse) {

    let listOfCampus = convertToCampusListModel(apiResponse: response)
    campusPickerData = CampusPickerData(list: listOfCampus)

    // Below code block is performed on main thread because this method runs on background thread and only being invoked when list is received from API
    DispatchQueue.main.async {
        self.campusPick.dataSource = self.campusPickerData
        self.campusPick.delegate = self.campusPickerData
        self.selectedCampus.text = listOfCampus[0].description
    }

    campusResponseException = false
    campusResponseFail = false

    hideAlert(alert: progressAlert)

}

When I tap on campusSelect UIView, it seems like app is on sleep mode and takes more than 20 seconds to show hidden view and same goes for hiding it.

This is my Gesture Tap:

@objc func handleCampusOpenTap(sender: UITapGestureRecognizer) {
    DispatchQueue.main.async {
      self.campusContainer.isHidden = false
    }
}

And this is the action of a button that is used to hide UIView

@IBAction func doneCampusSelectClick(_ sender : UIButton){
    let campus = campusPickerData.campusList[campusPick.selectedRow(inComponent: 0)].description

    selectedCampus.text = campus
    DispatchQueue.main.async {
      self.campusContainer.isHidden = true
    }
}

What I am doing wrong! My UIPickerView is displayed in hidden view. Is it because of I have custom class for UIPickerView's dataSource and delegate or is it related to Threading?

Just for more clarity, here is my NetworkController's method:

let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error in
        guard error == nil else{
            handler.onCampusResponseFail()
            return
        }

        guard let data = data else {
            DispatchQueue.main.async {
                handler.onCampusResponseException()
            }
            return
        }

        do {
            let jsonDecoder = JSONDecoder()

            let campusListResponse = try jsonDecoder.decode(CampusListResponse.self, from: data)

            DispatchQueue.main.async {
                handler.onCampusResponseSuccess(response: campusListResponse)
            }

        } catch let error {
            DispatchQueue.main.async {
                handler.onCampusResponseException()
            }
            print(error.localizedDescription)
        }
    })
    task.resume()

Sorry for lengthy post!

Upvotes: 0

Views: 727

Answers (1)

Pooja Gupta
Pooja Gupta

Reputation: 333

Kindly hide and show the view in main thread i.e.,

//for hiding

@objc func handleCampusOpenTap(sender: UITapGestureRecognizer) {
   DispatchQueue.main.async {
     self.campusContainer.isHidden = false
   }
}

Also for showing,

@IBAction func doneCampusSelectClick(_ sender : UIButton){
    let campus = campusPickerData.campusList[campusPick.selectedRow(inComponent: 0)].description

    selectedCampus.text = campus
    DispatchQueue.main.async {
      campusContainer.isHidden = true
    }
}

Hopefully it will help, as rest of code is preety fine.

Upvotes: 0

Related Questions