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

Conversation

fabian-guerra
Copy link
Contributor

@fabian-guerra fabian-guerra commented Sep 17, 2019

Fixes #7443.

Adds the iOS equivalent of Android's setConnected flag. Unlike Android iOS don't have a ConnectivityManager to turn on/off the network requests. The network implementation checks if the stopsRequests flag is set to YES the core network library will stop making request. This enforce the usage of any previously cached data.

@fabian-guerra fabian-guerra added feature iOS Mapbox Maps SDK for iOS labels Sep 17, 2019
@fabian-guerra fabian-guerra requested a review from a team September 17, 2019 21:40
@fabian-guerra fabian-guerra self-assigned this Sep 17, 2019
@fabian-guerra fabian-guerra added the needs changelog Indicates PR needs a changelog entry prior to merging. label Sep 17, 2019
@julianrex julianrex added this to the release-ristretto milestone Sep 17, 2019
@julianrex
Copy link
Contributor

FYI @tobrun

@fabian-guerra fabian-guerra force-pushed the fabian-network-pause-14469 branch 2 times, most recently from 1ce830a to 78fd9d6 Compare September 18, 2019 18:21
@julianrex
Copy link
Contributor

I see mbgl::OnlineFileSource::Impl has an online instance variable - though it looks from code comments that this is really for testing only.

I'm wondering if in addition to setOnlineStatus we could add a getOnlineStatus, that uses the proposed MGLNetworkConfiguration.stopsRequests property?

@tmpsantos would you mind adding your 2 cents here?

@julianrex julianrex requested review from tmpsantos and removed request for a team and 1ec5 September 18, 2019 22:20
@@ -229,6 +229,10 @@ void cancel() {
std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource, Callback callback) {
auto request = std::make_unique<HTTPRequest>(callback);
auto shared = request->shared; // Explicit copy so that it also gets copied into the completion handler block below.

if ([MGLNetworkConfiguration sharedManager].stopsRequests) {
return std::move(request);
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem with this implementation is when you resume, it will not re-request the previously requested (but not fulfilled) network requests.

We have an API for this: mbgl::NetworkStatus::Set(NetworkStatus::Status::Offline).

@tobrun
Copy link
Member

tobrun commented Sep 19, 2019

The Mapbox#setConnected(true) was introduced for other reasons. We had a client that had a device without an internet connection and we had to force an a connected state for that device (normally we rely on the connectivity state change listener that Android OS exposes).

The api that @tmpsantos mentiones we expose on the Map object (NativeMapView#setReachable(boolean). Imo we should update the Mapbox#setConnected to align with the API on the map object.

Copy link
Contributor

@friedbunny friedbunny left a comment

Choose a reason for hiding this comment

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

We have to add tests for this before it can be accepted.


- (BOOL)stopsRequests {
auto status = mbgl::NetworkStatus::Get();
if (status == 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.

This can just be return (status == mbgl::NetworkStatus::Status::Offline);

@@ -66,6 +67,19 @@ - (NSURLSessionConfiguration *)defaultSessionConfiguration {
return sessionConfiguration;
}

- (void)setStopsRequests:(BOOL)stopsRequests {
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;
}

@@ -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.

networkStatus = mbgl::NetworkStatus::Get();
offline = networkStatus == mbgl::NetworkStatus::Status::Offline ? YES : NO;
XCTAssertEqual([MGLNetworkConfiguration sharedManager].stopsRequests, offline);
}
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.

@fabian-guerra fabian-guerra removed needs changelog Indicates PR needs a changelog entry prior to merging. needs tests labels Oct 7, 2019
Copy link
Contributor

@julianrex julianrex left a comment

Choose a reason for hiding this comment

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

Just a couple of notes.

@@ -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.

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


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 ?

@degtiarev
Copy link

degtiarev commented Nov 4, 2019

It crashes the app when I am trying to use it:
MGLNetworkConfiguration.sharedManager.stopsRequests = false

@julianrex
Copy link
Contributor

@fabian-guerra @knov Closing this PR in preference of mapbox/mapbox-gl-native-ios#217, which tracks whether this needs to be ported.

@julianrex julianrex closed this Mar 17, 2020
@fabian-guerra
Copy link
Contributor Author

Continues in mapbox/mapbox-gl-native-ios#416

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature iOS Mapbox Maps SDK for iOS offline
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a way to disable network activity for maps
6 participants