swift - 如果互联网连接丢失,如何停止函数执行

标签 swift connection reachability

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

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

现在它看起来像这样:

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

当 Reachability 状态改变时,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() 函数的执行

最佳答案

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

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

在您的代码中,您可以直接停止您的 nsoperation:

全局变量:

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

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

关于swift - 如果互联网连接丢失,如何停止函数执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37010526/

相关文章:

ios - 将状态/绑定(bind)传递给 UIViewRepresentable

php - 连接到远程 mysql 服务器

ios - 从 Swift 中的导航后退按钮解除 segue

ios - 将 Xcode 代码库转换为框架

mysql - SHOW PROCESSLIST 仅显示 20 个连接,而服务器表示 "has more than ' max_user_connections 的事件连接”

ios - CNCopySupportedInterfaces() 没有返回任何值,尽管 wifi 已连接

iOS - 可达性 isReachable 不工作

ios - iOS 中的架构问题

swift - 将日期选择器保存到 userdefaults 并在加载时设置值

android - 在Android中将多个设备连接到蓝牙