开发者

When accessing ormlite repository in multple activities should there be multiple instances, a singleton, or what?

开发者 https://www.devze.com 2023-04-01 03:14 出处:网络
I am using the sample provided by James Morgan\'s DemoORMLiteAndroid which has one activity that instantiates a repository.

I am using the sample provided by James Morgan's DemoORMLiteAndroid which has one activity that instantiates a repository.

for reference

public class Repository {
    private Dao<Room, Integer> roomDao;

    public Repository(final DatabaseHelper databaseHelper) {
        this.roomDao = getRoomDao(databaseHelper);
        ...

and in Activity

public class RoomActivity extends OrmLiteBaseListActivity<DatabaseHelper> {
    private Repository repository;
    @Override
    public void onCreate(final Bundle savedInstanceSt开发者_如何转开发ate) {
        super.onCreate(savedInstanceState);
        this.repository = new Repository(getHelper());
    }
        ...
        this.repository.clearData();
        ...etc..

How should the repository be accessed in other activities or classes?


I'm not sure this a great answer @Kevin but here it goes.

ORMLite has a couple of base classes which help with the bootstrapping of the Android databases.

  • OrmLiteBaseActivity
  • OrmLiteBaseActivityGroup
  • OrmLiteBaseListActivity
  • OrmLiteBaseService
  • OrmLiteBaseTabActivity

Here are the Javadocs for them: http://ormlite.com/javadoc/ormlite-android/

All these base classes do is provide utility methods which help manage a DatabaseHelper class which extends OrmLiteSqliteOpenHelper. You only want one instance of the helper class since it manages the connection to the database which gets passed in with the onCreate() method.

The onCreate() method is what gets passed the Android SQLiteDatabase associated with the application which is needed for ORMLite to wrap inside its database connection code.

If you ask more specifically what you are trying to accomplish I'll edit my response to include more information.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号