why does is monitorNet() being called as many times as i navigate to another view controller?
I am having an issue with multiple calls when navigating to a view controller and coming back to the previous page. Basically, I have a method Net.shared.monitored()
that tells me if I have an internet connection or not, based on that it modifies a variable. Problem is that the method is being called as many times as I navigate and get back to that page. Below are the related code:
in Home viewController:
in viewDidLoad:
Net.shared.monitorNet(){ [self]value in
if(value == "connected"){
print(Int(Float(bounds.size.height)))
if(initialized == false){
initFetchRequests()
}
DispatchQueue.main.async { [self] in
let image = UIImage(named: "art.scnassets/con.png")?.resized(to: CGSize(width: Int(0.12*Float(bounds.size.width)), height: Int(0.09*Float(bounds.size.width))))
if( connectionNotifier.image != image!){
connectionNotifier.image = image!
}
}
if connection != true{
connection = true
print("offSwitch")
}
} else {
print("none")
DispatchQueue.main.async { [self] in
let image = UIImage(named: "art.scnassets/noCon.png")?.resized(to: CGSize(width: Int(0.12*Float(bounds.size.width)), height: Int(0.09*Float(bounds.size.width))))
if( connectionNotifier.image != image!){
connectionNotifier.image = image!
}
}
if connection != false{
connection = false
print("onSwitch")
}
}
}
variable in question:
var connection:Bool = true{
didSet { // noOnlineNotifier Bool is true else if connection then close then show connected
print("outChanged")
DispatchQueue.main.async { [self] in
print("con tha changed")
let w = Int(Float(bounds.size.width)) - Int(0.12*Float(bounds.size.width))
let y = 20 + Int(0.12*Float(bounds.size.width))/2 + Int(0.09*Float(bounds.size.width))
UIView.animate(withDuration: 1, delay: 0, options: .curveLinear, animations: {
connectionNotifier.frame = CGRect(x: w/2, y: y, width: Int(0.12*Float(bounds.size.width)), height: Int(0.09*Float(bounds.size.width)))},completion: { finish in
UIView.animate(withDuration: 1, delay: 2.5,animations: {
connectionNotifier.frame = CGRect(x: w/2, y: -y, width: Int(0.12*Float(bounds.size.width)), height: Int(0.09*Float(bounds.size.width)))
},completion: nil)})
}}
}
function to navigate to other view Controller:
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "ServerError")
self.present(nextViewController, animated:true, completion:nil)
the other viewController:
import UIKit
class ServerError: UIViewController {
var serverErrV:UIView!
override func viewDidLoad() {
super.viewDidLoad()
serverErrV = self.view
serverErrV.addSubview(noServerLayout)
// Do any additional setup after loading the view.
homeUI.addTarget(self, action: #selector(BackHome), for: UIControl.Event.touchDown)
homeUI.addTarget(self, action: #selector(Home().buttonOut), for: UIControl.Event.touchUpInside)
homeUI.addTarget(self, action: #selector(Home().buttonOut), for: UIControl.Event.touchDragExit)
}
@objc func BackHome (){
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Home")
self.present(nextViewController, animated:true, completion:nil)
}
thank you all for your time