Asynchronous vs Synchronous vs Threading in an iPhone App

后端 未结 8 1385
失恋的感觉
失恋的感觉 2021-01-30 05:28

I\'m in the design stage for an app which will utilize a REST web service and sort of have a dilemma in as far as using asynchronous vs synchronous vs threading. Here\'s the sce

相关标签:
8条回答
  • 2021-01-30 06:04

    Why can't you use an asynchronous request like so:

    - (NSArray *)users {
         if(users == nil && !didLaunchRequestAlready )
            users = do_async_request // Looks good to me
         return users;
     }
    

    Asynchronous is absolutely the only option - the only real question is if you want to start using separate threads, or if you want to just use the asynch calls. Start there and look at managing threads if you really need to.

    0 讨论(0)
  • 2021-01-30 06:11

    I'd recommend the asychronous way, no question. Then, load the information only when needed, and use a delegate system to give that information to the correct object.

    You don't want to block the UI. Ever. And loading information asynchronously allows you better control over what's happening so you can throw up an error message if needed.

    0 讨论(0)
  • 2021-01-30 06:12

    I personally look at what is being done, I will ususally use an asyc request to ensure that the UI doesn't block, however, I MAY during the course of that request disable the UI of my application.

    A prime example of this is in an application that I built with a "search" button. Once the search was triggered as an async request I would disable the button until the response came back, effectivly limiting the ability for the user to spawn a second asyc request.

    Doing this, at least I can prevent the need for priorites, granted this only works if you can in an easy to do way, limit your users to one action at a time.

    0 讨论(0)
  • 2021-01-30 06:16

    An official response is that you should almost always go asynchronous and that synchronous is bad. I found ASIHTTPRequest makes asynchronous requests easy-peasy.

    0 讨论(0)
  • 2021-01-30 06:18

    I don't think that there's a "right" answer. It seems that you understand the compromises involved and you just need to make your design around those.

    A few extra random points: sometimes your application forces a particular approach. For example, many of the convenience (i.e., synchronous) methods won't allow authentication. For me that meant that my decision was made.

    For Yummy I ended up not using threads. I made all my network calls asynchronous and I used the default XML parser (which works using call backs). Since it's all event driven and each unit is small it allows the GUI to be pretty fluid without having the complexity of threading.

    I use a state machine to figure out why I'm getting a particular response, and a queue so that I only need to have a single operation "in flight" at any given time. There's a distinct order to most requests so I have no need for a priority system.

    The networking code is the most complex in my app and it took a long time to get working much less robust!

    0 讨论(0)
  • 2021-01-30 06:22

    I'm not discounting asynchronous delegate calls, but I usually end up using a threaded worker class with synchronous requests. I find it's easier in the long run to have a well defined, threaded API, instead of filling up your controller with code managing the state between asynchronous methods. You could even make asynchronous in your worker thread, although usually it's easier to use the synchronous methods unless they don't support a feature you need to use. Of course, all of this depends on the circumstances, I can think of many situations where simply using the asynchronous methods would be the best route.

    Definitely consider NSOperationQueue if you go this route; it greatly simplifies creating multiple worker threads, and it also supports priorities and dependancies between operations. Right now there are some problems with it on 10.5, but I haven't heard of any issues on the iPhone.

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