简体   繁体   English

应用变量的效率?

[英]efficiency of application variable?

I have a database from where i need to extract quite a lot of data. 我有一个数据库,我需要从中提取很多数据。 Now i get that data when required, ie I have made a class that handles database interactions and whenever an activity requires data it will call that class for the data. 现在,我可以在需要时获取该数据,即,我已经创建了一个用于处理数据库交互的类,只要活动需要数据,它将为该数据调用该类。 So at a time an Activity only has the bare minimum amount of data in memory (ie the data that it is using). 因此,一个活动一次只能在内存中保留最少的数据量(即它正在使用的数据)。 But everytime i change an activity i have to perform database access to fetch data for the new activity. 但是每次我更改活动时,都必须执行数据库访问以获取新活动的数据。

Method 2 方法2

As opposed to this i have this other alternative, in which i make an application object and then perform database access in the beginning and then store all the data that i would require (in all the activities) in the application object. 与此相对,我有另一个选择,在该选择中,我创建了一个应用程序对象,然后从头开始执行数据库访问,然后将我需要(在所有活动中)的所有数据存储在该应用程序对象中。 Whenever i need the data, i refer to the application object. 每当我需要数据时,我都会引用应用程序对象。 The downside of this that i will be holding too much extra data that i am not using at a given instant. 不利的一面是,在给定的瞬间,我将拥有太多未使用的额外数据。

Which of the above 2 approaches is better? 以上两种方法中哪一种更好?

Thank you in advance. 先感谢您。

It depends on your requirements and their priorities. 这取决于您的要求及其优先级。 If the time required for solution 2 is too long for you to accept then optimize (eg by using method 2, but in general I would advise against storing potentially all of your database in memory ... assuming the amount of memory will suffice). 如果解决方案2所需的时间太长而无法接受,那么可以进行优化(例如,使用方法2,但一般而言,我建议不要将所有数据库都潜在地存储在内存中……假设内存量足够了)。

Did you try solution 1. If the problem is only to read the data from database, it should not take too long to load the data for one activity. 您是否尝试过解决方案1.如果问题仅在于从数据库读取数据,则为一个活动加载数据应该不会花费太长时间。 If complex calculations are involved you might be pressed to optimize. 如果涉及复杂的计算,则可能需要进行优化。 But don't optimize just in case! 但是请不要进行优化,以防万一!

我之所以喜欢第一种方法,是因为除非多个应用程序都可以访问数据库,否则调用数据库并不昂贵。

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

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