热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么在Swift上显示警报时需要DispatchQueue?-WhywouldyouneedaDispatchQueuewhenshowingAlertsonSwift?

IamnewtoSwift,andtryingtoexamineafinishedproject.Butthereissomethingicouldntunder

I am new to Swift, and trying to examine a finished project. But there is something i couldn't understand.

我是Swift的新手,并试图检查一个完成的项目。但有一些我无法理解的东西。

After a network request is completed, the app show an alert under a condition.

网络请求完成后,应用程序会在条件下显示警报。

func makeNetworkRequest() {
   //newtork result...
   DispatchQueue.main.async {
       self.showAlert(versionMessage: "Error")
   }
} 

func showAlert(versionMessage: String) {
   let alert = UIAlertView(title: "", message: versionMessage, delegate: self)
   alert.show()
}

However, it is done with a DispatchQueue. Why would anyone need to use DispatchQueue in this situation.

但是,它是使用DispatchQueue完成的。为什么在这种情况下任何人都需要使用DispatchQueue。

2 个解决方案

#1


0  

It’s a conscious design decision from Apple’s side to not have UIKit be thread-safe. Making it thread-safe wouldn’t buy you much in terms of performance; it would in fact make many things slower. And the fact that UIKit is tied to the main thread makes it very easy to write concurrent programs and use UIKit. All you have to do is make sure that calls into UIKit are always made on the main thread. So according to this the fact that UIKit objects must be accessed on the main thread is a design decision by apple to favor performance.

苹果方面的一个有意识的设计决定是没有UIKit是线程安全的。使其具有线程安全性并不会对性能产生太大影响;事实上它会让很多东西变慢。 UIKit与主线程绑定的事实使得编写并发程序和使用UIKit变得非常容易。您所要做的就是确保始终在主线程上调用UIKit。因此,根据这一点,UIKit对象必须在主线程上访问的事实是苹果的设计决定有利于性能。

for more detailed information you can go through this article

有关更多详细信息,请阅读本文

https://www.objc.io/issues/2-concurrency/thread-safe-class-design/

https://www.objc.io/issues/2-concurrency/thread-safe-class-design/

In your case , You are showing alert from another thread so you have to write code under the MainThread so , you can get the main thread using below code

在您的情况下,您正在显示来自另一个线程的警报,因此您必须在MainThread下编写代码,因此,您可以使用下面的代码获取主线程

DispatchQueue.main.async {
       // Your UI Updation here 
   }

Reason

原因

In Cocoa Touch, the UIApplication i.e. the instance of your application is attached to the main thread because this thread is created by UIApplicatioMain(), the entry point function of Cocoa Touch. It sets up main event loop, including the application’s run loop, and begins processing events. Application's main event loop receives all the UI events i.e. touch, gestures etc.

在Cocoa Touch中,UIApplication即应用程序的实例附加到主线程,因为该线程是由Cocoa Touch的入口点函数UIApplicatioMain()创建的。它设置主事件循环,包括应用程序的运行循环,并开始处理事件。应用程序的主事件循环接收所有UI事件,即触摸,手势等。

#2


1  

You´ll for sure notice that the alert will lag if you don´t show the alert on the main thread, that´s because your UI code does always have to be done on your main thread.

如果您没有在主线程上显示警报,那么您肯定会注意到警报会滞后,这是因为您的UI代码始终必须在主线程上完成。

So if you're on a background thread and want to execute code on the main thread, you need to call async(). That´s way you call DispatchQueue.main, which is the main thread.

因此,如果您在后台线程上并且想要在主线程上执行代码,则需要调用async()。这就是你调用DispatchQueue.main的方式,这是主线程。


推荐阅读
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • t-io 2.0.0发布-法网天眼第一版的回顾和更新说明
    本文回顾了t-io 1.x版本的工程结构和性能数据,并介绍了t-io在码云上的成绩和用户反馈。同时,还提到了@openSeLi同学发布的t-io 30W长连接并发压力测试报告。最后,详细介绍了t-io 2.0.0版本的更新内容,包括更简洁的使用方式和内置的httpsession功能。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
author-avatar
mobiledu2502901317
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有