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

[ios] Cache Management API #14978

Merged
merged 45 commits into from
Jul 16, 2019
Merged

[ios] Cache Management API #14978

merged 45 commits into from
Jul 16, 2019

Conversation

jmkiley
Copy link
Contributor

@jmkiley jmkiley commented Jun 20, 2019

iOS version of #14962

Tracking my progress

Core Method iOS Method/property implementation iosapp example tests docs benchmarking
invalidateAmbientCache() - invalidateAmbientCache X X X X
invalidateRegion(int64_t regionID) - invalidateOfflineRegionForPack: X X X X
maximumAmbientCacheSize(uint64_t) setMaximumAmbientCacheSize X X X X
clearAmbientCache() - clearAmbientCache X X X X
resetDatabase() - resetDatabase X X X X

To Do:

  • Implementation
  • Document the new API.
  • Revise existing documentation to clarify that MGLOfflineStorage manages both offline packs - and the ambient cache
  • Add a demo to iosapp
  • Add tests
    - [ ] Benchmark/time the methods. Tailwork.

cc @langsmith @chloekraw

@jmkiley jmkiley self-assigned this Jun 20, 2019
@chloekraw chloekraw mentioned this pull request Jun 21, 2019
7 tasks
@chloekraw chloekraw added the iOS Mapbox Maps SDK for iOS label Jun 26, 2019
@chloekraw chloekraw added this to the release-p milestone Jun 28, 2019
@jmkiley jmkiley force-pushed the jmkiley-cache-management-api branch from 657c28b to 21ff217 Compare June 28, 2019 22:48
@jmkiley
Copy link
Contributor Author

jmkiley commented Jun 28, 2019

I am currently getting this warning when I call resetDatabase in both iosapp and tests:

Log::Warning(Event::Database, "Removing existing incompatible offline database");

Not sure if I'm missing something with the current Objective-C method.

cc @tmpsantos

@jmkiley
Copy link
Contributor Author

jmkiley commented Jul 3, 2019

I updated the iOS docs around setMaximumAmbientCacheSize to match the Android docs, which state that the maximum size should be set before the map and style have been loaded.

I am also still seeing the issue I commented about here, and am not sure how to handle a warning. It doesn't seem to be caught by the exception.
I was misreading the message and that was not the cause. 🤦‍♀

@jmkiley
Copy link
Contributor Author

jmkiley commented Jul 3, 2019

Thank you @fabian-guerra for helping me troubleshoot the issue with -resetDatabaseWithCompletionHandler. The issue was that I had dispatch_async(dispatch_get_main_queue(), [&, completion, error](void) { instead of dispatch_async(dispatch_get_main_queue(), ^{. It does not seem to be crashing now 🎉 .

@jmkiley jmkiley marked this pull request as ready for review July 4, 2019 00:10
@jmkiley jmkiley requested review from a team, fabian-guerra and julianrex July 4, 2019 00:10
platform/darwin/src/MGLOfflineStorage.h Outdated Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Outdated Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Outdated Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Outdated Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Outdated Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Outdated Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Outdated Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.mm Outdated Show resolved Hide resolved
@chloekraw chloekraw self-requested a review July 15, 2019 20:44
platform/darwin/src/MGLOfflineStorage.h Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.h Show resolved Hide resolved
the latest version on the server are updated.

This is more efficient than cleaning the cache because valid local tiles will
not be downloaded again.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say this is not quite accurate: this method clears the entire ambient cache by marking tiles as invalid. Currently there's no way to specify which specific tiles are valid or not; all tiles in the ambient cache are considered invalid and re-downloaded if this method is triggered.

This is more efficient than deleting the ambient cache or the offline database. I also recommend referencing the specific APIs for those other two methods in parentheses so it's clear that this is the most preferred method for clearing the ambient cache.

It might even be worth making that the last line in this section of documentation:

"This is the recommended method for clearing the ambient cache. It's the most efficient and least computationally expensive method."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, sounds good. I was going off the core documentation. It sounds like the behavior doesn't match this?

* Forces Mapbox GL Native to revalidate resources stored in the ambient
* cache with the tile server before using them, making sure they
* are the latest version. This is more efficient than cleaning the
* cache because if the resource is considered valid after the server
* lookup, it will not get downloaded again.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh, thanks. I'll try to sync with @tmpsantos tomorrow to clear things up. cc @zugaldia

- (void)invalidateAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable error))completion;

/**
Clears the ambient cache, freeing storage space. This method does not
Copy link
Contributor

Choose a reason for hiding this comment

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

Change to "Deletes the ambient cache."

Let's leave out "freeing storage space" -- developers will be more incentivized to use this method than they should be.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also mention the reinitialization of the ambient cache here?

Copy link
Contributor Author

@jmkiley jmkiley Jul 16, 2019

Choose a reason for hiding this comment

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

Per an earlier review from @friedbunny, I would like to keep it as clears so that we are starting the documentation with the verb from method name. How about Clears the ambient cache by deleting resources.?
Edited to add: I don't believe this method reinitializes the ambient cache. It removes resources from the cache, but the cache.db is not deleted.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah okay, fair enough. The core documentation consistently uses "erase" as the verb but the method name does start with clear, and I understand the desire to keep those consistent. I'm also ok with using "erase" as a verb instead of "delete" to match the core documentation:

Clears the ambient cache by erasing resources from the ambient cache.

FWIW I think the rest of the core documentation is good to copy and paste over to the platform side:

This operation can be potentially slow because it will trigger a VACUUM on SQLite, forcing the database to move pages on the filesystem.

Resources overlapping with offline regions will not be affected by this call.

Copy link
Contributor

Choose a reason for hiding this comment

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

Core and Android use clearAmbientCache (unless I'm looking at the wrong thing).

platform/darwin/src/MGLOfflineStorage.h Outdated Show resolved Hide resolved
- (void)setMaximumAmbientCacheSize:(NSUInteger)cacheSize withCompletionHandler:(void (^)(NSError *_Nullable error))completion;

/**
Invalidates the ambient cache. This method checks that the tiles in the
Copy link
Contributor

@chloekraw chloekraw Jul 16, 2019

Choose a reason for hiding this comment

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

Suggestion: "Clears the ambient cache by marking its resources as invalid. This method checks that the tiles in the ambient cache match those from the server. If the local tiles do not match, the tiles in the ambient cache are re-downloaded."

}

_mbglFileSource->invalidateOfflineRegion(region, [&](std::exception_ptr exception) {
if (exception) {
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 move inside the if (completion) below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I move it inside if (completion), will it only happen if there is a completion handler?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, but you're only creating the error object, and that only gets used in the completion block. (Same thing in the methods below too.)

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.

Thinking more about error codes, can we test that bad input/etc throw the expected errors?

platform/darwin/src/MGLOfflineStorage.mm Outdated Show resolved Hide resolved
platform/darwin/src/MGLOfflineStorage.mm Outdated Show resolved Hide resolved
@jmkiley
Copy link
Contributor Author

jmkiley commented Jul 16, 2019

Thanks @friedbunny, @chloekraw, @julianrex, and @zugaldia!

@friedbunny That would be good. Could I leave testing bad input as tailwork to do this week?

I think I addressed the feedback related to this specific PR. If there is anything else, please let me know. Maybe @chloekraw can ticket out questions about docs that are consistent cross-platform, so those can be addressed later this week?

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.

Thanks for tackling this, it’s a long overdue API. 😁 I’m onboard with merging this now and then backfilling tests before the final picklejuice release.

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.

This looks good to merge. Just my minor comment, but that's small fry and can wait :)

}

_mbglFileSource->invalidateOfflineRegion(region, [&](std::exception_ptr exception) {
if (exception) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, but you're only creating the error object, and that only gets used in the completion block. (Same thing in the methods below too.)

@jmkiley jmkiley merged commit 9d8839c into master Jul 16, 2019
@jmkiley jmkiley deleted the jmkiley-cache-management-api branch July 16, 2019 21:58
@jmkiley jmkiley added the needs changelog Indicates PR needs a changelog entry prior to merging. label Jul 16, 2019
@chloekraw chloekraw removed the needs changelog Indicates PR needs a changelog entry prior to merging. label Jul 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
iOS Mapbox Maps SDK for iOS offline
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants