2

I use lib named RealReachability that getting currently reachability status in real time!

And i have a function that getting data from my server.

Now its look like this:

    RealReachability.sharedInstance().reachabilityWithBlock { (status: ReachabilityStatus) in
        switch status {
        case .RealStatusNotReachable:
            break
        default:
        operationQueue.addOperationWithBlock {
            GettingDataFromServer() }
        }
    }

Also RealReachability can send Notification when Reachability status changed. Its look like this:

var operationQueue = NSOperationQueue()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyController.networkChanged), name: kRealReachabilityChangedNotification, object: nil)

  func networkChanged(notification: NSNotification) {
    print("NetworkChanged")

    let status = RealReachability.sharedInstance().currentReachabilityStatus()

    switch status {
    case .RealStatusNotReachable:
        print("try to stop Operation")
        operationQueue.cancelAllOperations()
        ShowInternetConnectionErrorView()

    default:
        print("Internet OK!")
    }



}

What do I need to stop GettingDataFromServer() function execution when reachability status changed to .RealStatusNotReachable

Dmitry
  • 2,963
  • 2
  • 21
  • 39

1 Answers1

0

In RealReachability library, GLobalRealReachability is the shared instance. So you can query this class to know the current status:

let status = GLobalRealReachability.currentReachabilityStatus
if (status == RealStatusNotReachable)
{
    //do whatever you want when internet is not reachable
    // To stop your observer you can do:
    NSNotificationCenter.defaultCenter().removeObserver(self)
    // ...or by specify the exact name:
    NSNotificationCenter.defaultCenter().removeObserver(self, name: kRealReachabilityChangedNotification, object: nil)
}

In your code to stop directly your nsoperation you can do:

global var:

var  operation1 : NSBlockOperation!

self.operation1 : NSBlockOperation = NSBlockOperation ({
            GettingDataFromServer() 
})

and modify your code up :

RealReachability.sharedInstance().reachabilityWithBlock { (status: ReachabilityStatus) in
        switch status {
        case .RealStatusNotReachable:
            if operationQueue.operations.containsObject(operation1) {
                 for op in operationQueue.operations {
                     if op == operation1 {
                        op.cancel()
                     }
                 }
            }
            break
        default:
            operationQueue.addOperation(operation1)
    }

UPDATE: Like I've report in my last comment try to declare:

var operationQueue = NSOperationQueue()

on a shared instance class or to the appDelegate.

For example about the last solution you can do:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let aVariable = appDelegate.someVariable

and use your queue wherever you want around your project.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • I want to stop executing function GetDataFromServer() if (status == RealStatusNotReachable)! You suggest me to start function again! – Dmitry May 03 '16 at 18:33
  • Alessandro Ornano, you suggest me to removeObserver... but i need to stop function GetDataFromServer()! – Dmitry May 03 '16 at 18:43
  • This is another change, try now and report your results – Alessandro Ornano May 03 '16 at 20:42
  • 1
    Alessandro Ornano, thank you for your help! I think you suggestion is right! but i have some error in my code... I need a little time to find out ! – Dmitry May 03 '16 at 22:14
  • I think your error is this: var operationQueue = NSOperationQueue() , you re-create each time the queue, so you lost the old queue in memory and search an inexistent operation in the current new queue. The solution is to create one and one only time the queue, for example on a shared instance or appDelegate a the launch, and take reference to it as your global queue – Alessandro Ornano May 04 '16 at 06:42