iOS:检查远程文件更新
我有一些 UITableViews 从远程 XML 文件收集内容。有时文件会更新,但视图不会更新,因为我在 viewDidLoad 方法中加载文件。
我假设我应该检查 viewDidAppear 并再次加载我的 XML 文件。但我不希望每次用户单击返回该视图时都会加载它。
最好的方法是什么?我可以以某种方式检查网络上远程文件的更新日期吗?或者我应该将更新日期存储在我的应用程序中,然后每隔一段时间检查一次?必须有一种首选方法来执行此操作,以便我知道 XML 文件已更新。
谢谢。
I have a few UITableViews that gather their content from remote XML files. Sometimes the file is updated, but the view is not updated, because I load the file in my viewDidLoad method.
I am assuming I should do a check in viewDidAppear and load my XML file again. But I don't want it loading every time the user clicks back to that view.
What is the best way to do this? Can I somehow check the updated date of a remote file on the web? Or should I store the updated date in my app, then check at an interval? There must be a preferred way to do this, so that I know that the XML file has been updated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议向开销很小的页面发出 get 请求,仅包含检查版本是否已更改所需的数据。
在 iOS 应用程序上,存储您存储的最后一个版本值,并根据您拉出的这个小页面进行检查。如果页面上的值更大(更新),则提取完整的 XML 文件。
该版本值可以是更新的日期时间或版本号。我目前不在 Mac 上,因此测试 Obj-C 代码并不简单,但这里有一些伪代码:
I suggest making a get request to a page that has a very small overhead with only the data you need to check whether the version has changed.
On the iOS app, store the last version value you stored and check it against this small page you pull. If the value on the page is greater (newer) then pull the full XML file.
This version value could either be the datetime it was updated or the version number. I am not on a Mac at the moment so testing Obj-C code isn't trivial, but here's some pseudocode:
也许您可以在应用程序进入后台时立即加载新数据(在应用程序委托中检测到)。
一个选项:
UITableView
的控制器中引用它。当您的视图控制器调用viewDidAppear
时,只需调用UITableView
的reloadData
即可。Perhaps you could load fresh data right as your application is brought to the background(detected in your app delegate).
an option for this:
UITableView
. When your view controller callsviewDidAppear
, just call yourreloadData
of yourUITableView
.为什么不告诉服务器您的远程(基于客户端)版本的版本号或更新日期,而不是向服务器询问版本号或更新日期。
然后,您的服务器会决定您是否已经拥有最新版本 (A) 还是需要新版本 (B):
Instead of asking the server for a version number or update-date, why not telling the server the version number or update-date of your remote (client-based) version.
Your server then decides if you have a recent version already (A) or if you need a new one (B):
其他答案已经很好地涵盖了“我应该何时重新加载”文件。
另一件需要考虑的事情是使用如今在表格视图应用程序中非常常见的“拉动刷新”隐喻,因为它为用户提供了一种既可以查看数据上次更新时间又可以请求立即更新的方法。
请参阅 iPhone Pull Down Refresh like Tweetie 了解一些使其实现更漂亮的库直接向前。
The other answers have already covered "when should I reload" the file quite well.
One other thing to consider is using the "pull to refresh" metaphor that's very common in tableview apps these days, as it gives users a way to both seen when the data was last updated and to request it's immediately updated.
See iPhone Pull Down Refresh like Tweetie for some libraries that make implementing it pretty straight forward.
您需要从应用程序发送简单的请求(可能通过计时器事件),这将询问服务器有关更新的信息。如果服务器上的内容已更新,那么您需要在应用程序中重新加载内容。
您可以跟踪服务器上的更改时间和应用程序中的最新内容更新时间。
You need to send simple request from application (maybe by timer event), which will ask server about updates. If your content on server was updated then you need to reload your content in application.
You can track changes time on server and latest content update time in application.