Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios] Add a flag to pause network requests. #15650

Closed
wants to merge 9 commits into from
2 changes: 1 addition & 1 deletion next/platform/macos/macos.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ target_sources(
${MBGL_ROOT}/platform/darwin/src/MGLMapCamera.mm
${MBGL_ROOT}/platform/darwin/src/MGLMapSnapshotter.mm
${MBGL_ROOT}/platform/darwin/src/MGLMultiPoint.mm
${MBGL_ROOT}/platform/darwin/src/MGLNetworkConfiguration.m
${MBGL_ROOT}/platform/darwin/src/MGLNetworkConfiguration.mm
${MBGL_ROOT}/platform/darwin/src/MGLOfflinePack.mm
${MBGL_ROOT}/platform/darwin/src/MGLOfflineStorage.mm
${MBGL_ROOT}/platform/darwin/src/MGLOpenGLStyleLayer.mm
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/filesource-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"//": "This file can be edited manually and is the canonical source.",
"sources": [
"platform/darwin/src/MGLLoggingConfiguration.mm",
"platform/darwin/src/MGLNetworkConfiguration.m",
"platform/darwin/src/MGLNetworkConfiguration.mm",
"platform/darwin/src/http_file_source.mm",
"platform/default/src/mbgl/storage/file_source.cpp",
"platform/default/src/mbgl/storage/sqlite3.cpp"
Expand Down
11 changes: 11 additions & 0 deletions platform/darwin/src/MGLNetworkConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ MGL_EXPORT
*/
@property (atomic, strong, null_resettable) NSURLSessionConfiguration *sessionConfiguration;

/**
A Boolean value indicating whether the current `NSURLSessionConfiguration` stops
making network requests.
fabian-guerra marked this conversation as resolved.
Show resolved Hide resolved

When this property is set to `YES` `MGLMapView` will rely solely on pre-cached
tiles.

The default value of this property is `NO`.
*/
@property (atomic, assign) BOOL stopsRequests;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "MGLNetworkConfiguration_Private.h"

#include <mbgl/storage/reachability.h>
#include <mbgl/storage/network_status.hpp>

static NSString * const MGLStartTime = @"start_time";
static NSString * const MGLResourceType = @"resource_type";
Expand Down Expand Up @@ -66,6 +67,19 @@ - (NSURLSessionConfiguration *)defaultSessionConfiguration {
return sessionConfiguration;
}

- (void)setStopsRequests:(BOOL)stopsRequests {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopRequests is not really a great name for this API, because is a double negation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this; @tobrun will setConnected remain the same?

@fabian-guerra how about we go with a connected property? My concern here is that this might be confused whether there is a valid connection (a la reachability).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should match Android in this instance; let's discuss.

if (stopsRequests) {
mbgl::NetworkStatus::Set(mbgl::NetworkStatus::Status::Offline);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong, but this will be Offline forever. Because once you set the status to Offline, the following code will always be true.

- (BOOL)stopsRequests {
    auto status = mbgl::NetworkStatus::Get();
    return status == mbgl::NetworkStatus::Status::Offline;
}

} else {
mbgl::NetworkStatus::Set(mbgl::NetworkStatus::Status::Online);
}
}

- (BOOL)stopsRequests {
auto status = mbgl::NetworkStatus::Get();
return status == mbgl::NetworkStatus::Status::Offline;
}

- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType {
if (urlString && ![self eventDictionaryForKey:urlString]) {
NSDate *startDate = [NSDate date];
Expand Down
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT

* Added `-[MGLMapSnapshotOverlay coordinateForPoint:]` and `-[MGLMapSnapshotOverlay pointForCoordinate:]` to convert between context and map coordinates, mirroring those of `MGLMapSnapshot`. ([#15746](https://github.com/mapbox/mapbox-gl-native/pull/15746))
* Suppress network requests for expired tiles update, if these tiles are invisible. ([#15741](https://github.com/mapbox/mapbox-gl-native/pull/15741))
* Added `MGLNetworkConfiguration.stopsRequests` to stop `NSURLSessionConfiguration` from creating requests. ([#15650](https://github.com/mapbox/mapbox-gl-native/pull/15650))
* Fixed an issue that cause the ornaments to ignore `MGLMapView.contentInset` property. ([#15584](https://github.com/mapbox/mapbox-gl-native/pull/15584))
* Fixed an issue that cause `-[MGLMapView setCamere:withDuration:animationTimingFunction:edgePadding:completionHandler:]` persist the value of `edgePadding`. ([#15584](https://github.com/mapbox/mapbox-gl-native/pull/15584))
* Added `MGLMapView.automaticallyAdjustsContentInset` property that indicates if wether the map view should automatically adjust its content insets. ([#15584](https://github.com/mapbox/mapbox-gl-native/pull/15584))
Expand Down
20 changes: 10 additions & 10 deletions platform/ios/ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
CA6914B520E67F50002DB0EE /* MGLAnnotationViewIntegrationTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = CA6914B420E67F50002DB0EE /* MGLAnnotationViewIntegrationTests.mm */; };
CA7766832229C10E0008DE9E /* MGLCompactCalloutView.m in Sources */ = {isa = PBXBuildFile; fileRef = DA8848451CBAFB9800AB86E3 /* MGLCompactCalloutView.m */; };
CA7766842229C11A0008DE9E /* SMCalloutView.m in Sources */ = {isa = PBXBuildFile; fileRef = DA88488A1CBB037E00AB86E3 /* SMCalloutView.m */; };
CA86FF0E22D8D5A0009EB14A /* MGLNetworkConfigurationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CA86FF0D22D8D5A0009EB14A /* MGLNetworkConfigurationTests.m */; };
CA86FF0E22D8D5A0009EB14A /* MGLNetworkConfigurationTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = CA86FF0D22D8D5A0009EB14A /* MGLNetworkConfigurationTests.mm */; };
CA88DC3021C85D900059ED5A /* MGLStyleURLIntegrationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CA88DC2F21C85D900059ED5A /* MGLStyleURLIntegrationTest.m */; };
CA8FBC0921A47BB100D1203C /* MGLRendererConfigurationTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = CA8FBC0821A47BB100D1203C /* MGLRendererConfigurationTests.mm */; };
CAA69DA4206DCD0E007279CD /* Mapbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA4A26961CB6E795000B7809 /* Mapbox.framework */; };
Expand Down Expand Up @@ -762,8 +762,8 @@
DAF2571B201901E200367EF5 /* MGLHillshadeStyleLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = DAF25718201901E200367EF5 /* MGLHillshadeStyleLayer.h */; settings = {ATTRIBUTES = (Public, ); }; };
DAF2571C201901E200367EF5 /* MGLHillshadeStyleLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = DAF25718201901E200367EF5 /* MGLHillshadeStyleLayer.h */; settings = {ATTRIBUTES = (Public, ); }; };
DAF25720201902BC00367EF5 /* MGLHillshadeStyleLayerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = DAF2571F201902BB00367EF5 /* MGLHillshadeStyleLayerTests.mm */; };
DD0902A91DB1929D00C5BDCE /* MGLNetworkConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.m */; };
DD0902AA1DB1929D00C5BDCE /* MGLNetworkConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.m */; };
DD0902A91DB1929D00C5BDCE /* MGLNetworkConfiguration.mm in Sources */ = {isa = PBXBuildFile; fileRef = DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.mm */; };
DD0902AA1DB1929D00C5BDCE /* MGLNetworkConfiguration.mm in Sources */ = {isa = PBXBuildFile; fileRef = DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.mm */; };
DD0902AB1DB192A800C5BDCE /* MGLNetworkConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
DD4823751D94AE6C00EB71B7 /* fill_filter_style.json in Resources */ = {isa = PBXBuildFile; fileRef = DD4823721D94AE6C00EB71B7 /* fill_filter_style.json */; };
DD4823761D94AE6C00EB71B7 /* line_filter_style.json in Resources */ = {isa = PBXBuildFile; fileRef = DD4823731D94AE6C00EB71B7 /* line_filter_style.json */; };
Expand Down Expand Up @@ -1216,7 +1216,7 @@
CA5E5042209BDC5F001A8A81 /* MGLTestUtility.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = MGLTestUtility.h; path = ../../darwin/test/MGLTestUtility.h; sourceTree = "<group>"; };
CA65C4F721E9BB080068B0D4 /* MGLCluster.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLCluster.h; sourceTree = "<group>"; };
CA6914B420E67F50002DB0EE /* MGLAnnotationViewIntegrationTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLAnnotationViewIntegrationTests.mm; path = "Annotation Tests/MGLAnnotationViewIntegrationTests.mm"; sourceTree = "<group>"; };
CA86FF0D22D8D5A0009EB14A /* MGLNetworkConfigurationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLNetworkConfigurationTests.m; sourceTree = "<group>"; };
CA86FF0D22D8D5A0009EB14A /* MGLNetworkConfigurationTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLNetworkConfigurationTests.mm; sourceTree = "<group>"; };
CA88DC2F21C85D900059ED5A /* MGLStyleURLIntegrationTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLStyleURLIntegrationTest.m; sourceTree = "<group>"; };
CA8FBC0821A47BB100D1203C /* MGLRendererConfigurationTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLRendererConfigurationTests.mm; path = ../../darwin/test/MGLRendererConfigurationTests.mm; sourceTree = "<group>"; };
CAAA65D72321BBA900F08A39 /* MGLTestAssertionHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = MGLTestAssertionHandler.h; path = ../../darwin/test/MGLTestAssertionHandler.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1470,7 +1470,7 @@
DAFEB3762093AE6800A86A83 /* ko */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = ko; path = ko.lproj/Foundation.stringsdict; sourceTree = "<group>"; };
DAFEB3772093AE7900A86A83 /* ko */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ko; path = ko.lproj/Localizable.strings; sourceTree = "<group>"; };
DAFEB3782093AE9200A86A83 /* ko */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = ko; path = ko.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGLNetworkConfiguration.m; sourceTree = "<group>"; };
DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLNetworkConfiguration.mm; sourceTree = "<group>"; };
DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLNetworkConfiguration.h; sourceTree = "<group>"; };
DD4823721D94AE6C00EB71B7 /* fill_filter_style.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = fill_filter_style.json; sourceTree = "<group>"; };
DD4823731D94AE6C00EB71B7 /* line_filter_style.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = line_filter_style.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2096,6 +2096,7 @@
9658C154204761FC00D8A674 /* MGLMapViewScaleBarTests.m */,
076171C22139C70900668A35 /* MGLMapViewTests.m */,
9686D1BC22D9357700194EA0 /* MGLMapViewZoomTests.mm */,
CA86FF0D22D8D5A0009EB14A /* MGLNetworkConfigurationTests.mm */,
1F95931C1E6DE2E900D5B294 /* MGLNSDateAdditionsTests.mm */,
96036A0520059BBA00510F3D /* MGLNSOrthographyAdditionsTests.m */,
DAE7DEC11E245455007505A6 /* MGLNSStringAdditionsTests.m */,
Expand All @@ -2112,7 +2113,6 @@
357579811D502AD4000B822E /* Styling */,
409F43FB1E9E77D10048729D /* Swift Integration */,
4031ACFD1E9FD26900A3EA26 /* Test Helpers */,
CA86FF0D22D8D5A0009EB14A /* MGLNetworkConfigurationTests.m */,
);
name = "SDK Tests";
path = test;
Expand Down Expand Up @@ -2164,7 +2164,7 @@
927FBCFE1F4DB05500F8BF1F /* MGLMapSnapshotter.mm */,
DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */,
1F2B94BF221636D800210640 /* MGLNetworkConfiguration_Private.h */,
DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.m */,
DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.mm */,
3EA9337830C7738BF7F5493C /* MGLRendererConfiguration.h */,
3EA931BC4F087E166D538F21 /* MGLRendererConfiguration.mm */,
92F2C3EC1F0E3C3A00268EC0 /* MGLRendererFrontend.h */,
Expand Down Expand Up @@ -3300,7 +3300,7 @@
DA2DBBCE1D51E80400D38FF9 /* MGLStyleLayerTests.m in Sources */,
DA35A2C61CCA9F8300E826B2 /* MGLCompassDirectionFormatterTests.m in Sources */,
DAE7DEC21E245455007505A6 /* MGLNSStringAdditionsTests.m in Sources */,
CA86FF0E22D8D5A0009EB14A /* MGLNetworkConfigurationTests.m in Sources */,
CA86FF0E22D8D5A0009EB14A /* MGLNetworkConfigurationTests.mm in Sources */,
4085AF091D933DEA00F11B22 /* MGLTileSetTests.mm in Sources */,
DAEDC4341D603417000224FF /* MGLAttributionInfoTests.m in Sources */,
1F7454A91ED08AB400021D39 /* MGLLightTest.mm in Sources */,
Expand Down Expand Up @@ -3418,7 +3418,7 @@
40834BF01FE05E1800C1BD0D /* MMELocationManager.m in Sources */,
DA8848281CBAFA6200AB86E3 /* MGLShape.mm in Sources */,
DA35A2B31CCA141D00E826B2 /* MGLCompassDirectionFormatter.m in Sources */,
DD0902A91DB1929D00C5BDCE /* MGLNetworkConfiguration.m in Sources */,
DD0902A91DB1929D00C5BDCE /* MGLNetworkConfiguration.mm in Sources */,
35D13AB91D3D15E300AFB4E0 /* MGLStyleLayer.mm in Sources */,
74CB5EB3219B252C00102936 /* MGLStyleLayerManager.mm in Sources */,
DA35A2CB1CCAAAD200E826B2 /* NSValue+MGLAdditions.m in Sources */,
Expand Down Expand Up @@ -3539,7 +3539,7 @@
9C6E282C22A981580056B7BE /* MGLMapboxEvents.m in Sources */,
DAA4E41F1CBB730400178DFB /* MGLMultiPoint.mm in Sources */,
96E6145C22CC169000109F14 /* MGLCompassButton.mm in Sources */,
DD0902AA1DB1929D00C5BDCE /* MGLNetworkConfiguration.m in Sources */,
DD0902AA1DB1929D00C5BDCE /* MGLNetworkConfiguration.mm in Sources */,
40834C041FE05E1800C1BD0D /* MMELocationManager.m in Sources */,
9C6E281F22A980AC0056B7BE /* CLLocation+MMEMobileEvents.m in Sources */,
DA35A2B41CCA141D00E826B2 /* MGLCompassDirectionFormatter.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion platform/ios/sdk-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"platform/ios/vendor/mapbox-events-ios/MapboxMobileEvents/MMELocationManager.m",
"platform/darwin/src/MGLShape.mm",
"platform/darwin/src/MGLCompassDirectionFormatter.m",
"platform/darwin/src/MGLNetworkConfiguration.m",
"platform/darwin/src/MGLNetworkConfiguration.mm",
"platform/darwin/src/MGLStyleLayer.mm",
"platform/darwin/src/MGLStyleLayerManager.mm",
"platform/darwin/src/NSValue+MGLAdditions.m",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>
#import "MGLNetworkConfiguration_Private.h"
#include <mbgl/storage/network_status.hpp>

@interface MGLNetworkConfigurationTests : XCTestCase
@end
Expand Down Expand Up @@ -40,4 +41,22 @@ - (void)testAccessingEventsFromMultipleThreads {

[self waitForExpectations:@[expectation] timeout:10.0];
}

- (void)testStopsRequests {
auto networkStatus = mbgl::NetworkStatus::Get();
BOOL offline = networkStatus == mbgl::NetworkStatus::Status::Offline ? YES : NO;
XCTAssertEqual([MGLNetworkConfiguration sharedManager].stopsRequests, offline);

[MGLNetworkConfiguration sharedManager].stopsRequests = YES;
XCTAssertTrue([MGLNetworkConfiguration sharedManager].stopsRequests);

networkStatus = mbgl::NetworkStatus::Get();
offline = networkStatus == mbgl::NetworkStatus::Status::Offline ? YES : NO;
// TODO: When the double linking framework fix lands this should be replaced to an equal assert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's link to an issue here that gives more detail. Perhaps #15724 ?

XCTAssertNotEqual([MGLNetworkConfiguration sharedManager].stopsRequests, offline);

// Resets to the default value in order to let other tests run properly
[MGLNetworkConfiguration sharedManager].stopsRequests = NO;

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests look good, thanks! Have you considered implementing tests of the offline/network functionality — e.g., does enabling this property actually result in no network requests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that this will stop tile and related requests, but other network activity would still occur.

@end
Loading