step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
668 stars 168 forks source link

Issue Creating Offline Database in MAUI on iOS - "Your platform does not support FileStream.Lock" #308

Open FBonini22 opened 1 year ago

FBonini22 commented 1 year ago

Hello,

I am receiving the below exception when running my .NET MAUI app on any iOS device. The code works on Android devices.:

"Your platform does not support FileStream.Lock. Please set mode=Exclusive in your connnection string to avoid this error."

image

Here is the stack trace, it seems to be traced back to LiteDB:

  at LiteDB.StreamExtensions.CreateLockNotSupportedException(PlatformNotSupportedException innerEx)
   at LiteDB.StreamExtensions.<>c__DisplayClass4_0.<TryLock>b__0()
   at LiteDB.FileHelper.TryExec(Action action, TimeSpan timeout)
   at LiteDB.StreamExtensions.TryLock(FileStream stream, Int64 position, Int64 length, TimeSpan timeout)
   at LiteDB.FileDiskService.Lock(LockState state, TimeSpan timeout)
   at LiteDB.LiteEngine..ctor(IDiskService disk, String password, Nullable`1 timeout, Int32 cacheSize, Logger log, Boolean utcDate)
   at LiteDB.LiteDatabase.<>c__DisplayClass11_0.<.ctor>b__0()
   at LiteDB.LazyLoad`1[[LiteDB.LiteEngine, LiteDB, Version=4.1.4.0, Culture=neutral, PublicKeyToken=4ee40123013c9f27]].get_Value()
   at LiteDB.LiteCollection`1.<Find>d__17[[Firebase.Database.Offline.OfflineEntry, Firebase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at System.Linq.Enumerable.ToDictionary[OfflineEntry,String,OfflineEntry](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[OfflineEntry,String,OfflineEntry](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Firebase.Database.Offline.OfflineDatabase..ctor(Type itemType, String filenameModifier)

If I install the latest LiteDB from NuGet, I still get an error on the same line, but the debugger just can't resolve the symbols.

Below is the code I am running which is causing the issue:

public UserRootDatabase(string root, bool isOnline)
        {
            this.root = root;

            string userId = "TESTUSERID";

            clientOptions = new FirebaseOptions()
            {
                OfflineDatabaseFactory = (t, s) => **new OfflineDatabase(t, s),**
                AuthTokenAsyncFactory = async () =>
                {
                    return userId;
                }
            };

            string path = Config.Urls.FirebaseUrl.addPath("users").addPath(userId);

            firebaseClient = new FirebaseClient(path, clientOptions);

            rtdb = firebaseClient
                .Child(root)
                .AsRealtimeDatabase<T>(root,
                streamingOptions: StreamingOptions.None,
                initialPullStrategy: InitialPullStrategy.None,
                pushChanges: false);

The error occurs when rtdb is set to firebaseClient.Child(root)..... and the action "new OfflineDatabase(t, s) is then called.

Does anyone have an idea what could be causing an error like that? I'm going to try a couple more things and report back. Love this library, it's very helpful. TIA

FBonini22 commented 1 year ago

I went to Microsoft Docs and found that iOS isn't supported for filestream locking.

https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream.lock?view=net-7.0

[**System.Runtime.Versioning.UnsupportedOSPlatform("ios")**] [System.Runtime.Versioning.UnsupportedOSPlatform("macos")] [System.Runtime.Versioning.UnsupportedOSPlatform("tvos")] [System.Runtime.Versioning.UnsupportedOSPlatform("freebsd")] public virtual void Lock (long position, long length);

I didn't have this issue in a prior Xamarin Forms app.

I will have to somehow get around this, but I'll share anything I come up with.