iPhone 网络性能

发布于 2024-07-25 08:31:38 字数 832 浏览 7 评论 0原文

我有一个问题,并且非常乐意接受建议(甚至是非常奇怪的建议!)

我正在编写一个 iPhone 应用程序,它向服务器发出请求(带有参数的 URL)。 iPhone 收到 XML 作为响应。 一切都很好。

现在,我希望通过测量执行某些任务所需的时间来提高应用程序的速度。 我发现,在执行的所有任务(下载、XML 解析、发送请求、处理请求、从 XML 解析对象)中,下载实际的 XML 花费的时间最长。

现在,我的 XML 文件非常非常简单,而且非常非常小。 我主要使用它们来读取类似 RSS 的数据并在 UITableView 中显示它们。

我的应用程序运行得很好,没有什么感觉真的很慢,但现在应用程序商店中有一个应用程序,它的功能与我的应用程序非常相似,但速度更快,感觉更“敏捷”,如果你知道的话我是说。 它还具有从 RSS 源中一一加载标题的强大功能。

目前,我正在尝试对数据进行 gzip 压缩,但压缩只会使数据大小减少一半,而且似乎对性能没有任何真正的好处。 最主要的是,在解析数据之前必须下载数据。 如果有一个数据“流”,它在传入时就被解析,那将是非常酷的。这样,我几乎可以同时做两项工作,并逐一加载标题(使用户交互性更具吸引力)。

有人知道如何提高我的表现吗? 无论是通过出色的压缩技巧还是通过完全不同的方式与服务器通信......欢迎一切!

更新:将服务器的延迟和响应能力放在一边; 我如何才能将 XML 源“传输”到我的 iPhone(逐字节下载)并同时进行解析? 现在是一个线性的下载过程-> 解析-> 显示,但它可以通过下载和下载变成半并行。 同时解析(并在下载完成时显示每个项目,而不是在 UITableView 中同时加载所有项目)

I have a question and I am very open to suggestions (even very odd ones!)

I am writing an iPhone app, which does a request (URL with parameters) to a server. As a response, the iPhone receives XML. All is well.

Right now, I am looking to improve my application's speed by measuring the time it takes to perform certain tasks. I've found that, of all the tasks performed (downloading, XML Parsing, sending the request, handeling the request, parsing objects from XML), downloading the actual XML takes the longest.

Now, my XML files are very, very, very easy and very, very, very small. I use them primarily to read RSS-like data and show them in a UITableView.

My app works very well, and there is nothing that feels really slow, but there is one application in the App Store right now which does something very similar to my application, but is way faster and feels more 'snappy', if you know what I mean. It also has the great feature to load the headlines one by one from the RSS-feed.

Currently I'm experimenting with gzip compression of my data, but the compression only makes my data half the size and it doesn't seem to do any real good for performance. The main thing is, that the data has to be downloaded, before it gets parsed. It would be very cool to have a 'stream' of data, which is parsed as it comes in. That way, I can do two jobs almost simultaneous and load headlines one by one (making user interactivity more attractive).

Anyone has an idea of how to improve my performance? Either by great compression tips or entirely different ways to communicate with the server.. All is welcome!

UPDATE: putting the latency and responsiveness of the server aside; how could I get a source of XML to be 'streamed' to my iPhone (downloaded byte for byte) and at the same time get parsed? Right now it is a linear process of downloading -> parsing -> showing, but it could become semi-parallel by downloading & parsing at the same time (and show each item when it is done downloading, instead of loading it all at the same time in a UITableView)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情释 2024-08-01 08:31:38

假设您正在使用 NSXMLParser 的 initWithContentsOfURL:,这可能是问题的一部分。 看起来像是下载了 URL 的全部内容,然后将其交给解析器一次性解析所有内容。

尽管 NSXMLParser 是一个事件驱动的解析器,但它似乎不支持以增量方式将数据流式传输到解析器。 当然,您可以将 NSXMLParser 替换为其他一些解析库,以更合理的方式处理增量数据。

另一种方法是使用 NSURLConnection,并在 NSURLConnection 委托的 connection:didReceiveData: 方法中创建一个新的 NSXMLParser 并在每次传入某些数据时重新解析数据。 您必须编写一些额外的代码来忽略多次重新解析文件开头而产生的额外事件。

