【问题标题】:How to stop function execution if internet connection is lost如果互联网连接丢失,如何停止函数执行
【发布时间】:2023-04-03 03:15:01
【问题描述】:

我使用名为 RealReachability 的库来实时获取当前的可达性状态!

我有一个从我的服务器获取数据的函数。

现在它看起来像这样:

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

RealReachability 还可以在可达性状态更改时发送通知。它看起来像这样:

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!")
    }



}

当可达性状态更改为 .RealStatusNotReachable 时,我需要什么来停止 GettingDataFromServer() 函数的执行

【问题讨论】:

  • 你的意思是func什么时候在执行过程中?
  • Arun Gupta,是的,我的意思是 func 正在执行过程中!!!
  • 您可能必须使用 NSOperationqueue 或 dispatchqueue 来执行 func 并在收到通知时取消操作。
  • Arun Gupta,我尝试使用 NSOperationqueue ......但这对我没有帮助!我已经编辑了我的问题...请看!

标签: swift connection reachability


【解决方案1】:

RealReachability 中,libraryGLobalRealReachability 是共享实例。所以你可以查询这个类来了解当前的状态:

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)
}

在你的代码中直接停止你的ns操作你可以这样做:

全局变量:

var  operation1 : NSBlockOperation!

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

并修改你的代码:

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)
    }

更新: 就像我在上一条评论中报告的那样尝试声明:

var operationQueue = NSOperationQueue()

在共享实例类或 appDelegate 上。

例如关于你可以做的最后一个解决方案:

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

并在项目周围的任何地方使用您的队列。

【讨论】:

  • 我想停止执行函数 GetDataFromServer() if (status == RealStatusNotReachable)!你建议我重新开始工作!
  • Alessandro Ornano,你建议我删除Observer...但我需要停止函数GetDataFromServer()!
  • 这是另一个变化,现在试试并报告你的结果
  • 亚历山德罗·奥纳诺,感谢您的帮助!我觉得你的建议是对的!但我的代码中有一些错误...我需要一点时间来找出答案!
  • 我认为您的错误是这样的: var operationQueue = NSOperationQueue() ,您每次都重新创建队列,因此您丢失了内存中的旧队列并在当前新队列中搜索不存在的操作。解决方案是只在队列中创建一个且一个,例如在共享实例或 appDelegate 上启动,并将其作为全局队列引用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
相关资源
最近更新 更多