打印-后台或主线程操作 [英] print - Background or main thread operation

查看:107
本文介绍了打印-后台或主线程操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很基础也很愚蠢,但它困扰了我一段时间. 如何按照操作方式对打印进行分类-主印刷还是背景印刷?
作为一个小测试,将print放入后台任务-Web服务调用:

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 :

Webservice().loadHeadlinesForSource(source: source) { headlines in
            print("background print")
            self.headlineViewModels = headlines.map(HeadlineViewModel.init)

            DispatchQueue.main.async {
                print("main thread print")
                completion()
            }
        }  

两个打印语句都被打印.根据以前的经验,如果print是主线程任务,那么Xcode会警告我说我需要将其放入主线程. 这是打印不是主线程操作的证据. 请注意,我并不是说打印是后台任务.
但是,我了解到,由于print在控制台上显示输出,因此它不是后台操作.事实上,并非所有日志记录操作.
如何证明分类的正确性?

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. 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. 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.

如果不研究print在编码方面的作用,我们可以看到它可以在多台计算机"上工作.您可以在插入设备后在设备(iPhone)上运行您的应用程序,并且计算机上的Xcode会打印出日志.这使人怀疑print非常类似于对远程服务器的调用,在这种情况下,服务器负责序列化事件,因此客户端所处的线程没有区别.还有其他可能性,例如将日志拖放到文件中然后发送,这实际上没有什么区别.

Without looking into what print does in terms of coding we can see that it works across multiple "computers". You can run your app on your device (iPhone) while plugged and Xcode on your computer will print out logs. 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. There are other possibilities such as dropping logs into file and then sending it which really makes little difference.

那么如何按操作方式对打印进行分类-主打印还是背景打印?答案可能是没有.该调用不限于任何线程,因此它不是主要的.它可能会锁定它所处的线程,直到操作完成为止,因此也不会成为后台线程.可以将其想象为Data(contentsOf: <#T##URL#>),它将阻塞线程,直到从给定URL中检索到数据(或引发异常)为止.

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).

这篇关于打印-后台或主线程操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