adarshaU
adarshaU

Reputation: 960

How to stop multiple calls to a method in swift 3 on network status change?

I want to execute a method callFormDataPushOnBackground only once per network status change. But now whenever network status changed to online ".reachable" calling multiple times because of listener. How to allow only one call and discard the rest? i tried with listenerQueue.asyncAfter but its not working.

Any help appreciated.

func rechability()->(Isconected:Bool,status:Error?){

    var conectivity:Bool = false
    var statusError:Error?

    var offlinePushFlag = true
    manager?.listener = { status in

        switch status {

        case .notReachable:

            conectivity = false
            statusError = typeError.eNotRechable
            offlinePushFlag = true
            self.updatetheSyncLable()
            print("The network is not reachable")

        case .unknown :

            statusError = typeError.eUnknown
            conectivity = false
            offlinePushFlag = true
            self.updatetheSyncLable()

            print("It is unknown whether the network is reachable")

        case .reachable(.ethernetOrWiFi):

            statusError = typeError.eRechable
            conectivity = true
            self.updatetheSyncLable()
            var x = 1

            self.manager?.listenerQueue.asyncAfter(deadline: .now() +  .milliseconds(10000 * x + 2), execute: {
                x = x + 1
                if offlinePushFlag == true{
                    offlinePushFlag = false
                    print("I calledonlyoneTimeWIFI")
                    self.callFormDataPushOnBackground()

                }
            })

            print("The network is reachable over the WiFi connection")

        case .reachable(.wwan):

            statusError = typeError.eWanRechable
            conectivity = true
            self.updatetheSyncLable()
            if offlinePushFlag == true{
                print("I calledonlyoneTimeWWAN")
                offlinePushFlag = false
                self.callFormDataPushOnBackground()
            }
            print("The network is reachable over the WWAN connection")

        }
    }

    manager?.startListening()



    return (conectivity,statusError)
}

Upvotes: 0

Views: 539

Answers (1)

Vladyslav Zavalykhatko
Vladyslav Zavalykhatko

Reputation: 17414

If I understand you correctly, You can use a DispatchSemaphore to ensure that one call completes before the next can start

let semaphore = DispatchSemaphore(value:1)
manager?.listener = { status in
    // some code
    // you completed your work here
    self.semaphore.signal()
}

Note, You should be careful not to block the main queue as that will cause your app to become non-responsive.

Like:

let analyticsQueue = DispatchQueue(label:"analyticsQueue")

let semaphore = DispatchSemaphore(value:1)


manager?.listener = { status in
    analyticsQueue.async {
        self.semaphore.wait()
        // code here
        self.semaphore.sygnal()
    }
}

Upvotes: 2

Related Questions