DataBaseHelper,单例与否? [英] DataBaseHelper, Singleton or not?

查看:78
本文介绍了DataBaseHelper,单例与否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个主题有几个问题,但没有一个给出明确的理由来说明为什么应该或不应该有一个或多个 databaseHelper 实例.什么时候有多个 DatabaseHelper 实例是个好主意,什么时候不是.较低的复杂性(如果确实如此)是否足以成为只有一个实例的充分理由?

There have been a few questions on this topic but none giving firm reasons for why there should or shouldn't be a single or multiple instances of the databaseHelper. When is it a good idea to have multiple instances of DatabaseHelper and when is it not. Is less complexity (if that is actually the case ) a good enough reason to have just a single instance ?

推荐答案

你的 DatabaseHelper 肯定应该是一个单例.每个助手都维护与数据库的单一连接.如果您有多个助手连接到同一个数据库,则会导致并发问题.Sqlite 在单个连接下进行自己的锁定,以确保对数据库的正确并发访问,因此建议并要求对所有数据库操作使用该单个连接(以及该单个帮助程序).

Your DatabaseHelper should be a singleton for sure. Each helper maintains the single connection to the database. If you have multiple helpers with connections to the same database, then concurrency issues will result. Sqlite does it's own locking underneath the single connection to ensure proper concurrent access to the database so using that single connection (and therefore that single helper) for all of your database operations is recommended and required.

这篇关于DataBaseHelper,单例与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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