为特定单例创建 Grand Central Dispatch 队列
我有一个用于创建应用程序范围报告的单例。当应用程序将数据传递给单例时,单例会格式化数据以供在报告中使用。我使用 Grand Central Dispatch,以便报告创建不在主线程上。
我的问题是,在初始化单例时创建一个仅由单例使用的 Grand Central Dispatch 队列是不好的做法吗?我希望单个线程与单例关联,因为如果我不这样做,报告的某些部分很容易不同步,从而破坏报告的格式。如果这是不好的做法,那么还有什么其他模式适合这个问题,但能给我快速且易于使用的代码。
I have a singleton that I use for creating an application wide report. As data is passed to the singleton by the application the singleton then formats the data for use in the report. I use Grand Central Dispatch so that the report creation is not on the main thread.
My question is, would it be bad practise to create a Grand Central Dispatch queue when the singleton is initialised that is then only used by the singleton. I'd like a single thread to be associated with the singleton because if I don't some parts of the report are prone to get out of sync breaking the formatting of the report. If this is bad practise then what other pattern would be suitable for this problem but giving me quick and easy to use code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的方法是正确的。串行调度队列用于同步对特定资源的访问。他们以先进先出的方式处理请求,这使得它们适合您的要求。我认为
singleton
部分在这里没有那么重要。Your approach is the right one. Serial dispatch queues are meant for synchronizing access to a particular resource. They process requests FIFO which makes them suitable for your requirement. I don't think the
singleton
part figures as much here.