Pool and reuse MKMapView instances on iOS 9 as well as iOS 10 (#680)
authorE.Z. Hart <hartez@users.noreply.github.com>
Mon, 16 Jan 2017 19:36:49 +0000 (12:36 -0700)
committerKangho Hur <kangho.hur@samsung.com>
Fri, 24 Mar 2017 04:12:23 +0000 (13:12 +0900)
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla39489.cs
Xamarin.Forms.Core.iOS.UITests/UITestCategories.cs
Xamarin.Forms.Maps.iOS/FormsMaps.cs
Xamarin.Forms.Maps.iOS/MapRenderer.cs

index 0e04fcd..622a5f4 100644 (file)
@@ -7,6 +7,7 @@ using System.Threading.Tasks;
 using Xamarin.Forms.Maps;
 
 #if UITEST
+using Xamarin.Forms.Core.UITests;
 using Xamarin.UITest;
 using NUnit.Framework;
 #endif
@@ -14,6 +15,9 @@ using NUnit.Framework;
 namespace Xamarin.Forms.Controls.Issues
 {
        [Preserve(AllMembers = true)]
+#if UITEST
+       [Category(UITestCategories.Maps)]
+#endif
        [Issue(IssueTracker.Bugzilla, 39489, "Memory leak when using NavigationPage with Maps", PlatformAffected.Android | PlatformAffected.iOS)]
        public class Bugzilla39489 : TestNavigationPage
        {
index dbc298f..28a6fff 100644 (file)
@@ -29,6 +29,7 @@
                public const string TimePicker = "TimePicker";
                public const string ToolbarItem = "ToolbarItem";
                public const string WebView = "WebView";
+               public const string Maps = "Maps";
 
                public const string ManualReview = "ManualReview";
        }
index 19c0701..a87cb8d 100644 (file)
@@ -7,6 +7,7 @@ namespace Xamarin
        {
                static bool s_isInitialized;
                static bool? s_isiOs8OrNewer;
+               static bool? s_isiOs9OrNewer;
                static bool? s_isiOs10OrNewer;
 
                internal static bool IsiOs8OrNewer
@@ -19,6 +20,16 @@ namespace Xamarin
                        }
                }
 
+               internal static bool IsiOs9OrNewer
+               {
+                       get
+                       {
+                               if (!s_isiOs9OrNewer.HasValue)
+                                       s_isiOs9OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(9, 0);
+                               return s_isiOs9OrNewer.Value;
+                       }
+               }
+
                internal static bool IsiOs10OrNewer
                {
                        get
index cc9f253..794a4a0 100644 (file)
@@ -137,13 +137,13 @@ namespace Xamarin.Forms.Maps.iOS
                        return Control.GetSizeRequest(widthConstraint, heightConstraint);
                }
 
-               // iOS 10 has some issues with releasing memory from map views; each one we create allocates
+               // iOS 9/10 have some issues with releasing memory from map views; each one we create allocates
                // a bunch of memory we can never get back. Until that's fixed, we'll just reuse MKMapViews
                // as much as possible to prevent creating new ones and losing more memory
 
                // For the time being, we don't want ViewRenderer handling disposal of the MKMapView
-               // if we're on iOS 10; during Dispose we'll be putting the MKMapView in a pool instead
-               protected override bool ManageNativeControlLifetime => !FormsMaps.IsiOs10OrNewer;
+               // if we're on iOS 9 or 10; during Dispose we'll be putting the MKMapView in a pool instead
+               protected override bool ManageNativeControlLifetime => !FormsMaps.IsiOs9OrNewer;
 
                protected override void Dispose(bool disposing)
                {
@@ -170,14 +170,14 @@ namespace Xamarin.Forms.Maps.iOS
                                mkMapView.Delegate = null;
                                mkMapView.RemoveFromSuperview();
 
-                               if (FormsMaps.IsiOs10OrNewer)
+                               if (FormsMaps.IsiOs9OrNewer)
                                {
                                        // This renderer is done with the MKMapView; we can put it in the pool
                                        // for other rendererers to use in the future
                                        MapPool.Add(mkMapView);
                                }
 
-                               // For iOS versions < 10, the MKMapView will be disposed in ViewRenderer's Dispose method
+                               // For iOS versions < 9, the MKMapView will be disposed in ViewRenderer's Dispose method
 
                                if (_locationManager != null)
                                {
@@ -208,7 +208,7 @@ namespace Xamarin.Forms.Maps.iOS
                                {
                                        MKMapView mapView = null;
 
-                                       if (FormsMaps.IsiOs10OrNewer)
+                                       if (FormsMaps.IsiOs9OrNewer)
                                        {
                                                // See if we've got an MKMapView available in the pool; if so, use it
                                                mapView = MapPool.Get();
@@ -216,7 +216,7 @@ namespace Xamarin.Forms.Maps.iOS
 
                                        if (mapView == null)
                                        {
-                                               // If this is iOS 9 or lower, or if there weren't any MKMapViews in the pool,
+                                               // If this is iOS 8 or lower, or if there weren't any MKMapViews in the pool,
                                                // create a new one
                                                mapView = new MKMapView(RectangleF.Empty);
                                        }