Hello,
I've run into an issue with sqlite-net-pcl v1.4 It's only occuring on iOS, not on Android.
This project is a .net standard 2 project, in a MVVM methods (mostly).
What I'm running into is that I've got a ViewModel that has two repositories. Each repository has a reference to the common database library. At finalization time, it appears that the 1st repository is garbage collected - (the database library is disposed first before each repository), somehow the SQLite connection is closed and I'm not sure why.
None of my code - common or iOS specific actually does a close. At the finalize of the second repository, this message shows up:
2018-10-09 18:25:39.413082-0400 Project.iOS[5915:371800] [logging] API call with invalid database connection pointer
2018-10-09 18:25:39.413239-0400 Project.iOS[5915:371800] [logging] misuse at line 154262 of [95fbac39ba]
Since this doesn't show up at every finalize occurrence and since it's disposing of the objects, it could be ignored - but I'd like to fix it.
I didn't see much out there excepting this, but that's more of a problem with the open side of things, not why it's auto closing the connection.
This isn't happening on iOS when the ViewModel only has one repository on it.
Has anyone run into this, if so, how did you fix it?
The iOS common code:
public class IOSSQLite : ISQLite
{
public SQLiteConnection GetConnection()
{
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder
string libraryPath = Path.Combine(documentsPath, "..", "Library"); // Library folder
var path = Path.Combine(libraryPath, DatabaseHelper.DbFileName);
var conn = new SQLiteConnection(path);
// Return the database connection
return conn;
}
}
Common code:
public class GetMyDBConnection
{
SQLiteConnection _sqliteconnection;
public SQLiteConnection Connection
{
get
{
return _sqliteconnection;
}
}
public GetMyDBConnection()
{
_sqliteconnection = Xamarin.Forms.DependencyService.Get<ISQLite>().GetConnection();
System.Diagnostics.Debug.WriteLine("GetMyDBConnection() pointer addr: " + _sqliteconnection.Handle.ptr.ToString());
System.Diagnostics.Debug.Flush();
}
}
public class ViewModelList : ViewModelBase
{
public ICommand FinishCommand { get; private set; }
private SQLiteConnection MyConnection { get; set; }
public ViewModelList(INavigation navigation, int Id)
{
System.Diagnostics.Debug.WriteLine("ViewModelList");
System.Diagnostics.Debug.Flush();
_navigation = navigation;
GetMyDBConnection connection = new GetMyDBConnection();
MyConnection = connection.Connection;
connection = null;
System.Diagnostics.Debug.WriteLine("ViewModelList - connection ptr: " + MyConnection.Handle.ptr.ToString());
System.Diagnostics.Debug.Flush();
_repositoryOne = new RepositoryOne(MyConnection);
_repositoryTwo = new RepositoryTwo(MyConnection);
FinishCommand = new Command(async () => await Finish());
FetchData(Id);
}
various other methods...
}
public class RepositoryOne : IRepositoryOne
{
DatabaseHelper _databaseHelper;
public RepositoryOne(SQLiteConnection connection)
{
_databaseHelper = new DatabaseHelper(connection);
}
various repository methods...
}
public class RepositoryTwo : IRepositoryTwo
{
DatabaseHelper _databaseHelper;
public RepositoryTwo(SQLiteConnection connection)
{
_databaseHelper = new DatabaseHelper(connection);
}
various repository methods...
}
public class DatabaseHelper
{
static SQLiteConnection sqliteconnection;
public const string DbFileName = "information.db";
public DatabaseHelper(SQLiteConnection connection)
{
System.Diagnostics.Debug.WriteLine("DatabaseHelper() - sqliteconnection ptr: " + connection.Handle.ptr.ToString());
sqliteconnection = connection;
sqliteconnection.CreateTable<Table>();
}
various specific database items...
}