Skip to content

Commit

Permalink
Dispose empty sets and try/catch exceptions (#10955)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored and rmarinho committed Oct 27, 2022
1 parent bc04bf1 commit 10a8bba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/Core/src/Platform/iOS/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
using Foundation;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -57,14 +58,23 @@ public static void CreatePlatformWindow(this IUIWindowSceneDelegate sceneDelegat
dicts.Add(session.UserInfo);
if (session.StateRestorationActivity?.UserInfo is not null)
dicts.Add(session.StateRestorationActivity.UserInfo);
if (connectionOptions.UserActivities is not null)
try
{
foreach (var u in connectionOptions.UserActivities)
using var activities = connectionOptions.UserActivities;
if (activities is not null)
{
if (u is NSUserActivity userActivity && userActivity.UserInfo is not null)
dicts.Add(userActivity.UserInfo);
foreach (var u in activities)
{
if (u is NSUserActivity userActivity && userActivity.UserInfo is not null)
dicts.Add(userActivity.UserInfo);
}
}
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
}

var window = CreatePlatformWindow(application, scene as UIWindowScene, dicts.ToArray());
if (window is not null)
Expand Down
30 changes: 24 additions & 6 deletions src/Essentials/src/Platform/WindowStateManager.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,18 @@ public void Init(Func<UIViewController?>? getCurrentUIViewController) =>
// if we have scene support, use that
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
{
var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows.FirstOrDefault();
try
{
using var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows.FirstOrDefault();
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
return null;
}
}

// use the windows property (up to 13.0)
Expand All @@ -115,9 +124,18 @@ public void Init(Func<UIViewController?>? getCurrentUIViewController) =>
// if we have scene support, use that
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
{
var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows;
try
{
using var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
return windowScene?.Windows;
}
catch (InvalidCastException)
{
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
// This only throws if the collection is empty.
return null;
}
}

// use the windows property (up to 15.0)
Expand Down

0 comments on commit 10a8bba

Please sign in to comment.