【问题标题】:Serial Queue sync method interrupting UIView animation on Main串行队列同步方法中断 Main 上的 UIView 动画
【发布时间】:2018-05-25 06:01:01
【问题描述】:

我遇到了一个问题,我在 main 上运行的进度条动画被对我的串行队列的同步调用打断了。

以前我没有将此队列用作串行队列,而仅用作异步队列 - 但是这会导致在处理进入后台/前台转换时出现问题,我不得不将队列视为同步并仅调用 sync 而不是异步就可以了。

现在,队列让我的应用程序完美运行——除了 UIView 动画现在卡顿(有时超过一秒),这取决于对我的串行队列的每个同步调用的负载。

是否允许对我的串行队列进行同步调用而不会在我的 UIView 动画中造成这些卡顿?我已经尝试将每个同步块中的新动画动态调用到我的串行队列中,但这并不能解决问题。

【问题讨论】:

    标签: ios swift main uiviewanimation dispatch-queue


    【解决方案1】:

    请尝试以下方法:

    1. 创建 NSOperationQueue。设置最大并发任务1。它将执行串行队列模式
    2. 创建 NSOperation 子类(假设为 CustomOperation)并在 main 方法中编写所需的代码。一旦所需的工作完成触发完成并切换到主线程并执行进度条更新内容或其他
    3. 创建 CustomOperation 的对象并将它们全部添加到第 1 步队列中

    类 CustomOperation:操作 {

    var identifier:String?
    var index:Int?
    
    override func main() {
        /// do sync or async tasks
    
        //// post completion tasks
    
        NotificationCenter.default.post(name: NSNotification.Name("TASKDONE"), object: self)
    }
    

    }

    func addOperations() {

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.trackOperation(operation:)), name: NSNotification.Name(rawValue: "TASKDONE"), object: nil)
    
    let operationQueue = OperationQueue()
    var items = [CustomOperation]()
    for index in 0..<10 {
        let operation = CustomOperation()
        operation.index = index
        operation.name = "Operation:\(index)"
        items.append(operation)
    }
    operationQueue.maxConcurrentOperationCount = 1
    operationQueue.addOperations(items, waitUntilFinished: false)
    

    }

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多