Skip to content

Commit 1176b86

Browse files
committed
Minor update to handle Xamarin support.
1 parent 1ded620 commit 1176b86

File tree

2 files changed

+106
-28
lines changed

2 files changed

+106
-28
lines changed

ReflectXMLDB/DatabaseHandler.cs

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class DatabaseHandler : DatabaseHandlerBase
3838
/// <summary>
3939
/// Mutex to protect file access from multiple processes.
4040
/// </summary>
41-
protected static Mutex mutex = new Mutex(false, "ReflectXMLDBMutex");
41+
protected static Mutex mutex = null;
4242

4343
#endregion
4444

@@ -127,6 +127,27 @@ public OnDatabaseExportedEventArgs(string databaseName, DateTime date)
127127

128128
#endregion
129129

130+
/// <summary>
131+
/// Creates a new instance of DatabaseHandler.
132+
/// </summary>
133+
public DatabaseHandler()
134+
{
135+
//Xamarin does not currently support mutexes for some mobile platforms.
136+
//When a new Mutex instance is created by Xamarin.Forms, a NotSupportedException/NotImplementedException is thrown.
137+
//The following code handles this exception. The mutex will be ignored because it is null.
138+
try
139+
{
140+
mutex = new Mutex(false, "ReflectXMLDBMutex");
141+
}
142+
catch (Exception ex)
143+
{
144+
if (ex is NotSupportedException || ex is NotImplementedException)
145+
{
146+
Console.WriteLine("Cross-process disabled.");
147+
}
148+
}
149+
}
150+
130151
#region Private methods
131152

