Version
Which LiteDB version/OS/.NET framework version are you using. (REQUIRED)
Latest ,
Describe the bug
public LiteDBService()
{
var db = new LiteDatabase(DependencyService.Get
_collection = db.GetCollection
}
When i upgrade into latest version , I am getting error at the constructor. Might there are some changes can you please share me xamarin forms sample link if it is possible.
@LumiProj Could you provide a sample code and maybe the stack trace of the error?
I also ran into problems with LiteDB 5.0.3 and Xamarin.Forms (4.5) and was also seeing an exception when using LiteDatabase but only on Android. Only tested briefly but worked on UWP and iOS, like @LumiProj mentioned this exception came from the LiteDatabase ctor.
I saw a similar exception/issue mentioned in #1275
The message was:
Sequence contains no elements
The stacktrace was:
at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0000b] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/System.Linq/src/System/Linq/First.cs:16
at LiteDB.Engine.LiteEngine..ctor (LiteDB.Engine.EngineSettings settings) [0x00129] in
at LiteDB.ConnectionString.CreateEngine () [0x0003e] in
at LiteDB.LiteDatabase..ctor (LiteDB.ConnectionString connectionString, LiteDB.BsonMapper mapper) [0x0002e] in
at LiteDB.LiteDatabase..ctor (System.String connectionString, LiteDB.BsonMapper mapper) [0x00007] in
at XamarinBugApp.ViewModels.MainPageViewModel.CreateLiteDb () [0x0001c] in C:\code\work\xamarinbugs\src\XamarinBugApp\ViewModels\MainPageViewModel.cs:45
Attached is a simple repro project, that hopefully works.
XF_debug.zip
Yeah I've got the same problem with Xamarin.Forms 4.5.
The App is crashing at this Line:
var db = new LiteDatabase(dbPath);
I switched back to 4.1.4.
at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable1[T] source) [0x0000b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Linq/src/System/Linq/First.cs:16
at LiteDB.Engine.LiteEngine..ctor (LiteDB.Engine.EngineSettings settings) [0x00123] in <dd8295e446b24319b0f9b29a4b1e03f0>:0
at LiteDB.ConnectionString.CreateEngine () [0x0003e] in <dd8295e446b24319b0f9b29a4b1e03f0>:0
at LiteDB.LiteDatabase..ctor (LiteDB.ConnectionString connectionString, LiteDB.BsonMapper mapper) [0x0002e] in <dd8295e446b24319b0f9b29a4b1e03f0>:0
at uTube.Database.LiteDBService1[T]..ctor () [0x0001a] in /Volumes/--/Projects/Others/uTube/--/--/----/--/LiteDBService.cs:23
that's totally disappointment I am 100% sure they didn't test with the latest version of XF and just release the new version and our all apps are in trouble and they are not taking it seriously.
@LumiProj The problem is happening because of the way DatabasePath() in ILiteDbConfiguration is implemented. A zero-length file is being created if it doesn't exist, and then LiteDB tries to read it as a valid datafile (which it isn't).
We'll probably add a check for zero-length file, but in the meantime, just remove the if (!File.Exists(path))...
@bluekuen @AtifShahzed Please check if your code isn't doing the same.
@LumiProj The problem is happening because of the way
DatabasePath()inILiteDbConfigurationis implemented. A zero-length file is being created if it doesn't exist, and then LiteDB tries to read it as a valid datafile (which it isn't).We'll probably add a check for zero-length file, but in the meantime, just remove the
if (!File.Exists(path))...@bluekuen @AtifShahzed Please check if your code isn't doing the same.
Thats solved the problem now in new apps it is working fine. I don't know what would happen if existing users gets an update so what would impact on their devices.
Now I am submitting new version of my app and see how it goes.
Thanks
I experienced this same issue when passing the db path to the LiteDatabase ctor on an Azure Web App Service, for me the solution was to remove the zero-length db and perform a Find so it created the db again.