0

I used NVActivityIndicatorView to create a loading page and turn over the tab screen for 5 seconds.

ActivityIndicatorView is short-lived and disappears quickly. Using a timer, I made an event happen in five seconds. Timer.scheduledTimer()used DispatchQueue.main.async{}. I thought it runs main thread. I changed timeInterval, and it didn't change.

   DispatchQueue.main.async {

          self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
          self.performLogin()
     }

Code

import UIKit
import NVActivityIndicatorView

class AutoLoginViewController: UIViewController, NVActivityIndicatorViewable {

    var timer: Timer?
    var indicatorView: NVActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()
        setup()

        getToken(username: "yoshimi", password: "11111")

    }


    override func viewWillDisappear(_ animated: Bool) {
        stopIndicator()
    }
    // MARK: - Setup
    private func setup() {
        var midY = self.view.frame.height / 2
        var midX = self.view.frame.width / 2
        let frame = CGRect(x: midX, y: midY, width: 30, height: 30)

        indicatorView = NVActivityIndicatorView(frame: frame,
                                                    type: .ballScaleRippleMultiple)
        indicatorView?.tintColor = .white

    }

    // MARK: - Segue
    private func performLogin() {

        guard
             let tabBarController = R.storyboard.main.ramAnimatedTabBarController(),
             let mainViewController = tabBarController.viewControllers?.first as? MainViewController

        else {
              return
        }

      self.present(tabBarController, animated: true, completion: nil)

    }
    // MARK: - run indicator

    @objc func timerAction() {

        let size = CGSize(width: 30, height: 30)
        UIView.animate(withDuration: 5.0) {
            self.indicatorView.startAnimating()
            self.startAnimating(size,message: "Loading...", type: .ballScaleRippleMultiple, fadeInAnimation: nil)
        }
          timer?.invalidate()
      }

    private func stopIndicator() {

        self.indicatorView.stopAnimating()
        self.stopAnimating(nil)

    }

    // MARK: - APIs
    private func getToken(username: String?, password: String?) {
        guard
            let username = username,
            let password = password
        else {
            return
        }

        API.LoginClass.getToken(username: username, password: password) { (token, success) in
            guard success, let token = token else {
                self.loginError()
                return
            }

            Configure.token = token
            self.login(username: username, password: password)
        }



        DispatchQueue.main.async {

          self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)
          self.performLogin()
     }

    }
}
2
  • Any specific reason to use timer?, I think there is no use of timer when you want to show loader. Commented Feb 6, 2020 at 5:38
  • Server didn't set up. I can't connect network access I thought timer is good for working ActivityIndicatorView. Commented Feb 6, 2020 at 6:00

1 Answer 1

0

You can try this

self.timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: true)

     DispatchQueue.main.async {

             self.performLogin()
     }
Sign up to request clarification or add additional context in comments.

2 Comments

VC shows like gph.is/g/Zd715Ow. It changed tabbarcontroller. It shows lately.
@Sunmi set performLogin() in the timer?.invalidate()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.