Skip to content

Commit ece8788

Browse files
authored
[google_maps_ios] Cache +[GMSServices sharedServices] when first map is created (flutter#6211)
1 parent 8ab7452 commit ece8788

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

packages/google_maps_flutter/google_maps_flutter_ios/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.11
2+
3+
* Precaches Google Maps services initialization and syncing.
4+
15
## 2.1.10
26

37
* Splits iOS implementation out of `google_maps_flutter` as a federated

packages/google_maps_flutter/google_maps_flutter_ios/example/ios/RunnerTests/GoogleMapsTests.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
@import google_maps_flutter_ios;
66
@import google_maps_flutter_ios.Test;
77
@import XCTest;
8+
@import GoogleMaps;
89

910
#import <OCMock/OCMock.h>
1011
#import "PartiallyMockedMapView.h"
1112

13+
@interface FLTGoogleMapFactory (Test)
14+
@property(strong, nonatomic, readonly) id<NSObject> sharedMapServices;
15+
@end
16+
1217
@interface GoogleMapsTests : XCTestCase
1318
@end
1419

@@ -39,4 +44,16 @@ - (void)testFrameObserver {
3944
XCTAssertEqual(mapView.frameObserverCount, 0);
4045
}
4146

47+
- (void)testMapsServiceSync {
48+
id registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
49+
FLTGoogleMapFactory *factory1 = [[FLTGoogleMapFactory alloc] initWithRegistrar:registrar];
50+
XCTAssertNotNil(factory1.sharedMapServices);
51+
FLTGoogleMapFactory *factory2 = [[FLTGoogleMapFactory alloc] initWithRegistrar:registrar];
52+
// Test pointer equality, should be same retained singleton +[GMSServices sharedServices] object.
53+
// Retaining the opaque object should be enough to avoid multiple internal initializations,
54+
// but don't test the internals of the GoogleMaps API. Assume that it does what is documented.
55+
// https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_services#a436e03c32b1c0be74e072310a7158831
56+
XCTAssertEqual(factory1.sharedMapServices, factory2.sharedMapServices);
57+
}
58+
4259
@end

packages/google_maps_flutter/google_maps_flutter_ios/ios/Classes/GoogleMapController.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
@interface FLTGoogleMapFactory ()
1212

1313
@property(weak, nonatomic) NSObject<FlutterPluginRegistrar> *registrar;
14+
@property(strong, nonatomic, readonly) id<NSObject> sharedMapServices;
1415

1516
@end
1617

1718
@implementation FLTGoogleMapFactory
1819

20+
@synthesize sharedMapServices = _sharedMapServices;
21+
1922
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
2023
self = [super init];
2124
if (self) {
@@ -31,11 +34,26 @@ - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar
3134
- (NSObject<FlutterPlatformView> *)createWithFrame:(CGRect)frame
3235
viewIdentifier:(int64_t)viewId
3336
arguments:(id _Nullable)args {
37+
// Precache shared map services, if needed.
38+
// Retain the shared map services singleton, don't use the result for anything.
39+
(void)[self sharedMapServices];
40+
3441
return [[FLTGoogleMapController alloc] initWithFrame:frame
3542
viewIdentifier:viewId
3643
arguments:args
3744
registrar:self.registrar];
3845
}
46+
47+
- (id<NSObject>)sharedMapServices {
48+
if (_sharedMapServices == nil) {
49+
// Calling this prepares GMSServices on a background thread controlled
50+
// by the GoogleMaps framework.
51+
// Retain the singleton to cache the initialization work across all map views.
52+
_sharedMapServices = [GMSServices sharedServices];
53+
}
54+
return _sharedMapServices;
55+
}
56+
3957
@end
4058

4159
@interface FLTGoogleMapController ()

packages/google_maps_flutter/google_maps_flutter_ios/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_maps_flutter_ios
22
description: iOS implementation of the google_maps_flutter plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/google_maps_flutter/google_maps_flutter_ios
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
5-
version: 2.1.10
5+
version: 2.1.11
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)