0

I am new to iOs app development and I wonder if my app architecture is correct. A bug in my app make me believe that some of my variables are initialized several times.

To summarize my app : 2 screens and 2 views but only one controller. Depending the current view, the viewDidLoad have different results. I don't believe this is the right way to do. I guess the idea would be to create a controller for each view ?

But my main concern here : in my viewDidLoad, when the main screen is loaded, I set up a notification observer. I believe( because of bugs ) that this observer is setup each time the screen load and then is called multiple times.

My questions here : Where to put this listener , is there a place that will run the code only once this view is loaded ? It should be fixed by putting this listener into a variable ?

Is the AppDelegate application function a right place for that kind of things ?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Remove iphone sleep mode
    UIApplication.shared.isIdleTimerDisabled = true

    //Setup the external scanner object
    self.scanner.delegate = self
    self.scanner.connect()

    // Init the saved values
    let defaults = UserDefaults.standard

    // --------------- MAIN VIEW ---------------
    if(mainView != nil){

        // Add a notification receiver
        // Will receive results ### request
        NotificationCenter.default.addObserver(self, selector: #selector(mainTextNewLineNotification), name: Notification.Name(rawValue: "sendingToView"), object: nil)

        // Layout setup
        mainTextView.layer.cornerRadius = 6.0

        [...]
    }

    // --------------- SETTINGS VIEW ---------------
    if(settingsView != nil){

        //Fill the field with saved values
        inputHost.text = defaults.string(forKey: "hostname")
        inputPort.text = String(defaults.integer(forKey: "port"))
        if(defaults.string(forKey: "timeout") != nil){
            inputTimeout.text = defaults.string(forKey: "timeout")
        }
        if(UserDefaults().string(forKey: "confirmSwitch") == "On"){
            confirmSwitch.isOn = true
        } else {
            confirmSwitch.isOn = false
        }
    }
}
Priyanka
  • 169
  • 3
  • 10
Phelineg
  • 59
  • 9
  • Firstly you may control two views from one controller if your app is small and these views do similar things but it is not best practice. It is better to create controller for each view. You may read about MVC architecture, it is easy for beginners. as @Dharmesh said remove notification observer when mainView disappear. If you will not remove observer, it will call multiple times. – Yucel Bayram Jan 10 '19 at 05:10

1 Answers1

0
  1. You don't need to care about removeObserver logic in your case (since you use simple subscription using -selector, not -block). From Apple doc: If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc(deinit) method.

  2. Each time when you initialize instance of UIViewController and if system loads its view viewDidLoad will be called. viewDidLoad is called once during UIViewController lifecycle. So your logic for subscription is correct.

I don't know your application entire logic so it is hard what is the reason of your bug.

Eugene El
  • 81
  • 3