从任何地方高效访问全球数据的解决方案

发布于 2024-12-18 09:03:50 字数 1908 浏览 0 评论 0原文

我需要找到一种解决方案来保存和访问大量复杂的全局数据和方法。它必须可以从活动和各种数据类的正常实例变量中访问。

我就是这样做的。我只是想知道它是否有什么问题或者是否有更好/更干净的方法。

首先,我多次像推荐的那样扩展 Application ...

public class MainDataManager extends Application{

   public ... large chunks of data in arrays, lists, sets,....

   //static variable for singleton access from within instance variables of other classes
   public static MainDataManager mainDataManager;

  //create and init the global data, and store it in the static variable of the class
  @Override
      public void onCreate() {
        super.onCreate();
        //in case it should get called more than once for any reason
        if (mainDataManager == null) {
            init();
            mainDataManager = this;
            }
      }

现在从活动中访问它,就像推荐的任何地方一样...

MainDataManager mainDataManager = (MainDataManager)getApplicationContext();

并且因为我需要从数据类的普通实例访问它...

public class MyDataClass {
    public MainDataManager mainDataManager;
    public String name;

    public MyDataClass(String namex) {
        this.name = namex;
        //this is why I defined the static variable within MainDataManager, so
        //one has access to it from within the instance of MyDataClass
        this.mainDataManager = MainDataManager.mainDataManager;
    }

      public void examplesForAccessing() {
      //some examples on how to access the global data structure and associated methods
        mainDataManager.someMethodAccess();
        xyz = mainDataManager.someDataAccess;
        mainDataManager.someIndirectMethodAccess.clear();
        mainDataManager.someOtherData = false;
    }
}

因为我没有到目前为止,我想知道这是否有什么问题。内存、效率……

非常感谢!

我可以添加一点旁注吗? 我也可以只使用一个类 MainDataClass 并通过 MainDataClass.var 或 MainDataClass.method() 访问。 有什么真正的缺点吗?

这两种情况下的数据都保存在堆/堆栈中吗?

I need to find a solution that holds and accesses large chunks of complex global data and methods. It has to be accessible from within activities and normal instance variables of various data classes.

This is how I have done it. I would just like to know if there is anything wrong with it or if there is a better/cleaner way.

First I extend Application like recommended many times...

public class MainDataManager extends Application{

   public ... large chunks of data in arrays, lists, sets,....

   //static variable for singleton access from within instance variables of other classes
   public static MainDataManager mainDataManager;

  //create and init the global data, and store it in the static variable of the class
  @Override
      public void onCreate() {
        super.onCreate();
        //in case it should get called more than once for any reason
        if (mainDataManager == null) {
            init();
            mainDataManager = this;
            }
      }

Now accessing it from within activities like everywhere recommended...

MainDataManager mainDataManager = (MainDataManager)getApplicationContext();

And since I need to access it from normal instances of data classes ...

public class MyDataClass {
    public MainDataManager mainDataManager;
    public String name;

    public MyDataClass(String namex) {
        this.name = namex;
        //this is why I defined the static variable within MainDataManager, so
        //one has access to it from within the instance of MyDataClass
        this.mainDataManager = MainDataManager.mainDataManager;
    }

      public void examplesForAccessing() {
      //some examples on how to access the global data structure and associated methods
        mainDataManager.someMethodAccess();
        xyz = mainDataManager.someDataAccess;
        mainDataManager.someIndirectMethodAccess.clear();
        mainDataManager.someOtherData = false;
    }
}

Since I have not done this so far, I would like to know if there is anything wrong with this. Memory, efficiency, ...

Thanks very much!

May I add a little sidenote?
I could also have just used a class MainDataClass and access by MainDataClass.var or MainDataClass.method(). Is there any REAL disadvantage?

Is the data in both cases held in heap/stack?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-12-25 09:03:50

您没有提供有关“大数据块”的详细信息,但请记住,onCreate 方法是应用程序启动时运行的第一个方法,并且它在主/UI 线程上运行。这意味着,如果您在 init() 方法中执行长时间任务,您的用户体验将会很差,更不用说您面临 ANR 异常的风险。

解决方案很简单:

  1. 保持 onCreate 较短
  2. 创建一个 BG 线程并使用它来运行所有初始化代码
  3. 在 BG 线程运行时显示带有适当进度条的“Splash”/“Welcome”屏幕。

You haven't given much detail about your "large chunks of data" but keep in mind that the onCreate method is the first things that runs when your application is starting and it runs on the main/UI thread. This means that if you do long tasks in your init() method your UX will be poor, not to mention that you are risking an ANR exception.

The solution for that is simple:

  1. Keep your onCreate short
  2. Create a BG thread and use it to run all initialization code
  3. Show a "Splash"/"Welcome" screen with the a proper progressbar while the BG thread is running.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文