nst / STHTTPRequest

Obj-C / Cocoa HTTP requests for humans
BSD 3-Clause "New" or "Revised" License
825 stars 82 forks source link

Is there any particular reason that it works only on the main thread? #13

Closed breadncup closed 10 years ago

breadncup commented 10 years ago

Just wonder why you made this to work on only main thread?

nst commented 10 years ago

Simplicity.

ochococo commented 10 years ago

For simpler (less code in VC) UI handling after request is done… Just guessing.

nst commented 10 years ago

By default, NSURLRequest delegate methods are called on the same thread that the one the request was started from. When starting a request from a background thread, it can get really complicated to handle and test every possible cases. I don't even talk about shared cookies or credentials storage. So, it would be possible but would require quite a lot of work and would hurt maintainability.

Also, starting a request on a main thread is insignificant from a performance point of view. UIKit itself is not thread safe so 99.9% of the time you start a request from the main thread in a view controller and update the view directly from the successBlock / errorBlock.

So really it's a design decision. The goal of a library such as STHTTPRequest is to provide an easy to use abstraction over NSURLConnection / NSURLRequest. You'll always find edge cases where a high level library won't be sufficient. In these cases, you'll have to dive deeper in the software stack and remove a level of abstraction. As the maintainer of STHTTPRequest, I decided to focus on the most used scenarios.

breadncup commented 10 years ago

Thank you for the explanation. As an iOS UI programming starter, I don't still quite understand well about why multi thread request could get complicated to handle and what test cases would be, but while studying more, I may realize it.

Thanks for sharing the idea and the codes.