Android的对象处理/持久 [英] Android object handling / persistence

查看:159
本文介绍了Android的对象处理/持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty远到我的第一个Android应用程序,和我有心想准是我做错了。我的应用程序会谈到Ruby on Rails的服务器和对象序列化来回通过XML。之前,我知道发生了什么事,我发现自己膝盖深在写我自己蹩脚的ORM,这是事实,我没有从高中写任何Java加剧的问题。

I am pretty far into my first Android application, and I have the sneaking suspicion that I'm "Doing It Wrong". My app talks to a Ruby on Rails server and serializes objects back and forth via XML. Before I knew what was happening, I found myself knee deep in writing my own crappy ORM, a problem which is compounded by the fact that I haven't written any Java since high school.

我在这里的冲突是,我希望我的客户端(安卓)应用能够通过各种方法,如HTTP / XML序列化的,到本地数据库,或到本地文件系统。我开始了策略模式,但我觉得我的解决方案是严重缺乏。

My conflict here is that I want my client-side (android) app to be capable of serializing via a variety of methods, such as HTTP/XML, to a local database, or out to the local filesystem. I started out with the Strategy pattern, but I feel like my solution is badly lacking.

有关的,我应该重新实现所有客户端上的Rails的模型验证,因为我不知道如果我总是会被使用Rails在另一侧做的工作?该甚至更大的问题是,现在我只能重新present平面物体键值,因为我的code不能处理像一个真正的ORM嵌套对象。

For one, should I re-implement all of Rails model validation on the client side, because I don't know if I'm always going to be working with Rails on the other side? The even bigger issue is that right now I can only represent flat objects as key-values, as my code can't handle nested objects like a true ORM.

我敢肯定的Andr​​oid开发者处理这一切的时候,所以我很感兴趣地听到别人这样做,以应对这些问题。

I'm sure Android devs deal with this all the time, so I'm interested to hear what other people do to cope with these issues.

推荐答案

我不会接近你的Andr​​oid应用程序作为Ruby应用程序的扩展 - 而一个一个API消费国。如果你可以尝试使您的服务器应用程序的JSON(或其他格式,但JSON是最轻量级),并从你的Andr​​oid应用程序消耗这些API,你很可能有少的问题作为JSON已经以K / V格式。

I wouldn't approach your Android application as an extension of a Ruby app - rather a consumer of an API. If you can try to expose your server application as JSON (or other format, but JSON is the most lightweight) and consume these APIs from your Android application you would most likely have less problems as JSON is already in K/V format.

我还没有书面的Andr​​oid对象SQLite的,但我已经写他们既是Parcelable对象和共享preferences。这两种策略都满足小到中档的应用程序。对于数据密集型的应用程序,很明显,你将不得不采取这一步对于SQLite。

I have not yet written Android objects to SQLite yet, but I have written them as both Parcelable objects and to the SharedPreferences. Both of these strategies are sufficient for small to mid-range apps. For data intensive apps, obviously you will have to take it a step further to SQLite.

有对这些方法的一些伟大的文章:管理国家

There are some great articles for these approaches: Managing State.

它归结为,可以很容易被序列化方式设计你的对象。这意味着没有循环引用或极复杂的对象。这不应该是一个非常大的问题,特别是如果你的数据是JSON格式了。你只需要扩展一些类,并添加返回Parcelable对象重新presentation或字符串重新presentation让你的对象可以保存这样的功能。

It boils down to designing your objects in a way that can be serialized easily. That means no circular references or extremely complex objects. This shouldn't be a very large problem, especially if your data is in JSON format already. You simply need to extend some classes and add functions that return a Parcelable object representation or a string representation so your objects can be saved thus.

我将避免克隆你的服务器端对象和验证在Android作为它,然后修改需要这两个来源,如果你做小的变化。服务器应该处理所有的数据和验证,你应该仅仅是请求,高速缓存和在Android发送数据。

I would avoid cloning your server-side objects and validation in Android as it then requires modifying both sources if you make small changes. The server should handle all data and validation and you should simply be requesting, caching, and sending data from Android.

我很有兴趣听到的话,有没有对象写入任何SQLite的挑战,但我想它不是从我上面列出的细节步骤的多。希望这有助于在一些容量!

I'd be interested to hear if there any challenges to writing objects to SQLite, but I imagine it's not that much more of step from the details I've outlined above. Hope this helps in some capacity!

这篇关于Android的对象处理/持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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