这看起来比仅仅获取其他库并对其进行调整要更多的工作,但也许不是,这取决于您如何处理表数据的下游创建。

Assuming you're using NSXMLParser's initWithContentsOfURL:, that's probably part of the problem. It seems like that downloads the entire contents of the URL, then hands it off to the parser to parse all at once.

Despite the fact that the NSXMLParser is an event-driven parser, it doesn't seem to support streaming data to the parser in an incremental manner. You could, of course, replace NSXMLParser with some other parsing library that handles incremental data in a more sensible way.

An alternative would be to use NSURLConnection, and create a new NSXMLParser and re-parse the data each time some data comes in, in the connection:didReceiveData: method of your NSURLConnection's delegate. You''d have to write some extra code to ignore the extra events from re-parsing the beginning of the file more than once.

This seems like it'd be more work than just grabbing some other library and adapting it, but maybe not, depending on how you're handling the downstream creation of your table data.

酒儿 2024-08-01 08:31:38

如果 NSMutableArray 是 UITableView 的底层数据结构,你应该尝试
使用 initWithContentsOfURL。 服务器上的格式需要采用易于生成的 apples“plist”xml 格式。 我猜如果可可已经获得了资源
处理 xml 时,使用它们会比创建自己的 xml 解析器实例更快。

If NSMutableArray is the underlying data structure of your UITableView you should try
using initWithContentsOfURL. The format on the server needs to be in apples "plist" xml which is easy to generate. I'm guessing that if cocoa already has resources acquired for
processing xml it would be quicker to use them instead of creating your own xml parser instance.

打小就很酷 2024-08-01 08:31:38

您如何解析 XML 数据。 在收到 XML 时对其进行解析的需求是拉式解析被发明的全部原因,并且他们有 NSXMLParser 事件驱动解析器,该解析器对数据流进行操作...

这也意味着数据压缩会适得其反。

对于非常少量的 XML,速度问题可能与连接延迟有关。

因此,另一个重要因素可能是 DNS 查找。 您可以事先自己执行一次并缓存 IP,也许仅在无法连接到服务器时重新检查...

How are you parsing the XML data. The need to parse XML as you receive it is the whole reason pull parsing was invented, and they have the NSXMLParser event driven parser that operates on a stream of data...

That also means compression of the data is counterproductive.

For very small amounts of XML, the issue of speed is probably about latency of connection.

Thus, other big factor could be the DNS lookup. You could do that yourself once beforehand and cache the IP, perhaps rechecking only on failure to connect to your server...

白芷 2024-08-01 08:31:38

由于您的 xml 文件非常小(我想只有 8k 左右?)我认为尝试解析它们时不会看到很大的性能提升。您可以使用异步 NSURLConnection 来做到这一点,但我看不出它对小文件有多大帮助。 您是否考虑过在服务器上生成 XML 文件所需的时间? 您是使用 PHP 创建 XML,还是访问静态文件? 出于测试目的,访问静态文件并查看比较结果可能会很有趣。

不久前我遇到了这样的问题,结果发现我的服务器上的 mySQL 数据库速度很慢,而且它确实与我的应用程序无关。

此外,还可以使用 Instruments 查看下载和解析 XML 文档时分配的内存量。 一些 XML 解析器将整个 XML 文档加载到内存中,然后允许您随机索引文档的元素,而其他解析器仅提供逐步解析。 对于像 iPhone 这样的有限设备,分步方法要好得多,而且您可能正在使用它,但快速查看一下内存消耗情况应该会告诉您答案。

Since your xml files are very small, (just 8k or so, I imagine?) I don't think you'd see a big performance boost trying to parse them as they come in. You can use an asynchronous NSURLConnection to do that, but I can't see it helping with a small file very much. Have you considered the time it takes to generate the XML file on the server? Are you using PHP to create the XML, or accessing a static file? For testing purposes, it might be interesting to access static files and see how that compares.

I ran into a problem like this a while ago, and it turned out the mySQL database on my server was being slow, and it really had nothing to do with my app.

Also, use Instruments to look at the amount of memory that is allocated while you're downloading and parsing an XML document. Some XML parsers load the entire XML document into memory and then allow you to index randomly into the document's elements, while others provide step by step parsing only. The step-by-step method is much better for limited devices like the iPhone and it's probably what you're using, but a quick look at memory consumption should tell you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文