zoukankan      html  css  js  c++  java
  • Main Thread Checker 问题解决

    1. without a return value

    https://developer.apple.com/documentation/code_diagnostics/main_thread_checker

    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{ // Correct
            self.label.text = [NSString stringWithFormat:@"%lu bytes downloaded", data.length];
        });
    }];
    [task resume];
    

      

    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
       if let data = data {
          DispatchQueue.main.async { // Correct
             self.label.text = "(data.count) bytes downloaded"
          }
       }
    }
    task.resume()
    

      

    2. with a return value

    https://stackoverflow.com/questions/45832155/how-do-i-refactor-my-code-to-call-appdelegate-on-the-main-thread

    static var realDelegate: AppDelegate?;
    
    static var appDelegate: AppDelegate {
        if Thread.isMainThread{
            return UIApplication.shared.delegate as! AppDelegate;
        }
        let dg = DispatchGroup();
        dg.enter()
        DispatchQueue.main.async{
            realDelegate = UIApplication.shared.delegate as? AppDelegate;
            dg.leave();
        }
        dg.wait();
        return realDelegate!;
    }
  • 相关阅读:
    操作系统六文件管理
    Educational Codeforces Round 38 (Rated for Div. 2) ABCD
    51nod 1100 斜率最大
    51nod 最小方差
    51nod 1065 最小正子段和
    P1280 尼克的任务
    牛客小白月赛2
    Codeforces Round #210 (Div. 1) B 二分+dp
    江西财经大学第一届程序设计竞赛
    51nod 1596 搬货物
  • 原文地址:https://www.cnblogs.com/willbin/p/7883724.html
Copyright © 2011-2022 走看看