mirror of
				https://github.com/Nioux/AideDeJeu.git
				synced 2025-10-31 15:36:07 +00:00 
			
		
		
		
	Ajout d'un semaphore autour de la bdd library
This commit is contained in:
		
							parent
							
								
									f603308299
								
							
						
					
					
						commit
						34e6fd36e4
					
				
					 4 changed files with 50 additions and 2 deletions
				
			
		|  | @ -61,6 +61,7 @@ namespace AideDeJeu.ViewModels | ||||||
|         { |         { | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|  |                 await StoreViewModel.SemaphoreLibrary.WaitAsync(); | ||||||
|                 using (var context = await StoreViewModel.GetLibraryContextAsync()) |                 using (var context = await StoreViewModel.GetLibraryContextAsync()) | ||||||
|                 { |                 { | ||||||
|                     var primary = await context.Items. |                     var primary = await context.Items. | ||||||
|  | @ -123,6 +124,10 @@ namespace AideDeJeu.ViewModels | ||||||
|             { |             { | ||||||
|                 return new List<SearchedItem>(); |                 return new List<SearchedItem>(); | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 StoreViewModel.SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -319,6 +319,7 @@ namespace AideDeJeu.ViewModels | ||||||
|             var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? ""; |             var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? ""; | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|  |                 await StoreViewModel.SemaphoreLibrary.WaitAsync(); | ||||||
|                 using (var context = await StoreViewModel.GetLibraryContextAsync()) |                 using (var context = await StoreViewModel.GetLibraryContextAsync()) | ||||||
|                 { |                 { | ||||||
|                     return context.SpellsVO.Where(spell => |                     return context.SpellsVO.Where(spell => | ||||||
|  | @ -338,6 +339,10 @@ namespace AideDeJeu.ViewModels | ||||||
|             { |             { | ||||||
|                 return new List<Item>(); |                 return new List<Item>(); | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 StoreViewModel.SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public override List<KeyValuePair<string, string>> Classes { get; } = new List<KeyValuePair<string, string>>() |         public override List<KeyValuePair<string, string>> Classes { get; } = new List<KeyValuePair<string, string>>() | ||||||
|  | @ -406,6 +411,7 @@ namespace AideDeJeu.ViewModels | ||||||
|             var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? ""; |             var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? ""; | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|  |                 await StoreViewModel.SemaphoreLibrary.WaitAsync(); | ||||||
|                 using (var context = await StoreViewModel.GetLibraryContextAsync()) |                 using (var context = await StoreViewModel.GetLibraryContextAsync()) | ||||||
|                 { |                 { | ||||||
|                     return context.SpellsHD.Where(spell => |                     return context.SpellsHD.Where(spell => | ||||||
|  | @ -425,6 +431,10 @@ namespace AideDeJeu.ViewModels | ||||||
|             { |             { | ||||||
|                 return new List<Item>(); |                 return new List<Item>(); | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 StoreViewModel.SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public override List<KeyValuePair<string, string>> Classes { get; } = new List<KeyValuePair<string, string>>() |         public override List<KeyValuePair<string, string>> Classes { get; } = new List<KeyValuePair<string, string>>() | ||||||
|  | @ -674,6 +684,7 @@ namespace AideDeJeu.ViewModels | ||||||
| 
 | 
 | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|  |                 await StoreViewModel.SemaphoreLibrary.WaitAsync(); | ||||||
|                 using (var context = await StoreViewModel.GetLibraryContextAsync()) |                 using (var context = await StoreViewModel.GetLibraryContextAsync()) | ||||||
|                 { |                 { | ||||||
|                     return context.MonstersVO.Where(monster => |                     return context.MonstersVO.Where(monster => | ||||||
|  | @ -694,6 +705,10 @@ namespace AideDeJeu.ViewModels | ||||||
|             { |             { | ||||||
|                 return new List<Item>(); |                 return new List<Item>(); | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 StoreViewModel.SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public override List<KeyValuePair<string, string>> Categories { get; } = new List<KeyValuePair<string, string>>() |         public override List<KeyValuePair<string, string>> Categories { get; } = new List<KeyValuePair<string, string>>() | ||||||
|  | @ -796,6 +811,7 @@ namespace AideDeJeu.ViewModels | ||||||
| 
 | 
 | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|  |                 await StoreViewModel.SemaphoreLibrary.WaitAsync(); | ||||||
|                 using (var context = await StoreViewModel.GetLibraryContextAsync()) |                 using (var context = await StoreViewModel.GetLibraryContextAsync()) | ||||||
|                 { |                 { | ||||||
|                     return context.MonstersHD.Where(monster => |                     return context.MonstersHD.Where(monster => | ||||||
|  | @ -816,6 +832,10 @@ namespace AideDeJeu.ViewModels | ||||||
|             { |             { | ||||||
|                 return new List<Item>(); |                 return new List<Item>(); | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 StoreViewModel.SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|         public override List<KeyValuePair<string, string>> Categories { get; } = new List<KeyValuePair<string, string>>() |         public override List<KeyValuePair<string, string>> Categories { get; } = new List<KeyValuePair<string, string>>() | ||||||
|         { |         { | ||||||
|  | @ -935,7 +955,8 @@ namespace AideDeJeu.ViewModels | ||||||
| 
 | 
 | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|                 using (var context = await GetLibraryContextAsync()) |                 await StoreViewModel.SemaphoreLibrary.WaitAsync(); | ||||||
|  |                 using (var context = await StoreViewModel.GetLibraryContextAsync()) | ||||||
|                 { |                 { | ||||||
|                     return context.Equipments.Where(equipment => |                     return context.Equipments.Where(equipment => | ||||||
|                         equipment.Type.ToLower().Contains(type.ToLower()) && |                         equipment.Type.ToLower().Contains(type.ToLower()) && | ||||||
|  | @ -953,6 +974,10 @@ namespace AideDeJeu.ViewModels | ||||||
|             { |             { | ||||||
|                 return new List<Item>(); |                 return new List<Item>(); | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 StoreViewModel.SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public abstract List<KeyValuePair<string, string>> Types { get; } |         public abstract List<KeyValuePair<string, string>> Types { get; } | ||||||
|  | @ -1042,7 +1067,8 @@ namespace AideDeJeu.ViewModels | ||||||
| 
 | 
 | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|                 using (var context = await GetLibraryContextAsync()) |                 await StoreViewModel.SemaphoreLibrary.WaitAsync(); | ||||||
|  |                 using (var context = await StoreViewModel.GetLibraryContextAsync()) | ||||||
|                 { |                 { | ||||||
|                     return context.MagicItems.Where(magicitem => |                     return context.MagicItems.Where(magicitem => | ||||||
|                         MatchContains(magicitem.Type, type) && |                         MatchContains(magicitem.Type, type) && | ||||||
|  | @ -1056,6 +1082,10 @@ namespace AideDeJeu.ViewModels | ||||||
|             { |             { | ||||||
|                 return new List<Item>(); |                 return new List<Item>(); | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 StoreViewModel.SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public abstract List<KeyValuePair<string, string>> Types { get; } |         public abstract List<KeyValuePair<string, string>> Types { get; } | ||||||
|  |  | ||||||
|  | @ -11,6 +11,7 @@ using System.Linq; | ||||||
| using System.Reflection; | using System.Reflection; | ||||||
| using System.Text; | using System.Text; | ||||||
| using System.Text.RegularExpressions; | using System.Text.RegularExpressions; | ||||||
|  | using System.Threading; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using Xamarin.Forms; | using Xamarin.Forms; | ||||||
| 
 | 
 | ||||||
|  | @ -405,6 +406,8 @@ namespace AideDeJeu.ViewModels | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         public static SemaphoreSlim SemaphoreLibrary = new SemaphoreSlim(1, 1); | ||||||
|  | 
 | ||||||
|         public static async Task<AideDeJeuContext> GetLibraryContextAsync() |         public static async Task<AideDeJeuContext> GetLibraryContextAsync() | ||||||
|         { |         { | ||||||
|             var dbPath = await DependencyService.Get<INativeAPI>().GetDatabasePathAsync("library"); |             var dbPath = await DependencyService.Get<INativeAPI>().GetDatabasePathAsync("library"); | ||||||
|  | @ -420,6 +423,7 @@ namespace AideDeJeu.ViewModels | ||||||
|             } |             } | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|  |                 await SemaphoreLibrary.WaitAsync(); | ||||||
|                 using (var context = await GetLibraryContextAsync()) |                 using (var context = await GetLibraryContextAsync()) | ||||||
|                 { |                 { | ||||||
|                     return await context.Items.Where(item => item.Id == id || item.RootId == id).FirstOrDefaultAsync(); |                     return await context.Items.Where(item => item.Id == id || item.RootId == id).FirstOrDefaultAsync(); | ||||||
|  | @ -429,6 +433,10 @@ namespace AideDeJeu.ViewModels | ||||||
|             { |             { | ||||||
|                 return null; |                 return null; | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public async Task<Item> GetItemFromDataAsyncOld(string source, string anchor) |         public async Task<Item> GetItemFromDataAsyncOld(string source, string anchor) | ||||||
|  |  | ||||||
|  | @ -64,6 +64,7 @@ namespace AideDeJeu.Views | ||||||
|             await Task.Delay(1000).ConfigureAwait(false); |             await Task.Delay(1000).ConfigureAwait(false); | ||||||
|             try |             try | ||||||
|             { |             { | ||||||
|  |                 await StoreViewModel.SemaphoreLibrary.WaitAsync(); | ||||||
|                 using (var context = await StoreViewModel.GetLibraryContextAsync().ConfigureAwait(false)) |                 using (var context = await StoreViewModel.GetLibraryContextAsync().ConfigureAwait(false)) | ||||||
|                 { |                 { | ||||||
|                     var item = context.Items.FirstOrDefault(); |                     var item = context.Items.FirstOrDefault(); | ||||||
|  | @ -72,6 +73,10 @@ namespace AideDeJeu.Views | ||||||
|             catch |             catch | ||||||
|             { |             { | ||||||
|             } |             } | ||||||
|  |             finally | ||||||
|  |             { | ||||||
|  |                 StoreViewModel.SemaphoreLibrary.Release(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Yan Maniez
						Yan Maniez