TBXML 中的异步性和 initWithURL
我计划将 TBXML 用于我的天气应用程序。到目前为止,我已经将 NSXMLParser 与 NSURLConnection 一起使用,但我觉得这一定能够更简单、更快、更稳定(当前的代码非常复杂,小问题不断出现,调试起来很困难)。
我一直在寻找异步解析器,最终找到了一篇文章(“如何为您的 iPhone 项目选择最佳的 xml 解析器”,位于 raywenderlich) 这将我引向了 TBXML。阅读文档后,我发现您还可以使用 URL 来初始化它,这样就不需要使用 NSURLConnection 了。我的第一个问题是:与此相比,使用 NSURLRequest 有什么优点?在启动 TBXML 之前,我将首先检查网络连接是否正常,这样就不会出现问题。
我的第二个问题是关于异步性:如果我只是调用 initWithURL 或 initWithData,它的处理会是异步的吗?据我所知,它没有委托调用,而是直接从 TBXML 对象获取元素。如果我尝试在完成之前从中获取元素,它会简单地挂在那里吗?因为如果您使用 initWithURL (这几乎可以回答我的第一个问题),这可以持续特别长的时间。如果是这样,TBXML 在另一个线程上运行是否安全(我假设是)?
I'm planning to use TBXML for my Weather app. I've used NSXMLParser with a NSURLConnection up to now, but i feel this must be able much simpler, faster and more stable (the current code is so complex minor issues keep slipping in that are hell to debug).
I've been searching for asynchronous parsers for a bit and ended up in an article ('How to choose the best xml parser for your iPhone project' on raywenderlich) which lead me to TBXML. Reading through the documentation, i saw that you can also init it with an URL, which would take away the need for using the NSURLConnection. My first question is this: What are the advantages of using NSURLRequest over this? I am going to check if the network connection is alright first before i start TBXML, so that won't be a problem.
My second question is about the asynchronousity: If i simply call initWithURL or initWithData, will its processing be asynchronous? From what i've seen, it has no delegate calls but instead you just directly get the elements from the TBXML object. If i try getting elements from it before it's done, will it simply hang there? Since this can last especially long if you use initWithURL (which would pretty much answer my first question) If so, is TBXML safe to run on another thread (which i assume it is)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想异步解析它,你确实需要一个辅助线程、套接字和类似的东西可以在运行循环上工作,因为它们只需要每隔一段时间检查一次是否有数据可供读取。解析器确实需要使用一些处理 powah..
只需使用中央调度,解析您的数据并使用它。我无法告诉你它的线程是否安全,但如果它没有说它不安全,那么它很可能是安全的。
If you want to parse it asynchronously you really need a secondary thread, sockets and things like that can work on the runloop because they only have to check every once in a while whether there is data available to read. A parser really needs to use some processing powah..
Just use grand central dispatch, parse your data and use it. I can't tell you whether its threading safe but if it does not say its unsafe then its most likely safe anyways..
http://www.tbxml.co.uk/TBXML/TBXML_Free.html
点击链接即可得到答案。
http://www.tbxml.co.uk/TBXML/TBXML_Free.html
follow the link and get your answer.