I have a class which I want to confine to a thread. To be precise I want it's void returning methods to be accessed on a certain thread or serial queue created by the class privately. To acheive this I looked at the NSManagedObjectContext
API as a reference. There is a perform(block:() -> Void)
method which can do this.
This comes with a code pattern that I am a bit uncomfortable. The user of this API needs to make calls to the object in the block of the perform(block:() -> Void)
method. Is there a more elegant solution wherein I can declare that all methods and properties are confined to this particular thread. So essentially the user can access the methods but the internal operation happen on the intended thread.
I know the option where I can go to each method and dispatch work of that method into the intended thread. But this is an additional overhead to write the dispatch code.