我搜索了很多,我找不到任何答案。
我正在编写一个Xamarin Forms Mobile应用程序,似乎当我最小化应用程序然后重新打开它或者我的一个活动被启动时,会抛出以下异常:
SQLiteConnection.CreateCommand (System.String cmdText, System.Object[] ps)
SQLite.SQLiteException: Cannot create commands from unopened database
SQLiteConnection.CreateCommand (System.String cmdText, System.Object[] ps)
TableQuery`1[T].GenerateCommand (System.String selectionList)
TableQuery`1[T].GetEnumerator ()
System.Collections.Generic.List`1[T]..ctor (System.Collections.Generic.IEnumerable`1[T] collection) [0x00062] in :0
Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source)
AsyncTableQuery`1[T].<ToListAsync>b__9_0 ()
Task`1[TResult].InnerInvoke ()
Task.Execute ()
这是我的代码:
通用存储库(创建Sqlite实例的位置)
public class Repository<T> : IRepository<T> where T : Entity, new()
{
private readonly SQLiteAsyncConnection _db;
public Repository(string dbPath)
{
_db = new SQLiteAsyncConnection(dbPath);
_db.CreateTableAsync<T>().Wait();
}
}
IOC注册
FreshIOC.Container.Register<IRepository<Settings>>(new Repository<Settings>(dbPath)); // FreshIOC is a wrapper around TinyIOC
在我的App.xaml.cs OnResume
中protected override void OnResume()
{
SQLiteAsyncConnection.ResetPool();
}
以上ResetPool
我把它放进去看看它是否有所作为,但事实并非如此。
网址活动
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var url = Intent.Data.ToString();
var split = url.Split(new[] { "ombi://", "_" }, StringSplitOptions.RemoveEmptyEntries);
if (split.Length > 1)
{
var dbLocation = new FileHelper().GetLocalFilePath("ombi.db3");
var repo = new Repository<OmbiMobile.Models.Entities.Settings>(dbLocation);
var settings = repo.Get().Result;
foreach (var s in settings)
{
var i = repo.Delete(s).Result;
}
repo.Save(new Settings
{
AccessToken = split[1],
OmbiUrl = split[0]
});
}
Intent startup = new Intent(this, typeof(MainActivity));
StartActivity(startup);
Finish();
}
我不确定还有什么要做或寻找,我似乎无法找到有关此类错误的任何信息。
更新
经过更多调试后,似乎只在Url活动结束后才会发生。
我已从活动中删除了数据库代码,但它似乎仍然存在。一旦Activity启动了主App()
,然后运行此代码:
var repo = FreshIOC.Container.Resolve<IRepository<Settings>>();
try
{
Task.Run(async () =>
{
settings = (await repo.Get()).FirstOrDefault();
}).Wait();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
发生错误的地方。当调用Get()
调用return _db.Table<T>().ToListAsync();
我尝试过将所有内容设置为异步(没有帮助),使存储库,连接以及我们执行CreateTableAsync
异步并且仍然没有运气。
答案 0 :(得分:7)
您正在进行.Wait()
和.Result
等同步阻塞调用,这些调用在与异步API混合时可能会导致死锁。
SQLiteAsyncConnection
意味着异步使用。
一个常见的工作是创建允许进行异步非阻塞调用的事件处理程序。
例如,在存储库中调用CreateTableAsync
时
public class Repository<T> : IRepository<T> where T : Entity, new() {
private readonly SQLiteAsyncConnection _db;
public Repository(string dbPath) {
_db = new SQLiteAsyncConnection(dbPath);
createTable += onCreateTable; //Subscribe to event
createTable(this, EventArgs.Empty); //Raise event
}
private event EventHandler createTable = delegate { };
private async void onCreateTable(object sender, EventArgs args) {
createTable -= onCreateTable; //Unsubscribe from event
await _db.CreateTableAsync<T>(); //async non blocking call
}
//...
}
存储库抽象似乎有一个异步API但还有同步调用。
这又可能导致死锁,不建议。
如果意图是拥有响应式UI或使用 SQLite.Net (非异步版本)进行同步调用,则代码需要重构为异步。 / p>
将URL活动重构为异步将遵循与上面相同的格式。
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
creating += onCreateCore; //subscribe to event
creating(this, EventArgs.Empty); //raise event
}
private event EventHandler creating = delegate { };
private async void onCreateCore(object sender, EventArgs args) {
creating -= onCreateCore; //unsubscribe to event
var url = Intent.Data.ToString();
var split = url.Split(new[] { "ombi://", "_" }, StringSplitOptions.RemoveEmptyEntries);
if (split.Length > 1) {
var dbLocation = new FileHelper().GetLocalFilePath("ombi.db3");
var repo = new Repository<OmbiMobile.Models.Entities.Settings>(dbLocation);
var settings = await repo.Get();
foreach (var s in settings) {
var i = await repo.Delete(s);
}
repo.Save(new Settings {
AccessToken = split[1],
OmbiUrl = split[0]
});
}
Intent startup = new Intent(this, typeof(MainActivity));
StartActivity(startup);
Finish();
}
同样从设计的角度来看,连接的初始化应该从存储库中反转出来并在外部进行管理(SRP)。
public interface ISQLiteAsyncProvider {
SQLiteAsyncConnection GetConnection();
}
public class DefaultSQLiteAsyncProvider : ISQLiteAsyncProvider {
private readonly Lazy<SQLiteAsyncConnection> connection;
public DefaultSQLiteAsyncProvider(string path) {
connection = new Lazy<SQLiteAsyncConnection>(() => new SQLiteAsyncConnection(path));
}
public SQLiteAsyncConnection GetConnection() {
return connection.Value;
}
}
使用
进行连接的异步延迟初始化/// <summary>
/// Provides support for asynchronous lazy initialization.
/// </summary>
/// <typeparam name="T"></typeparam>
public class LazyAsync<T> : Lazy<Task<T>> {
/// <summary>
/// Initializes a new instance of the LazyAsync`1 class. When lazy initialization
/// occurs, the specified initialization function is used.
/// </summary>
/// <param name="valueFactory">The delegate that is invoked to produce the lazily initialized Task when it is needed.</param>
public LazyAsync(Func<Task<T>> valueFactory) :
base(() => Task.Run(valueFactory)) { }
}
这使得现在可以重构存储库以使用延迟初始化,这允许删除存储库中的事件处理程序
public class Repository<T> : IRepository<T> where T : Entity, new() {
public Repository(ISQLiteAsyncProvider provider) {
this.connection = new LazyAsync<SQLiteAsyncConnection>(await () => {
var db = provider.GetConnection();
await db.CreateTableAsync<T>();
return db;
});
}
private readonly LazyAsync<SQLiteAsyncConnection> connection;
public async Task<List<T>> Get() {
var _db = await connection.Value;
return await _db.Table<T>().ToListAsync();
}
public async Task<T> Get(int id) {
var _db = await connection.Value;
return await _db.Table<T>().Where(x => x.Id == id).FirstOrDefaultAsync();
}
public async Task<int> Save(T entity) {
var _db = await connection.Value;
return entity.Id == 0
? await _db.InsertAsync(entity)
: await_db.UpdateAsync(entity);
}
public async Task<int> Delete(T entity) {
var _db = await connection.Value;
return await _db.DeleteAsync(entity);
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
// get rid of managed resources
}
// get rid of unmanaged resources
}
}
并注册
// same instance should be used for other repositories
var provider = new DefaultSQLiteAsyncProvider(dbPath);
var settingsRepository = new Repository<Settings>(provider);
FreshIOC.Container.Register<IRepository<Settings>>(settingsRepository);
答案 1 :(得分:5)
感谢@Nkosi对他的见解和建议,非常感谢,但没有一个解决方案有效。
在拉下sqlite.net-pcl库(再次由OSS保存!)并通过它进行调试后,似乎每次启动Activity
时都会检查连接是否为{{3并且它不是,它被设置为打开的唯一地方是SqliteConnection
是open时。现在我写它的方式,它是一个单身但愚蠢的Repository<T>
实施IDisposable
。所以我的IOC容器正在处理SqliteConnection
,但由于它是一个单例,所以它永远不会重新创建它。
TL; DR删除了存储库中的IDisposable
实现,因为SqliteConnection
是单例。
答案 2 :(得分:0)
我有同样的错误,但是不是由于Disposable
的实现。由于某些未知原因,如果出现以下情况,它会破裂:
lock (locker)
{
foreach (var item in database.Table<DBItems>()) //It broke on this line
{
//...
}
}
所以我将行更改为
foreach (var item in database.Table<DBItems>().ToList()) //Notice the "ToList()"
问题解决了...