Skip to content

Instantly share code, notes, and snippets.

@Belorus
Created November 25, 2015 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Belorus/81e0c3ed019af62b593b to your computer and use it in GitHub Desktop.
Save Belorus/81e0c3ed019af62b593b to your computer and use it in GitHub Desktop.
Xamarin IOS 9.2 AOT crash
private async Task DownloadItem(TResource remoteResource, CancellationToken token)
{
token.ThrowIfCancellationRequested();
bool zipValid = true;
ExceptionDispatchInfo exception = null;
do
{
token.ThrowIfCancellationRequested();
using (var outputStream = _storage.OpenReadWrite(remoteResource.FileName))
{
try
{
await _worker.Download(remoteResource, outputStream, _progressReporter, token);
}
catch (OperationCanceledException)
{
throw; // Handled on on upper level
}
catch (Exception e)
{
if (e is IOException && _storage.GetAvailableSpace() < remoteResource.Size)
{
exception = ExceptionDispatchInfo.Capture(new RemoteResourceLowDiskSpaceException("There is not enough space on the disk.", e));
}
else
{
exception = ExceptionDispatchInfo.Capture(e);
}
break;
}
}
try
{
if (remoteResource.IsDownloaded)
{
zipValid = _verifier.Verify(remoteResource, true);
if (!zipValid)
{
remoteResource.Offset = 0;
_storage.DeleteFile(remoteResource.FileName);
if (RemoteResourcesConfig.ThrowOnResourceVerifyError)
{
throw new RemoteResourceInvalidException(string.Format("Zip validation failed for '{0}'.", remoteResource.FileName));
}
}
}
}
catch (Exception e)
{
exception = ExceptionDispatchInfo.Capture(e);
break;
}
} while (!zipValid);
if (exception == null || !(exception.SourceException is IOException))
{
_settings.Update(remoteResource);
}
if (exception != null)
{
exception.Throw();
}
}
public class RemoteResource<TBundleId> : IDownloadable<TBundleId>, ICanClone<RemoteResource<TBundleId>>
{
public string Url { get; set; }
public TBundleId BundleId { get; set; }
public string FileName { get; set; }
public int Offset { get; set; }
public int Size { get; set; }
public float Progress
{
get { return (float)Offset / Size; }
}
public bool IsDownloaded
{
get { return Size > 0 && Offset == Size; }
}
public virtual RemoteResource<TBundleId> Clone()
{
return (RemoteResource<TBundleId>)MemberwiseClone();
}
}
public class RemoteResource : RemoteResource<int>, ICanClone<RemoteResource>
{
public string ResourceVersion;
public RemoteResourceType RemoteResourceType;
public string Hash;
public bool IsCritical;
public RemoteResource Update;
public new RemoteResource Clone()
{
return (RemoteResource)MemberwiseClone();
}
public void CopyTo(RemoteResource other)
{
other.Url = Url;
other.BundleId = BundleId;
other.FileName = FileName;
other.Offset = Offset;
other.Size = Size;
other.ResourceVersion = ResourceVersion;
other.RemoteResourceType = RemoteResourceType;
other.Hash = Hash;
other.IsCritical = IsCritical;
other.Update = Update;
}
}
public enum RemoteResourceType
{
Baseline,
Patch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment