ericsink / SQLitePCL.raw

A Portable Class Library (PCL) for low-level (raw) access to SQLite
Apache License 2.0
534 stars 110 forks source link

SQLitePCL.Batteries.Init() in MVVMCross app not firing #111

Closed TheFrogDaddy closed 2 years ago

TheFrogDaddy commented 8 years ago

I'm calling SQLitePCL.Batteries.Init() within InitializeFirstChance() for an MvvmCross app as instructed in your documentation, however, I keep getting the same error:

System.Exception: You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init()

These are my packages

<package id="sqlite-net-pcl" version="1.2.0" targetFramework="xamarinios10" />
<package id="SQLitePCLRaw.bundle_green" version="1.0.1" targetFramework="xamarinios10" />
<package id="SQLitePCLRaw.core" version="1.0.1" targetFramework="xamarinios10" />
<package id="SQLitePCLRaw.provider.sqlite3.ios_unified" version="1.0.1" targetFramework="xamarinios10" /> 

Am I missing something?

Thanks

ericsink commented 8 years ago

What's the stack trace on the exception?

I 'm pre-coffee, but so far it looks like you're doing things correctly. So I'm wondering if there is any way that something is somehow getting called before batteries Init.

TheFrogDaddy commented 8 years ago

Here you go @ericsink

I've just managed to get passed the error by using SQLitePCL.raw.SetProvider(new SQLite3Provider_sqlite3()); instead, but I'm getting another unrelated error now (I think) so don't know if this has really fixed it, or just hidden the issue for now.

MvvmCross.Platform.Exceptions.MvxException: Failed to construct and initialize ViewModel for type MyApp.Core.ViewModels.DashboardViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information ---> MvvmCross.Platform.Exceptions.MvxException: Problem creating viewModel of type DashboardViewModel ---> MvvmCross.Platform.Exceptions.MvxIoCResolveException: Failed to construct MedicineService ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> MvvmCross.Platform.Exceptions.MvxIoCResolveException: Failed to construct MedicineDatabase ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: You need to call SQLitePCL.raw.SetProvider().  If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().
  at SQLitePCL.SQLite3Provider_bait.SQLitePCL.ISQLite3Provider.sqlite3_open_v2 (System.String filename, System.IntPtr& db, System.Int32 flags, System.String vfs) [0x00000] in <2297f915f41447128032450438491973>:0
  at SQLitePCL.raw.sqlite3_open_v2 (System.String filename, SQLitePCL.sqlite3& db, System.Int32 flags, System.String vfs) [0x00000] in <2297f915f41447128032450438491973>:0
  at SQLite.SQLite3.Open (System.String filename, SQLitePCL.sqlite3& db, System.Int32 flags, System.IntPtr zVfs) [0x00000] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:3331
  at SQLite.SQLiteConnection..ctor (System.String databasePath, SQLite.SQLiteOpenFlags openFlags, System.Boolean storeDateTimeAsTicks) [0x00033] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:222
  at SQLite.SQLiteConnection..ctor (System.String databasePath, System.Boolean storeDateTimeAsTicks) [0x00000] in /Users/fak/Dropbox/Projects/sqlite-net/src/SQLite.cs:190
  at MyApp.iOS.SQLite_iOS.GetConnection () [0x00029] in /MyApp/iOS/Data/SQLite_iOS.cs:25
  at MyApp.Core.Data.MedicineDatabase..ctor () [0x0000d] in MyApp.Core/Data/MedicineDatabase.cs:20
  at at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661
  --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00016] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:667
  at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00089] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:652
  at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:680
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00033] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:318
  --- End of inner exception stack trace ---
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00042] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:322
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer+ConstructingResolver.Resolve () [0x00000] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:79
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, MvvmCross.Platform.IoC.MvxSimpleIoCContainer+IResolver resolver, System.Object& resolved) [0x00041] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:447
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Nullable`1[T] requiredResolverType, System.Object& resolved) [0x0002e] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:405
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Object& resolved) [0x00000] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:387
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.Resolve (System.Type t) [0x00011] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:197
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.Resolve[T] () [0x00000] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:189
  at MvvmCross.Platform.Mvx.Resolve[TService] () [0x00005] in D:\git\MvvmCross\MvvmCross\Platform\Platform\Mvx.cs:34
  at MyApp.Core.Services.MedicineService..ctor () [0x00008] in MyApp.Core/Services/MedicineService.cs:15
  at at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661
  --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00016] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:667
  at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00089] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:652
  at System.Reflection.MonoCMethod.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:680
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00033] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:318
  --- End of inner exception stack trace ---
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00042] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:322
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer+ConstructingResolver.Resolve () [0x00000] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:79
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, MvvmCross.Platform.IoC.MvxSimpleIoCContainer+IResolver resolver, System.Object& resolved) [0x00041] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:447
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Nullable`1[T] requiredResolverType, System.Object& resolved) [0x0002e] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:405
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Object& resolved) [0x00000] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:387
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.TryResolve (System.Type type, System.Object& resolved) [0x00011] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:182
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00015] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:496
  at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x0002a] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:314
  at MvvmCross.Platform.Mvx.IocConstruct (System.Type t) [0x00005] in D:\git\MvvmCross\MvvmCross\Platform\Platform\Mvx.cs:170
  at MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterValues, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00000] in <69bce0378e8e413982d3b552d7e387a8>:0
  --- End of inner exception stack trace ---
  at MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterValues, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00027] in <69bce0378e8e413982d3b552d7e387a8>:0
  at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request, MvvmCross.Core.ViewModels.IMvxBundle savedState, MvvmCross.Core.ViewModels.IMvxViewModelLocator viewModelLocator) [0x00015] in <69bce0378e8e413982d3b552d7e387a8>:0
  --- End of inner exception stack trace ---
  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:79
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:63
  at MyApp.iOS.Application.Main (System.String[] args) [0x00008] in /MyApp/iOS/Main.cs:17
ericsink commented 8 years ago

It definitely seems like your code is trying to construct a SQLite connection before batteries init gets called.

You added the SQLite-net-pcl package to your iOS app project? Not just to your core/portable project?

TheFrogDaddy commented 8 years ago

Correct. If I run the app in debug it certainly hits the init before creating an exception, this seems to happen with MVVM Cross compiles the ViewModel.

ericsink commented 8 years ago

I'm not sure what's going on.

The stack trace matches what you would get if you did not add the sqlite-net-pcl nuget package to your iOS app project.

Note that you shouldn't even need to call Batteries.Init() at all. sqlite-net-pcl 1.2 does this for you. As long as that nuget package is added to your app project.

The fact that SQLitePCL.raw.SetProvider(new SQLite3Provider_sqlite3()) worked for you (presumably in InitializeFirstChance), suggests that you do in fact have sqlite-net-pcl in your app project, but somehow the wrong copy of the batteries DLL is getting used. Which I've never seen before, and which makes me think about things like a full rebuild or a completely wipe of your build output directory.

ericsink commented 2 years ago

Closing old/stale issue.