132153
/// <summary>
@@ -219,17 +240,30 @@ private string GetDatabasePath(Type type)
219240
/// <param name="path"></param>
220241
private void StartMonitoringDirectory(string path)
221242
{
222-
fileSystemWatcher = new FileSystemWatcher
243+
//Xamarin does not currently support a unified FileSystemWatcher for mobile platforms.
244+
//When a new FileSystemWatcher instance is created by Xamarin.Forms, a NotSupportedException/NotImplementedException is thrown.
245+
//The following code handles this exception. The FileSystemWatcher will be ignored because it is null.
246+
try
223247
{
224-
Path = path
225-
};
226-
fileSystemWatcher.Changed += FileSystemWatcher_Changed;
227-
fileSystemWatcher.Created += FileSystemWatcher_Created;
228-
fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
229-
fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
248+
fileSystemWatcher = new FileSystemWatcher
249+
{
250+
Path = path
251+
};
252+
fileSystemWatcher.Changed += FileSystemWatcher_Changed;
253+
fileSystemWatcher.Created += FileSystemWatcher_Created;
254+
fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
255+
fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
230256

231-
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
232-
fileSystemWatcher.EnableRaisingEvents = true;
257+
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
258+
fileSystemWatcher.EnableRaisingEvents = true;
259+
}
260+
catch(Exception ex)
261+
{
262+
if(ex is NotSupportedException || ex is NotImplementedException)
263+
{
264+
Console.WriteLine("FileWatcher disabled.");
265+
}
266+
}
233267
}
234268
/// <summary>
235269
/// Stops monitoring a folder. This disposes the Filewatcher and its events.
@@ -266,7 +300,11 @@ private T GetDatabase<T>() where T : IDatabase
266300
{
267301
try
268302
{
269-
mutex.WaitOne();
303+
if(!mutex.IsNull())
304+
{
305+
mutex.WaitOne();
306+
}
307+
270308
database = path.Deserialize<T>();
271309
}
272310
catch
@@ -275,7 +313,10 @@ private T GetDatabase<T>() where T : IDatabase
275313
}
276314
finally
277315
{
278-
mutex.ReleaseMutex();
316+
if(!mutex.IsNull())
317+
{
318+
mutex.ReleaseMutex();
319+
}
279320
}
280321
}
281322
catch
@@ -402,7 +443,11 @@ public void CreateDatabase<T>() where T : IDatabase
402443
{
403444
try
404445
{
405-
mutex.WaitOne();
446+
if (!mutex.IsNull())
447+
{
448+
mutex.WaitOne();
449+
}
450+
406451
xml.Save(path);
407452
}
408453
catch
@@ -411,7 +456,10 @@ public void CreateDatabase<T>() where T : IDatabase
411456
}
412457
finally
413458
{
414-
mutex.ReleaseMutex();
459+
if (!mutex.IsNull())
460+
{
461+
mutex.ReleaseMutex();
462+
}
415463
}
416464
}
417465
catch
@@ -441,7 +489,11 @@ public void DeleteDatabase<T>() where T : IDatabase
441489
{
442490
try
443491
{
444-
mutex.WaitOne();
492+
if (!mutex.IsNull())
493+
{
494+
mutex.WaitOne();
495+
}
496+
445497
File.Delete(path);
446498
}
447499
catch
@@ -450,7 +502,10 @@ public void DeleteDatabase<T>() where T : IDatabase
450502
}
451503
finally
452504
{
453-
mutex.ReleaseMutex();
505+
if (!mutex.IsNull())
506+
{
507+
mutex.ReleaseMutex();
508+
}
454509
}
455510
}
456511
catch
@@ -577,16 +632,30 @@ public void Save<T>(ICollection<T> items) where T : ICollectableObject
577632
{
578633
try
579634
{
580-
mutex.WaitOne();
635+
if (!mutex.IsNull())
636+
{
637+
mutex.WaitOne();
638+
}
639+
581640
xml.Save(path);
641+
642+
//Will fire the database changed event when the FileSystemWatcher is unavailable.
643+
//This should happen only when using Xamarin.Forms as there is no native support for that class.
644+
if(fileSystemWatcher.IsNull())
645+
{
646+
OnDatabaseChanged?.Invoke(this, new OnDatabaseChangedEventArgs(dbInfo.DatabaseType.Name, DateTime.Now));
647+
}
582648
}
583649
catch
584650
{
585651
throw;
586652
}
587653
finally
588654
{
589-
mutex.ReleaseMutex();
655+
if (!mutex.IsNull())
656+
{
657+
mutex.ReleaseMutex();
658+
}
590659
}
591660
}
592661
catch
@@ -694,7 +763,11 @@ public void ImportDatabase(string fileToImport, string exportPath)
694763
{
695764
try
696765
{
697-
mutex.WaitOne();
766+
if (!mutex.IsNull())
767+
{
768+
mutex.WaitOne();
769+
}
770+
698771
//Opens the zip file up to be read
699772
using (ZipArchive archive = ZipFile.OpenRead(fileToImport))
700773
{
@@ -727,7 +800,10 @@ public void ImportDatabase(string fileToImport, string exportPath)
727800
}
728801
finally
729802
{
730-
mutex.ReleaseMutex();
803+
if (!mutex.IsNull())
804+
{
805+
mutex.ReleaseMutex();
806+
}
731807
}
732808
}
733809
catch
@@ -760,7 +836,10 @@ public void ExportDatabase(string pathToSave, string filename, string fileExtens
760836
{
761837
try
762838
{
763-
mutex.WaitOne();
839+
if (!mutex.IsNull())
840+
{
841+
mutex.WaitOne();
842+
}
764843

765844
if (!Directory.Exists(pathToSave))
766845
{
@@ -779,7 +858,10 @@ public void ExportDatabase(string pathToSave, string filename, string fileExtens
779858
}
780859
finally
781860
{
782-
mutex.ReleaseMutex();
861+
if (!mutex.IsNull())
862+
{
863+
mutex.ReleaseMutex();
864+
}
783865
}
784866
}
785867
catch

ReflectXMLDB/ReflectXMLDB.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<Company></Company>
77
<Description>Simple database handler based on XML serialization and reflection.
88

9-
This is a simple project that aims for C#/.NET beginners to achieve database functionality in their applications. The project is written for .NET Standard 2.0, which is compatible with .NET Core 2.0+ and .NET Framework 4.6.1+.</Description>
9+
This is a simple project that aims for C#/.NET developers to achieve database functionality in their applications. The project is written for .NET Standard 2.0, which is compatible with .NET Core 2.0+ and .NET Framework 4.6.1+.</Description>
1010
<Copyright>Copyright© ReflectXMLDB 2018</Copyright>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1212
<AssemblyName>ReflectXMLDB</AssemblyName>
1313
<RootNamespace>ReflectXMLDB</RootNamespace>
1414
<Product>ReflectXMLDB</Product>
1515
<PackageId>ReflectXMLDB</PackageId>
16-
<Version>0.0.2</Version>
16+
<Version>0.0.3</Version>
1717
<PackageLicenseUrl>https://github.com/Fe-Bell/ReflectXMLDB/blob/master/LICENSE</PackageLicenseUrl>
1818
<PackageProjectUrl>https://github.com/Fe-Bell/ReflectXMLDB</PackageProjectUrl>
1919
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
@@ -28,8 +28,4 @@ This is a simple project that aims for C#/.NET beginners to achieve database fun
2828
<OutputPath>..\bin\Release\netstandard2.0\</OutputPath>
2929
</PropertyGroup>
3030

31-
<ItemGroup>
32-
<Folder Include="Interface\" />
33-
</ItemGroup>
34-
3531
</Project>

0 commit comments

Comments
 (0)