I add an observer in a function which I call on the start. It is only initiated once(I thoroughly checked). On the safer side, I also remove observers before I add observers. below is the function
func setupLiveCycleNotifications() {
SweeprClient.removeNotificationObservers()
NotificationCenter.default.addObserver(self,
selector: #selector(appMovedToBackground),
name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(appMovedToForeground),
name: UIApplication.didBecomeActiveNotification, object: nil)
}
In my UNIT TEST functions, I tried to test this feature. I initiate the class and I post the notifications like shown below:
let backgroundExpectation = self.expectation(description: "background expectation")
let foregroundExpectation = self.expectation(description: "foreground expectation")
Client.modules!.subscribe(module: "session", event: SessionProvider .Events.application_lifecycle.rawValue,
tag: nil) { result, tag in
if result == "background" {
backgroundExpectation.fulfill()
}
if result == "foreground" {
foregroundExpectation.fulfill()
}
}
NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
wait(for: [backgroundExpectation], timeout: Config.Timeout)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
wait(for: [foregroundExpectation], timeout: Config.Timeout)
The feature is to call back to my modules where I fulfill the expectation but, the problem is both background and foreground are called TWICE. I don't know why. I reset multiple times.I removed observers in many places but, nothing works. Even if I comment the posting of notifications.Nobody calls my module. So, I am sure I am the only one who calls these notifications.
- I removed observers in multiple places
- I commented out the post notifications but the expectation only gets timed out, Nobody calls it