更重要的是有效的?静态,数据传递,共享preferences,数据库...?的是、更重要、静态、有效

2023-09-12 09:49:28 作者:冷风浮

最近我在做一个应用程序在Android和一些部分我有一个疑问,这是共享数据的有效途径。就像我可以在一个类中声明静态变量,并使用Classname.Variablename调用来自其他类变量或我能通过我的意图的数据,并从其他类获得意图数据或者我可以使用共享preferences和从它那里得到的数据或我连的数据存储在数据库中,并从从数据库中的其它类进行检索。什么我怀疑是这将是最有效的方式做到这一点(获取数据)?由于Android应用程序的最终设计为手机的内存使用和效率应限制。任何人都可以指导我在正确的道路,那将是非常有益的。

I was recently doing an application in android and at some part I got a doubt that which is the efficient way of sharing Data. Like I can declare a static variable in one class and call that variable from the other class using Classname.Variablename or I could pass that data with my intent and get data from intent from the other class or I can use shared preferences and get data from it or I could even store that data in database and retrieve it from the other class from the database. What my doubt was which will be the most efficient way to do that (get data)? Since android applications are ultimately designed for phones memory usage and efficiency should be constraints. Could anyone guide me in the right path, it would be very helpful.

推荐答案

我的观点:

每个一个是最好的取决于具体的方案。让我来解释一下。

Each one is best depends on the scenario. Let me explain.

中的静态变量都给予获得整个应用程序上下文的常用方法。如果你想保持的几DATAS 这是没有必要的,以保持应用程序退出后,这是最好的一个。

Static variable are common ways to give access to whole application context. If you want to maintain few datas which is not necessary to be maintained after the application exited, this is the best one.

这是不是一种数据存储的。这是一种数据共享。我认为这是将数据传递从活动到活动的最佳方式。但维持键的常量的值是一个良好的编程的事情。

This is not a kind of data storing. This is a kind of data sharing. I think this is the best way to pass data from activity to activity. But maintaining the keys for values in Constants is a good programming thing.

共享preferences不过是一个简单的表,有两列。 (键,值)。

Shared Preferences is nothing but a simple table which has two columns. (key, value).

优势:

快速检索 易于理解和程序

缺点:

维护键是困难的,如果我们存储了大量的值。 用户可以随时清除此。

当我们有很多值来存储复杂的结构,我们剩下的只有一个不错的解决方案,即。 DB。

Database:

When we have a lot of values to store with complex structure, we are left with only one great solution, ie. DB.

优点

我们可以保持数据的结构。 在Android有很好的和简单的API来处理sqlite的操作。

缺点

在操作时有点慢比较共享preferences。 用户可以随时清除此。

因此​​,我们发现的主要问题是,我们不能维持任何存储我们使用持久性。一个简单而有效的解决方案是将应用程序每次启动时保持在线数据,并同步其与移动分贝。

So we found the major problem is we can't maintain the persistence whatever storage we use. A simple and effective solution is to maintain data online and sync it with mobile db each time the application is started.