Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it worth it to refactor common HTTP request code, performance-wise?
    text
    copied!<p>I've been developing an iPhone app, which handed by an experienced developer. I'm just an apprentice programmer and still struggling with practical Objective-C/iOS application development (I have learned Java and PHP on my own, but objective-c is nothing like these to me).</p> <p>Our app is just another "web-centric" (I don't even know this word is appropriate...) app which heavily relies on server-side operations, making frequent http post request every single time (such as tracking user locations, send messages to another users etc.).</p> <p>When I was assigned to develop this app, I saw in the code, that every single http request was written inside each method. Each request was done by dispatching another thread, and each action were written for those requests' response accordingly.</p> <p>E.g. </p> <pre><code>-(void) methodA { // Making http request headers... // Dispatch another thread dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT , 0); dispatch_async(queue, ^{ // Send synchronous request and handle the response... }); } -(void) methodB { // Making http request headers... // Dispatch another thread dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT , 0); dispatch_async(queue, ^{ // Send synchronous request and handle the response... }); } </code></pre> <p>The codes like above are every where when the app needs to send request to the server. </p> <p>I'm wondering, why he didn't create a class that handles http requests.<br> In Java, you could create a class that make synchronous request to the server: </p> <pre><code>public class ClassHttpRequest { public int makePost { // Send synchronous request and return result... } } </code></pre> <p>Then make an instance of this class and execute it's instance method (in this case, makePost) inside a thread: </p> <pre><code>public class methodA { Thread t = new Thread(new Runnable() { public void run() { public ClassHttpRequest requestHandler = new ClassHttpRequest(); if (success == requestHandler.makePost()) { // Handle response... } } } }); t.start(); } </code></pre> <p>Is there any performance penalty or issues in creating a class and let it handles frequent http request in Objective-C? Or, it's just simply not "recommended" or something? I have heard that, in Objective-C, it is not common to use try-catch statement for exception handling, because it would consume much resources. I do have read several iOS and Objective-C books (and googled), but such kind of "practical" answer for real application development is hard to find, most of the time it's rather confusing to beginners like me.</p> <p>I should ask him why he didn't create a such class, but he's away now and I couldn't get in touch with him. Also, I belive that the professionals here in stackoverflow can provide me much more accurate and concise solutions than my predecessor. (I have asked several questions and already got what I wanted to know.) </p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload