diff options
author | dgeller-OUHSC <dgeller@ouhsc.edu> | 2017-10-03 05:23:32 -0500 |
---|---|---|
committer | Kangho Hur <kangho.hur@samsung.com> | 2017-10-23 13:33:58 +0900 |
commit | 7accccfc353bda9088d4e9f1be780cc6624d5eca (patch) | |
tree | cc33c791b9f2f12957e1697cb3b9e69660365ffc /Xamarin.Forms.Platform.Android | |
parent | 4d436f71bc9dece7abce4501747eae4292b676bf (diff) | |
download | xamarin-forms-7accccfc353bda9088d4e9f1be780cc6624d5eca.tar.gz xamarin-forms-7accccfc353bda9088d4e9f1be780cc6624d5eca.tar.bz2 xamarin-forms-7accccfc353bda9088d4e9f1be780cc6624d5eca.zip |
[Core, Android] Fix to address the elusive 'Sharing violation on path PropertyStore.forms.tmp' (#1075)
* use a semaphor for actual saving piece
make reading serialized propertystore readonly to help with collisions.
* change over to async Task and await OnStateChanged() method to try and mitigate what might be the race condition causing the ' Sharing violation on path PropertyStore.forms.tmp' error
* update Semaphore name
* remove private specifier per the style guide
* Update docs and add warning suppression for broken mono warning
* ... and adding the warning suppression to Release mode, as well.
* -add try...finally block so we can guarantee that the semaphore is released if there's an exception while saving the properties.
-remove the legacy code that the semaphore replaces
* Update docs
* Attempting to fix docs
* Trying again to get docs to pass
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
3 files changed, 32 insertions, 26 deletions
diff --git a/Xamarin.Forms.Platform.Android/AppCompat/FormsAppCompatActivity.cs b/Xamarin.Forms.Platform.Android/AppCompat/FormsAppCompatActivity.cs index 26024613..ed28656e 100644 --- a/Xamarin.Forms.Platform.Android/AppCompat/FormsAppCompatActivity.cs +++ b/Xamarin.Forms.Platform.Android/AppCompat/FormsAppCompatActivity.cs @@ -22,6 +22,7 @@ using AColor = Android.Graphics.Color; using AlertDialog = Android.Support.V7.App.AlertDialog; using ARelativeLayout = Android.Widget.RelativeLayout; using Xamarin.Forms.Internals; +using System.Threading.Tasks; #endregion @@ -169,7 +170,7 @@ namespace Xamarin.Forms.Platform.Android callback(resultCode, data); } - protected override void OnCreate(Bundle savedInstanceState) + protected override async void OnCreate(Bundle savedInstanceState) { if (!AllowFragmentRestore) { @@ -201,7 +202,7 @@ namespace Xamarin.Forms.Platform.Android _previousState = _currentState; _currentState = AndroidApplicationLifecycleState.OnCreate; - OnStateChanged(); + await OnStateChanged(); if (Forms.IsLollipopOrNewer) { @@ -228,7 +229,7 @@ namespace Xamarin.Forms.Platform.Android CheckForAppLink(intent); } - protected override void OnPause() + protected override async void OnPause() { _layout.HideKeyboard(true); @@ -241,20 +242,20 @@ namespace Xamarin.Forms.Platform.Android _previousState = _currentState; _currentState = AndroidApplicationLifecycleState.OnPause; - OnStateChanged(); + await OnStateChanged(); } - protected override void OnRestart() + protected override async void OnRestart() { base.OnRestart(); _previousState = _currentState; _currentState = AndroidApplicationLifecycleState.OnRestart; - OnStateChanged(); + await OnStateChanged(); } - protected override void OnResume() + protected override async void OnResume() { // counterpart to OnPause base.OnResume(); @@ -270,24 +271,24 @@ namespace Xamarin.Forms.Platform.Android _previousState = _currentState; _currentState = AndroidApplicationLifecycleState.OnResume; - OnStateChanged(); + await OnStateChanged(); } - protected override void OnStart() + protected override async void OnStart() { base.OnStart(); _previousState = _currentState; _currentState = AndroidApplicationLifecycleState.OnStart; - OnStateChanged(); + await OnStateChanged(); } // Scenarios that stop and restart your app // -- Switches from your app to another app, activity restarts when clicking on the app again. // -- Action in your app that starts a new Activity, the current activity is stopped and the second is created, pressing back restarts the activity // -- The user receives a phone call while using your app on his or her phone - protected override void OnStop() + protected override async void OnStop() { // writing to storage happens here! // full UI obstruction @@ -300,7 +301,7 @@ namespace Xamarin.Forms.Platform.Android _previousState = _currentState; _currentState = AndroidApplicationLifecycleState.OnStop; - OnStateChanged(); + await OnStateChanged(); } void AppOnPropertyChanged(object sender, PropertyChangedEventArgs args) @@ -387,7 +388,7 @@ namespace Xamarin.Forms.Platform.Android UpdateProgressBarVisibility(_busyCount > 0); } - async void OnStateChanged() + async Task OnStateChanged() { if (_application == null) return; diff --git a/Xamarin.Forms.Platform.Android/Deserializer.cs b/Xamarin.Forms.Platform.Android/Deserializer.cs index c85aaa73..ccce9cdf 100644 --- a/Xamarin.Forms.Platform.Android/Deserializer.cs +++ b/Xamarin.Forms.Platform.Android/Deserializer.cs @@ -20,22 +20,28 @@ namespace Xamarin.Forms.Platform.Android return Task.Run(() => { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) - using (IsolatedStorageFileStream stream = store.OpenFile(PropertyStoreFile, System.IO.FileMode.OpenOrCreate)) - using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max)) { - if (stream.Length == 0) + if (!store.FileExists(PropertyStoreFile)) return null; - try + using (IsolatedStorageFileStream stream = store.OpenFile(PropertyStoreFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)) + using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max)) { - var dcs = new DataContractSerializer(typeof(Dictionary<string, object>)); - return (IDictionary<string, object>)dcs.ReadObject(reader); - } - catch (Exception e) - { - Debug.WriteLine("Could not deserialize properties: " + e.Message); - Log.Warning("Xamarin.Forms PropertyStore", $"Exception while reading Application properties: {e}"); + if (stream.Length == 0) + return null; + + try + { + var dcs = new DataContractSerializer(typeof(Dictionary<string, object>)); + return (IDictionary<string, object>)dcs.ReadObject(reader); + } + catch (Exception e) + { + Debug.WriteLine("Could not deserialize properties: " + e.Message); + Log.Warning("Xamarin.Forms PropertyStore", $"Exception while reading Application properties: {e}"); + } } + } return null; diff --git a/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj b/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj index aa91dedb..5dd03486 100644 --- a/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj +++ b/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj @@ -42,8 +42,7 @@ <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <TreatWarningsAsErrors>true</TreatWarningsAsErrors> - <NoWarn> - </NoWarn> + <NoWarn>CS0109</NoWarn> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Turkey|AnyCPU'"> <DebugSymbols>true</DebugSymbols> |