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!;
    }
  • 相关阅读:
    Monkey界面版测试工具
    手游兼容性测试
    周版本制度
    08 | 事务到底是隔离的还是不隔离的?
    jackson
    localDateTime和Date
    服务器被攻击后数据库密码被破解
    GC收集器
    linux安装nginx,设置代理,负载均衡
    微信(公众,商户,开放)平台的区别
  • 原文地址:https://www.cnblogs.com/willbin/p/7883724.html
Copyright © 2011-2022 走看看