swift `UIApplication.shared.isNetworkActivityIndi​​catorVisible`管理クラス(延迟非対応)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift `UIApplication.shared.isNetworkActivityIndi​​catorVisible`管理クラス(延迟非対応)相关的知识,希望对你有一定的参考价值。

import Foundation

public class NetworkIndicatorManager {
    private init() {}
    private var lastStart: Date?
    private var count = 0
    private let lockQueue = DispatchQueue(label: "network indicator manager lock serial queue")
    public var timeout: TimeInterval? = 5
    public static let shared = NetworkIndicatorManager()

    private func toggleNetworkIndicator(isVisible: Bool) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = isVisible
    }

    public func start() {
        lockQueue.sync {
            count += 1
            toggleNetworkIndicator(isVisible: true)
            lastStart = Date()
        }
        if let timeout = timeout {
            dispatchOnMainThread(delay: timeout) {
                self.endIfTimeout()
            }
        }
    }
    public func end() {
        lockQueue.sync {
            count = max(0, count - 1)
            toggleNetworkIndicator(isVisible: count > 0)
        }
    }
    private func endIfTimeout() {
        guard
            let lastStart = lastStart,
            let timeout = timeout,
            UIApplication.shared.isNetworkActivityIndicatorVisible,
            Date() > lastStart.addingTimeInterval(timeout) else {
                return
        }
        lockQueue.sync {
            count = 0
            toggleNetworkIndicator(isVisible: false)
        }
    }
}

以上是关于swift `UIApplication.shared.isNetworkActivityIndi​​catorVisible`管理クラス(延迟非対応)的主要内容,如果未能解决你的问题,请参考以下文章

swift [Swift Notes]在学习Swift #Swift的同时收集笔记

Swift 反射

Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

swift 示例BS swift.swift

swift swift_bug.swift

ios 整理(一)swift和oc的区别