How the reader/ writer pattern as mentioned in the stackoverflow question will work for UI updates ? If we are using an array with reader / writer pattern (as mentioned in that question) for a UICollectionView's data source then how the collection view's insert / delete / reload method calls will be used ? In simple words, I want to know how UI part will fit in such reader - writer pattern code ?
1 Answers
Anything on the UI has to run on the main thread, and arrays of objects are not thread safe. So if you want to edit the data from threads other than the main thread, and reading it for the UI (from the main thread), you may run into unexpected problems. The reader / writer pattern from the link ensures thread-safety by making sure the writes can only happen one after another (synchronously). You can then edit your datasource from background threads and updating the uicollectionview on the main thread safely without the app crashing occasionally.
However, if your data source contains core data objects, then you must fetch the objects from a managed object context that runs on the main thread. If you edit the data using a managed object context which runs on a background thread, you have to push the changes back to a parent context, then read it using a moc that runs on the main thread before using the data to update your collection view. The reader / writer pattern will not help you in such cases.

- 469
- 4
- 10