在iPhone应用程序中的异步vs同步vs线程 [英] Asynchronous vs Synchronous vs Threading in an iPhone App

查看:84
本文介绍了在iPhone应用程序中的异步vs同步vs线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于一个应用程序的设计阶段,该应用程序将使用REST Web服务,并且在使用异步vs同步和线程方面存在两难处境。

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 scenario.

假设您有三个选项可供深入,每个选项都有自己的基于REST的资源。我可以延迟加载每个同步请求,但这将阻止用户界面,并阻止用户击中一个后退导航按钮,同时检索数据。这种情况几乎适用于除您的应用程序需要登录屏幕之外的(除了)。我看不到任何理由使用同步HTTP请求与异步,因为这个原因。唯一有意义的是让一个工作线程发出同步请求,并在请求完成时通知主线程。这将防止块。因此,问题在于标记您的代码,并查看哪个具有更多开销,线程同步请求或异步请求。

Say you have three options to drill down into, each one having its own REST-based resource. I can either lazily load each one with a synchronous request, but that'll block the UI and prevent the user from hitting a back navigation button while data is retrieved. This case applies almost anywhere except for when your application requires a login screen. I can't see any reason to use synchronous HTTP requests vs asynchronous because of that reason alone. The only time it makes sense is to have a worker thread make your synchronous request, and notify the main thread when the request is done. This will prevent the block. The question then is bench marking your code and seeing which has more overhead, a threaded synchronous request or an asynchronous request.

异步请求的问题是您需要设置智能通知或代理系统,因为您可以在任何给定时间对多个资源发出多个请求。另一个问题是如果我有一个类,说一个单例,它正在处理我的所有数据,我不能在一个getter方法中使用异步请求。意思如下不会去:

The problem with asynchronous requests is you need to either setup a smart notification or delegate system as you can have multiple requests for multiple resources happening at any given time. The other problem with them is if I have a class, say a singleton which is handling all of my data, I can't use asynchronous requests in a getter method. Meaning the following won't go:

 - (NSArray *)users {
     if(users == nil)
        users = do_async_request // NO GOOD

     return users;
 }

,而以下项目:

 - (NSArray *)users {
    if(users == nil)
      users == do_sync_request // OK.

    return users;
 }

您也可能有优先权。我的意思是优先级是,如果你看看苹果的邮件应用程序在iPhone上,你会注意到,他们第一次吸下你的整个POP / IMAP树,然后第二次请求检索您的消息的前2行(默认)。

You also might have priority. What I mean by priority is if you look at Apple's Mail application on the iPhone, you'll notice they first suck down your entire POP/IMAP tree before making a second request to retrieve the first 2 lines (the default) of your message.

我想我的问题给你的专家是这样的。什么时候使用异步,同步,线程 - 什么时候在线程中使用async / sync?您设置什么样的委派系统,以了解异步请求完成时该怎么做?您是否优先处理您的异步请求?

I suppose my question to you experts is this. When are you using asynchronous, synchronous, threads -- and when are you using either async/sync in a thread? What kind of delegation system do you have setup to know what to do when a async request completes? Are you prioritizing your async requests?

对于这个太常见的问题,有多种解决方案。它很简单的东西出来。问题是,我不想破解,我想要一个简单易于维护的东西。

There's a gamut of solutions to this all too common problem. It's simple to hack something out. The problem is, I don't want to hack and I want to have something that's simple and easy to maintain.

推荐答案

不要认为有一个正确的答案。看起来你明白所涉及的妥协,你只需要使你的设计围绕这些。

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.

对于美味我最后使用主题不是。我使所有的网络调用异步,我使用默认的XML解析器(它使用回调工作)。因为它是所有的事件驱动,每个单位是小的,它允许GUI是流畅的,而没有线程的复杂性。

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!

这篇关于在iPhone应用程序中的异步vs同步vs线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