[英]print - Background or main thread operation
This might sound quite basic and stupid but it has been bothering me for a while. 这听起来很基础也很愚蠢,但已经困扰了我一段时间。 How can print be classified in terms of operation - main or background ? 如何按操作分类印刷-主印刷或背景印刷?
As a small test, on putting print
in a background task - web service call : 作为一个小测试,将print
放入后台任务-Web服务调用:
Webservice().loadHeadlinesForSource(source: source) { headlines in
print("background print")
self.headlineViewModels = headlines.map(HeadlineViewModel.init)
DispatchQueue.main.async {
print("main thread print")
completion()
}
}
Both the print statements get printed. 这两个打印语句都将被打印。 From previous experience, if print was a main thread task, Xcode would have given me a warning saying that I need to put that in main thread. 根据以前的经验,如果print是主线程任务,那么Xcode会警告我说我需要将其放入主线程。 This is an evidence that print is not a main thread operation. 这证明打印不是主线程操作。 Note that I am not saying print is a background task. 请注意 ,我并不是说打印是后台任务。
However, I have this understanding that since print
displays output on Console, it is not a background operation. 但是,据我了解,由于print
在控制台上显示输出,因此它不是后台操作。 As a matter of fact all logging operations are not. 实际上,并非所有日志记录操作。
How would one justify the classification ? 如何证明分类呢?
It seems what you consider to be a main thread operation is a call that needs to be performed on the main thread. 看来您认为是主线程操作的是需要在主线程上执行的调用。 From that perspective you are correct and have found an evidence of this call not being a main thread operation . 从这个角度来看,您是正确的,并且发现此调用不是主线程操作的证据。
But does this have anything to do with anything else? 但这和其他东西有关系吗? Internally if needed this method may still execute its real operation on the main thread or any other thread for what we care. 在内部,如果需要,此方法仍可以在主线程或我们关心的任何其他线程上执行其实际操作。 So in this sense a main thread operation is a restriction that call needs to be performed on main thread but has nothing to do with its execution or multithreading. 因此,从这个意义上说, 主线程操作是一种限制,该调用需要在主线程上执行,但与它的执行或多线程无关。
Without looking into what print
does in terms of coding we can see that it works across multiple "computers". 如果不研究print
在编码方面的功能,我们可以看到它可以在多台“计算机”上工作。 You can run your app on your device (iPhone) while plugged and Xcode on your computer will print out logs. 您可以在插入设备的同时在设备(iPhone)上运行您的应用,并且计算机上的Xcode将打印出日志。 This makes a suspicion that print
is much like call to the remote server in which case the server is responsible for serializing the events so it makes no difference what thread the client is on. 这令人怀疑print
与调用远程服务器很像,在这种情况下,服务器负责序列化事件,因此客户端所处的线程没有区别。 There are other possibilities such as dropping logs into file and then sending it which really makes little difference. 还有其他可能性,例如将日志放入文件然后发送,这实际上没有什么区别。
So How can print be classified in terms of operation - main or background? 那么如何根据操作分类打印-主纸还是背景纸呢? The answer is probably none. 答案可能是没有。 The call is not restricted to any thread so it is not main. 该调用不限于任何线程,因此它不是主要的。 It will probably lock whatever thread it is on until the operation is complete so it is not background either. 它可能会锁定它所处的线程,直到操作完成为止,因此也不会成为后台线程。 Think of it like Data(contentsOf: <#T##URL#>)
which will block the thread until data from given URL is retrieved (or exception is thrown). 可以将其视为Data(contentsOf: <#T##URL#>)
,它将阻塞线程,直到从给定URL中检索到数据(或引发异常)为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.