简体   繁体   English

获取在线数据并将其存储在Android中的最佳方式

[英]Best way to get online data and store it in Android

I'm getting hung up on how to handle the data for an app I'm designing. 我正忙着处理如何处理我正在设计的应用程序的数据。 I want to pull a list of items from the net. 我想从网上提取一个项目列表。 The data is updated routinely, and I think it would be good to store all the data on the device so the app can load quickly and refresh the data in a background thread rather than have to wait for the network on every start-up. 数据会定期更新,我认为最好将所有数据存储在设备上,以便应用程序可以快速加载并在后台线程中刷新数据,而不必在每次启动时等待网络。

I think I should make the data available in an XML and have a thread parse and save into a SQLite DB, but I'm not sure if that's the "best practice." 我想我应该在XML中提供数据并进行线程解析并保存到SQLite DB中,但我不确定这是否是“最佳实践”。 Are there other ways that people go about handling this? 还有其他方法可以让人们处理这个问题吗?

The cleanest way (or at least, I think it is the cleanest way) is to implement a custom ContentProvider class that interfaces with your server. 最干净的方式(或者至少,我认为这是最干净的方式)是实现与您的服务器接口的自定义ContentProvider类。 You can query the contenprovider and if it doesn't have the data in a local cache (for example a SQLite db as you said) it downloads it from your server and adds it to the local data. 您可以查询contenprovider,如果它没有本地缓存​​中的数据(例如您所说的SQLite数据库),它会从您的服务器下载它并将其添加到本地数据。 Why a content provider? 为什么是内容提供商? because then you can easily access your data across apps and you have a nice and clean way to get your data when using intents. 因为这样您就可以轻松地跨应用程序访问您的数据,并且您在使用意图时可以通过一种干净的方式获取数据。 Also, I personally prefer not to download data when the app is not running, because it will cost battery life while the user does not actively requests the data. 此外,我个人不希望在应用程序未运行时下载数据,因为当用户不主动请求数据时,它将耗费电池寿命。

Sounds reasonable. 听起来很合理。

If the download of the new data takes more than some seconds, you might want to use a Service . 如果下载新数据的时间超过几秒钟,您可能需要使用服务 This will allow you to continue the update even when the user has left your app. 这样,即使用户离开您的应用,您也可以继续更新。

Also, think about how you will notify the user that something is going on. 另外,请考虑如何通知用户正在发生的事情。 Displaying some progress indicator is always a good idea. 显示一些进度指示器总是一个好主意。 Otherwise, users might just think the data is not up to date because the app is broken. 否则,用户可能只是认为数据不是最新的,因为应用程序已损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM