When to use NSEnumerationConcurrent

前端 未结 1 928
轻奢々
轻奢々 2021-02-19 10:18

Every now and then, I notice that I\'m using a block to iterate over a collection without writing to any shared data or causing any side effects. I consider adding in an NSEnume

1条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 11:01

    Generally, you'd only use concurrency when the operations to be performed are relatively "heavy". Even then, using the raw concurrency offered by enumerateObjectsWithOptions: could easily be problematic if the parallelism is the wrong granularity for the task at hand.

    GCD is really damned efficient at enqueuing and processing stuff, but that code is quite likely going to end up calling malloc() to copy the block (depends on whether the block has unique captured state).

    The answer to your second question fills many books, most useless.

    Taking non-concurrent code and making it concurrent is generally a Very Hard Problem rife with nightmarish bugs. Yet, designing for concurrency up front can be exceptionally time consuming. Worse, implementing for future concurrency without actually using it just leads to a nightmarish debugging experience when you do turn it on.

    One key point; when considering concurrency, focus on making entire sub-graphs of objects thread-isolated save for exceedingly well defined boundary API that spans threads/queues. A good example of this is Core Data.

    0 讨论(0)
提交回复
热议问题