文件或数据库? - 最佳实践,以节省的Andr​​oid设备对象 [英] File or Database? - Best Practice to save Objects on Android-Device

查看:94
本文介绍了文件或数据库? - 最佳实践,以节省的Andr​​oid设备对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立在Java的Andr​​oid的应用程序,我这样定义用户或播放列表等。

I'm building an android application in java where I define some Objects like "user" or "playlist" etc..

如何保存这些自定义对象的设备以后访问呢?

How to save these self-defined objects on the device for later access?

    Gson gson = new Gson();
    String json = gson.toJson(user);

我可以分析通过GSON对象到的JSONObject或JSONArray。现在,我有两个选择,保存字符串:在一个数据库或文件。我知道如何使用Android的数据库类和FileWriter的/读取器类,但什么是与性能有关,交通方便,尤其是简单?

I can parse the objects via GSON to a JSONObject or a JSONArray. Now I have two options to save the strings: In a Database or a File. I know how to use the android database-classes and the filewriter/reader classes, but what is the best practice with regards to performance, accessibility and especially to simplicity?

推荐答案

这真的取决于你的使用情况

如果您的应用程序数据的不会发生很大的变化(想了会议应用程序,使用一次或每年两次),那么你可以使用一个ContentProvider的+ CursorAdapters这将使您的生活更轻松。

If your application data doesn't change a lot (think a conference app that is used once or twice a year), then you can use a ContentProvider + CursorAdapters which will make your life easier.

如果您的应用程序数据的变化很大(认为Gmail中,其中多达上百个邮件,可弹出每一天,甚至一个新闻源,或类似Google+的社交网络应用程序),然后数据库很可能使你的生活变得更糟糕。你在这种情况下应该使用第三方的缓存系统,像谷歌排球或RoboSpice,等它处理JSON对象的所有缓存你和所有出现的并发性问题,等等。

If your application data changes a lot (think Gmail, where as much as hundreds of email can pop-up every day, or a news feed, or a social networking app like Google+), then a database could probably make your life worse. You should use in this case a 3rd party caching system, like Google Volley or RoboSpice, etc. which handles all the caching of JSON objects for you and all the concurrency problems that appear, etc.

这篇关于文件或数据库? - 最佳实践,以节省的Andr​​oid设备对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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