在不同的类主场迎战同一个班级,这是更好的SQLite数据库? Android版 [英] SQLite database in separate class vs. in same class, which is better? Android

查看:143
本文介绍了在不同的类主场迎战同一个班级,这是更好的SQLite数据库? Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQLite数据库是从扩展活动的主类单独的类​​。

我注意到,有建立数据库的两种方式。一种方式是把它的主要活动类中,无论是在课堂上或作为一个嵌套的子类。第二个办法是把它放在单独的类。

在单独的类看起来更好,但是有一个缺点。您可以在主活动类,你想要做的事,每次创建它的一个实例。我读了Android中实例化对象是昂贵的,应当避免。

尽管这样,我宁愿让数据库作为一个单独的类。是实例化对象不够,它使得并将该数据库在同一类更好的交易的成本是多少?

例如,对于SQLite数据库单独的类的:不完全psudo- code

 公共类SQLiteDB {
   私有静态类DbHelper扩展SQLiteOpenHelper {
      // DB辅助方法
   }为DB,如获取,设置,以及其他//方法
公共无效的openDatabase(){}
公共无效closeDatabse(){}
公共无效insertRecord(字符串记录){}
}

例如在主要活动中使用:incompete psudo- code

 公共类主要活动扩展{//我想每一次使用它,我必须实例化一个对象数据库类//创建SQLiteDB对象的许多情况下,垃圾收集器努力工作SQLiteDB mDatabase =新SQLiteDB();的openDatabase();insertRecord(插入此);closeDatabase();}


解决方案

  在不同的类主场迎战同一个班级,这是更好的

SQLite数据库?


这是非常COM prehensive问题,这取决于更多的因素(应用类型,个人需求,你会如何处理与数据库等)。有人可以preFER放置数据库内部类,有人为分隔的类。问题是,许多开发商正试图大棒尽可能多code地为一类,也许他们怕创造多一点的类。我不知道确切。我提到,只为我个人的音符。

但是,让我们回到你的问题。什么是好?

我觉得跟separeted类的方法。你应该让你的唯一活动课活动课> 仅用于创建和处理UI。应用程序的外观应该从应用程序逻辑中分离出来。如果你按照这个规定的code会变得更加干净和人类可读的(如果别人会看你的code,他不应该完全丢失)。这不是一个耻辱,有20个纯编写的类来都的东西,在一个类粘(如猪)。


  

但有一个缺点。你必须创建一个实例
  它在你想要做的事,每次的主要活动类。一世
  读了Android中实例化对象是昂贵的,应该是
  避免。


你想想 辛格尔顿的使用 ?这种设计模式是值得考虑一下。你将永远有一个有很多好处只有一个实例e.q.不浪费内存。我只有用辛格尔顿好的经验。因此,我建议你去尝试并使用它。

示例:

 私有静态SQLiteOpenHelper实例;公共静态SQLiteOpenHelper的getInstance(上下文mContext){
   如果(例如== NULL){
      例如=新SQLiteOpenHelperImplementation(mContext);
   }
   返回实例;
}

和结束时我给你几个建议:


  • 每当你使用游标的工作,数据库等发布/关闭
    他们后立即工作就完成了。这可以解决很多异常
    有关 SQLiteDatabase 光标


  • synchronized块和方法的用法是pretty好的做法
    在并发编程的情况下,以避免很多问题。


  • 如果您在数据库中有一个以上的表,我建议你创建
    服务类的每个表,将包装 CRUD 操作和具体
    该表的方法


  • 活动被破坏之前,检查并释放哪些不是所有来源
    已经发布。


I have an SQLite database that is in a separate class from the main class that extends Activity.

I noticed that there are two ways of setting up the database. one way is to put it inside the main Activity class, either in the class or as a nested sub class. the second way is to put it in the separate class.

the separate class looks better, however there is one disadvantage. You have to create an instance of it in the main activity class every time you want to do something. I read that instantiating objects in Android is expensive and should be avoided.

despite this, I would rather make the database as a separate class. Is the cost of instantiating objects enough that it makes putting the database in the same class a better deal?

example of separate class for SQLite database: incomplete psudo-code

public class SQLiteDB {
   private static class DbHelper extends SQLiteOpenHelper{
      // db helper methods
   }

// methods for DB, like get, set, and others
public void openDatabase(){  }
public void closeDatabse(){  }
public void insertRecord(String record){  }
}

example use in main Activity: incompete psudo-code

public class Main extends Activity{

// every time I want to use it I must instantiate an object for the database class

// many instances of SQLiteDB object created, garbage collector works hard

SQLiteDB mDatabase = new SQLiteDB();

openDatabase();

insertRecord("insert this");

closeDatabase();

}

解决方案

SQLite database in separate class vs. in same class, which is better?

This is very comprehensive question and it depends on more factors(type of application, personal requirements, how you'll deal with db etc.). Somebody can prefer to place database as inner class and someone as separated class. Problem is that many developers are trying to "stick" as much code as possible into one class and maybe they "fear" to create a little more classes. I don't know that exactly. I mentioned that only as my personal note.

But let's back to your question. What is better?

I think that approach with separeted class. You should let your Activity classes only "Activity classes" > only for creating and dealing with UI. Application appearance should be separated from application logic. If you'll follow this "rule" your code will become more clean and human-readable(if someone else will look at your code he shouldn't be completely lost). It's not a shame to have 20 purely written classes as to have all stuff sticked in one class(like a pig).

however there is one disadvantage. You have to create an instance of it in the main activity class every time you want to do something. I read that instantiating objects in Android is expensive and should be avoided.

Did you think about an usage of Singleton? This design pattern is worth to think about it. You will always have only one instance that have many benefits e.q. no waste of memory. I have only good experiences with Singleton. Therefore i recommend you to try and use it.

Example:

private static SQLiteOpenHelper instance;

public static SQLiteOpenHelper getInstance(Context mContext) {
   if (instance == null) {
      instance = new SQLiteOpenHelperImplementation(mContext);
   }
   return instance;
}

And at the end i give you a few suggestions:

  • Everytime you'll work with cursors, databases etc. release / close them immediately after work is done. This can solve many exceptions related to SQLiteDatabase and Cursor

  • An usage of synchronized blocks and methods is pretty good practise in the case of concurrent programming to avoid many problems

  • If you have more than one table in database i suggest you create "serving" class for each table that will wrap CRUD operations and specific methods of the table

  • Before Activity is destroyed, check and release all sources which are not already released.

这篇关于在不同的类主场迎战同一个班级,这是更好的SQLite数据库? Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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