[WPF] Fix application crash when storage file was deleted while app is being active. (#897)

* Create file if non exist on each call

* Added asyn operation
This commit is contained in:
Alexander Bodalevsky
2017-06-22 14:53:23 +03:00
committed by Sergey Akhalkov
parent ef5ab894b1
commit fe9745237b
2 changed files with 6 additions and 12 deletions

View File

@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(NuGetPackageRoot)' == ''">
<NuGetPackageRoot>$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
</PropertyGroup>
<ImportGroup>
<Import Project="$(NuGetPackageRoot)\Facebook.Yoga\1.0.1-pre\build\netstandard\Facebook.Yoga.targets" Condition="Exists('$(NuGetPackageRoot)\Facebook.Yoga\1.0.1-pre\build\netstandard\Facebook.Yoga.targets')" />
</ImportGroup>
</Project>

View File

@@ -62,11 +62,12 @@ namespace CodePush.Net46.Adapters.Storage
private readonly SemaphoreSlim mutex = new SemaphoreSlim(1, 1);
const string STORAGE_NAME = "AppStorage.data";
IFile storageFile = null;
string storageFileName = null;
public ApplicationDataContainer(string name = STORAGE_NAME)
{
storageFile = FileSystem.Current.LocalStorage.CreateFileAsync(name, CreationCollisionOption.OpenIfExists).Result;
storageFileName = name;
var storageFile = FileSystem.Current.LocalStorage.CreateFileAsync(storageFileName, CreationCollisionOption.OpenIfExists).Result;
var data = CodePushUtils.GetJObjectFromFileAsync(storageFile).Result;
if (data != null)
@@ -90,6 +91,7 @@ namespace CodePush.Net46.Adapters.Storage
{
await mutex.WaitAsync().ConfigureAwait(false);
var jobject = JObject.FromObject(Values);
var storageFile = await FileSystem.Current.LocalStorage.CreateFileAsync(storageFileName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
await storageFile.WriteAllTextAsync(JsonConvert.SerializeObject(jobject)).ConfigureAwait(false);
mutex.Release();
}
@@ -97,7 +99,8 @@ namespace CodePush.Net46.Adapters.Storage
public async Task DeleteAsync()
{
Values.Clear();
await storageFile.DeleteAsync();
var storageFile = await FileSystem.Current.LocalStorage.CreateFileAsync(storageFileName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
await storageFile.DeleteAsync().ConfigureAwait(false);
}
}
}