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

[DO NOT MERGE] research work on #61 - #71

Closed
incanus wants to merge 19 commits into
masterfrom
issue61
Closed

incanus wants to merge 19 commits into
masterfrom
issue61

Conversation

@incanus

@incanus incanus commented Mar 7, 2014

Copy link
Copy Markdown
Contributor

Just opening an official pull for @wsnook's issue61 branch as a possible way to fix #61.

@incanus

incanus commented Mar 8, 2014

Copy link
Copy Markdown
Contributor Author

I haven't run this yet, but having a look over the code, here are my initial thoughts. All of this is meant as constructive criticism in order to make the best public API and most maintainable library we can have while solving the problems this is meant to address. I will run it now and see what else I can learn & understand about the design.

  • The "standard delegate" should probably be called something more like just plain "toolkit delegate" or "alternate delegate", since the intention is stuff outside of and beyond what MKMapViewDelegate does. What does "standard" mean? A standard use case? In what sort of instances doesn't it work? I understand what it's for, but I'm playing devil's advocate and thinking about user support questions.
  • It's also confusing to me initially that there are three kinds of delegates (standard, raster overlay, and simplestyle), plus your own map view delegate, that you have to understand and use, even minimally. Neither Mapkit nor even our iOS SDK has that many (one for MapKit, two for the SDK). Why does a piece of style have a delegate? Again, devil's advocate. It doesn't seem intuitive.
  • The real reason we have these, of course, is because of the async nature of requesting TileJSON, markers and GeoJSON, and raster tiles. We need to communicate that somehow to the user if we are to use this variety of APIs. Otherwise, IMHO it just seems complex.
  • Image quality-related things are tied up in a cache manager -- also seems confusing. It feels like we are working from the point of view of "because it needs to be that way" instead of "this is the easiest to use". Is the cache manager and/or MBXCacheManagerProtocol meant to be public API? This could have something to do with my confusion here.
  • On the topic of "managers" -- this also feels like a black box. User POV: "There is enough going on here that a "manager" is needed? What does it manage? Why doesn't the map view subclass, which I thought is what this library basically was, manage this itself?" It also manages the user agent configuration -- why wouldn't this happen on the map view? What does a "cache manager" have to do with the user agent, other than the fact that they both touch downloads at separate points in time?
  • The raster tile -loadTileAtPath: doesn't run it's result() completion when called, but does so asynchronously at a later point subject to a background concurrency queue it doesn't have direct control over. Normally, it runs asynchronously from a background network-related serial queue a là NSURLConnection or NSURLSession. I'm worried in this case that MKMapView could self-throttle if these get backed up and stop requesting them in a timely manner. This tends to happen in our SDK when many background network requests get backed up and is a weakness in our current renderer (again, talking about the SDK here). I'm thinking this could happen over here in this project since the spawning of concurrent background loads goes unchecked in a non-serial queue.
  • The fact that -mapViewDidFinishRenderingMap: always calls sweepCache says to me that the more maps you use, the longer your cache period, and the more the map view is used, the worse performance gets since a lot of filesystem hits will have to happen to check cache each time a map loads. This is as opposed to the current garbage collection-like probability model.
  • I think that -didLoadMapID: calling -setRegion: is a regression of allow proper setting of center/zoom #8.
  • The name MBXCacheManagerProtocol is odd because it uses the word "protocol" in its name -- this is non-standard. It should describe what it does, which is "proxying requests through the cache" and "being responsible for cleanup of the cache". Even though this is likely meant as private API, it's still cognitive overhead and complexity on the maintenance of the library. Edit: This should probably rightfully be called MBXCacheManagerDelegate, but that's... another delegate.

I will keep digging, because I don't totally understand the wins with this approach yet (though I'm sure they are there). I'm not sure that offsets the above but happy to be proven wrong / enlightened.

@incanus

incanus commented Mar 8, 2014

Copy link
Copy Markdown
Contributor Author

To put 90% of my concerns another way, consider what the documentation to these new classes would look like, even at a narrative level ("The standard delegate is for foo. Simplestyle is blah, and the simplestyle delegate is for bar" etc.

Then consider the primary use case for the library, which is to bring easy mapID integration into MapKit (and with it, markers and eventually other features) as well as easy offline use & cache management. Contrast that with the the number of classes you have to get your head around to make sure you are doing things right (setting up three delegates, at minimum, plus setting a property on a singleton manager instance). There seems like a disconnect. In my experience, a large majority of our iOS users don't even know what TileJSON is, what simplestyle is, and how the markers get automatically on the map, if they do at all.

@incanus

incanus commented Mar 8, 2014

Copy link
Copy Markdown
Contributor Author

Minor feedback / probing question: what happens when -didFailToLoadMapID: happens? The overlay is now useless and you must remove, destroy, and replace it to try again. We should probably have a retry method available to the user.

@incanus

incanus commented Mar 8, 2014

Copy link
Copy Markdown
Contributor Author

Love how well this handles the example cases, and the style of the testing app for showing that. Big win towards #47.

@ghost

ghost commented Mar 8, 2014

Copy link
Copy Markdown

@incanus Cool. First off, I'd recommend that you run the code to calibrate your sense of how well these ideas work. I'm expecting you'll be favorably impressed. This is a pretty radical take on changing how MBXMapKit works, but I think it's justified by the results on screen.

Without getting too deep into all those points, a potentially useful idea is that I did a lot of stuff based on three four primary goals:

  1. Strictly adhere to the existing semantics of the MKMapKit api (e.g. MKMapView is not responsible/required for instantiating MKTileOverlay instances)
  2. Fix the TileJSON loading problems (i.e. automatically replacing layers is not allowed)
  3. Don't repeat code
    [edit: 4. Teach people by example how to make effective use of MKMapKit in the view controller]

The concept of these three delegate protocols is probably the key to unlocking my way of thinking about the issue61 branch:

  • MKMapViewDelegate
  • MBXRasterTileOverlayDelegate
  • MBXSimplestyleDelegate

Those protocols fix the tileJSON problems from #36.

MBXStandardDelegate could be called anything. The important part is that it provides a well behaving implementation for the three delegate protocols I mentioned above. I factored it out into its own class because it's needed by both of the demo apps. The Mac app delegate and the iOS view controller both need that exact same code to implement the "standard" behavior of: the correct view for an MBXPointAnnotation is an ___ , the renderer for an MBXRasterTileOverlay is a ___ , the correct behavior in response to a simplestyle point being parsed is adding it to the map, etc.

My goal was to provide a less restrictive way of implementing the delegate proxying functionality without requiring a bunch of boilerplate in people's view controllers. By less restrictive, I mean all someone needs to do for implementing alternate behaviors is make a view controller like

@interface MBXxViewController : UIViewController <MKMapViewDelegate,
         MBXRasterTileOverlayDelegate, MBXSimplestyleDelegate>

and then implement those protocols, and do whatever they want. They could also subclass MBXStandardDelegate (or a suitable renamed alternative) to override specific behaviors.

@incanus

incanus commented Mar 8, 2014

Copy link
Copy Markdown
Contributor Author

Cool. As I just commented, I love the demo. Maybe I need to chew on it. Will keep digging.

@ghost

ghost commented Mar 8, 2014

Copy link
Copy Markdown

Dunno if you saw my edit, but point number 4 is also a huge goal... it's possible to do really slick stuff with MKMapKit, but it requires you to do things skillfully at the view controller level in a way that matches your visual intent.

We don't know what people's intent will be, so trying to make assumptions in code is probably going to be counterproductive. However, we can demonstrate ways of realizing various intents in the sample app. The desire to teach effective practices is a key reason why I'm in favor of having the view controller / app delegate be more complicated (but not cluttered up with boilerplate).

@ghost

ghost commented Mar 8, 2014

Copy link
Copy Markdown

Also, I'm expecting that the whole MBXCacheManager thing will need to be gutted and re-implemented along the lines of #67 (comment).

@ghost

ghost commented Mar 8, 2014

Copy link
Copy Markdown

Minor feedback / probing question: what happens when -didFailToLoadMapID: happens? The overlay is now useless and you must remove, destroy, and replace it to try again. We should probably have a retry method available to the user.

MKMapView already provides the means to manipulate layers. Obviously not everybody knows about that, but I think education (sample apps, docs, links, etc ) is probably the best way to approach that lack of understanding.

Also, see @boundsj's comment about this: #24 (comment)

@ghost

ghost commented Mar 8, 2014

Copy link
Copy Markdown

The raster tile -loadTileAtPath: doesn't run it's result() completion when called ... I'm worried in this case that MKMapView could self-throttle if these get backed up and stop requesting them in a timely manner. This tends to happen in our SDK ...

Yes, that's every bit as bad as you describe, and it needs to change. It's there as a temporary expedient with the significant benefit being that it performs well for testing purposes and I didn't have to spend any time thinking about it while I was working on other more challenging aspects of the design. My intention is to re-implement the network code with something which is based on NSURLCache, assuming that we decide to go forward with this general approach to re-factoring.

The fact that -mapViewDidFinishRenderingMap: always calls sweepCache says to me that ...

My thinking on this is that the current implementation needs to go away and get replaced with something that uses NSURLCache. In that case, NSURLCache would be responsible for such things on its own.

I think that -didLoadMapID: calling -setRegion: is a regression of #8.

Not to my way of looking at it, because that code only gets called if the developer explicitly chooses to set an MBXStandardDelegate as the delegate for their MKMapKit MKMapView. If they don't want the "standard" behavior, all they need to do is provide their own delegate by implementing the relevant protocols directly, or by creating a subclass of MBXStandardDelegate (which could be renamed to MBXDelegateToolbox, or whatever other name seems best).

edit: The whole MBXStandardDelegate thing could be avoided by copying and pasting the contents of MBXStandardDelegate.m into the Mac demo app's app delegate and the iOS app's view controller. That has the disadvantages of duplicating code and requiring boilerplate in view controllers, but perhaps that is a lesser evil than the potential to confuse people about why MBXStandardDelegate exists.

@incanus

incanus commented Mar 10, 2014

Copy link
Copy Markdown
Contributor Author

We can discuss more on voice, but have you considered simplifying all of the multiple delegate stuff by moving to a completion block pattern, much like Apple's frameworks like NSURLSession and others have done recently? Then, you just get a single handler for TileJSON fetches/inits, marker adds, etc., letting the developer respond, while keeping delegate-related things like customizing markers and overlays in a true, but single, delegate?

@ghost

ghost commented Mar 10, 2014

Copy link
Copy Markdown

have you considered simplifying all of the multiple delegate stuff by moving to a completion block pattern

Like we talked about, no, I haven't really considered that. However, one thing I really like about the delegate approach is that it's possible to break links between tile overlay layers the connection between tile overlays and view controllers by setting the tile overlay's delegate to nil from a view controller. To get a completion block to cancel its code might be a bit more complicated.

The example I mentioned earlier was, say an app tries to load a mapID with TileJSON that centers the map on Washington DC, and for some reason that connection takes a long time. Suppose that in the mean time, the person running the app gets impatient and switches to a map of Los Angeles (which loads immediately). What happens if the completion block for the first map tries to center the MKMapView on Washington DC 15 seconds after the person running the app has started scrolling around Los Angeles?

The existing code in my demo app will fully disconnect the first tile overlay layer, so that it's not possible for it to interfere with the MKMapView once it has been removed. Any other mechanism which supports that same capability without adding any significant additional complexity would be perfectly acceptable as far as I'm concerned.

@incanus

incanus commented Mar 11, 2014

Copy link
Copy Markdown
Contributor Author

Ok, I've spent some quality time with the branch and have much more substantive feedback over my last round.

I will note upfront that, like before, I do focus a bit more on the public API (though not at the expense of the actual workings), but here's why. When compared to Apple's default toolkit, IMHO we need every detail sweated on the API side to compare favorably to their free alternative. You should be able to glean 50% or more of the usage just from scanning the headers and/or header-based docs. This comes across in method names, argument wording, various and designated initializers, and protocol methods, not to mention the volume of public API. We will have detailed docs, guides, sample apps, etc., but we have to make things as frictionless, as well as Apple-like, as we can, despite working with different constraints (styling options, server-based configuration, increased flexibility). So I put a great deal of value into these first impressions by way of API "self-documentation". It should be every bit as carefully considered as the inner workings of the classes.

On the functionality side, now that I've spent some time with this refactor, I greatly appreciate the planning, foresight, and overall elegance of the refactor. And I can see how the goal of leaving MKMapView itself unsullied is met while still giving loads of customizability with sane defaults.

Larger stuff

  • Regarding setting the delegate(s) to nil to effectively "cancel" the async download operations we have going on, I can see how completion handlers would complicate this. But one thing I've noticed is that this doesn't actually cancel anything, it just prevents asynchronous changes from happening (e.g. a map getting recentered after TileJSON is downloaded which describes a server-set center). Should we go all the way and consider a dedicated NSOperationQueue with cancellation? Again, I say this less out of concern for actual significant resources used (e.g., failing to cancel a Simplestyle parse after download -- not a big deal resource-wise) and more out of perception: setting a delegate to nil doesn't say "cancel" to me intuitively, and outright saying "cancel" (e.g. [_simplestyle cancel]) is a lie if it doesn't really cancel. Curious to hear your thoughts on this @wsnook. I've never seen a piece of Apple documentation that says "to cancel operation x, set the delegate to nil".

Smaller stuff

  • The inhibitTileJSON naming paradigm strikes me as odd since we're turning an object into a (negative) action. How about updateMapRegionFromTileJSON, since that's more descriptive of what happens (or doesn't)? Default value is YES.
  • What if we just call the default MBXStandardDelegate instead MBXMapViewDelegate? This lines up with MKMapViewDelegate, but more importantly, I think anyone used to working with MapKit or similar libraries will expect 1) there to be a single delegate and 2) that delegate to take care of all of the "map stuff". This can continue to adopt the data and overlay delegates by default, but strikes me naming-wise as a bit clearer.
  • Related question on MBXStandardDelegate: am I right in understanding that you'd either subclass or completely replace this if you wanted to do any customization of normal MKMapViewDelegate callbacks, since this only implements certain ones and doesn't do any proxying in the style of the current library?
  • Consider renaming MBXSimplestyle to MBXDataAnnotationCollection. Simplestyle is the payload (akin to PNG or JPEG). The world Simplestyle isn't even mentioned in the editor interface; it's Data. What we are doing, however, is downloading your map data and adding map annotations (in Apple parlance) as a collection. This also conceptually covers the markers that we have now, plus the lines and polygons we implement later.
  • Consider renaming default -init for Simplestyle and raster overlays to be more descriptive of why they are async. We voiced on this, but this pattern is very different from any other map provider and I feel a certain amount of explanation or conditioning is required to set the stage. Consider -[MBXDataAnnotationCollection initByDownloadingStyleForMapID:] and -[MBXRasterTileOverlay initByDownloadingTileJSONForMapID:].
  • Related to the previous point, we probably want to make instance variables such as imageQuality immutable so as to not create unpredictable cache conditions. So we should add a second initializer e.g. -[MBXRasterTileOverlay initByDownloadingTileJSONForMapID:imageQuality:].
  • Also in the instance variable department, centerZoom and center should probably be readonly publicly.

Minor stuff

  • The cache manager prepareCacheForMapID: is actually private API, so if classes like MBXRasterTileOverlay are "in the know", they should have a private category on MBXCacheManager for the method.
  • Consider renaming the MBXCacheManagerProtocol method patterns with -proxy... to something like -retrieveTileJSONForMapID:. Specifically considering the project's history with proxying, as well as the existence of NSProxy as well as proxy servers, this could be confusing otherwise.
  • Consider simplifying a couple cache manager method names like -clearDataForMapID: and -clearRasterTilesForMapID:.

@ghost

ghost commented Mar 11, 2014

Copy link
Copy Markdown

@incanus I like that. It will take a while to digest all of that, but my first impression is, this is good:

we have to make things as frictionless, as well as Apple-like, as we can, despite working with different constraints

and specific suggestions like

Consider renaming MBXSimplestyle to MBXDataAnnotationCollection. ... downloading your map data and adding map annotations (in Apple parlance) as a collection.

are very helpful. My experience with C goes back to about 1995, while my experience with Apple API's goes back to late 2012, so it's a lot easier for me to make things function well than to know what someone who has spent years in Cocoa would expect them to be called. I agree that nailing both of those things is important.

@ghost

ghost commented Mar 11, 2014

Copy link
Copy Markdown

Some further thoughts...

Larger stuff
Regarding setting the delegate(s) to nil to effectively "cancel" the async download operations we have going on...

I think it's important to make the distinction between a) conventions which are useful for working around bugs in MKMapKit at the view controller level, and b) things which implement the internals of the MXMapKit API.

To my mind, the whole setting the delegate to nil thing falls mostly into the first category as it is intended to address more or less the same crashing issue which is discussed at #26 and #31. We never came up with a very satisfactory solution to that: #26 (comment). As such, the goal in my mind isn't so much to "cancel" anything. Rather the objective is to do what's necessary to avoid bugs in MKMapKit which can crash the app or produce serious visual glitches.

That said, I will spend some more time looking for alternative approaches. I think we may have no choice but to do some kinda ugly stuff in order to avoid bugs, but I think there is also potentially a lot of room for making things cleaner and clearer.

Smaller stuff

  1. inhibitTileJSON is kinda redundant and should probably just be removed as opposed to renamed. In any case, I'm sure there's a better way to implement its functionality (i.e. preventing TileJSON from being downloaded as opposed to just ignoring it once it has been downloaded).

  2. Similarly, I think the problem with MBXStandardDelegate is that code implementing three different delegate protocols does not belong on the inside of the API. The reason this got started is that it annoyed me to have a bunch of common code between the OS X and iOS sample apps, so I factored it out and stuck it in MBXStandardDelegate. Now I'm convinced that was a bad move.

    The problem is that I blurred the line between example code showing how to use the MBXMapKit API and implementation code which is the MBXMapKit API. To my mind the solution is to get clear about that distinction and move the implementations of delegate protocols back to the OS X app delegate and iOS view controller where they naturally belong. That will make it clearer that our delegate protocol implementations are just examples which can and should be changed as needed, as opposed to having that code be an official and supported part of the API.

  3. Renaming MBXSimplestyle sounds good.

  4. Better initializers and readonly properties sound good.

Minor stuff
...cache...

I think the whole caching system needs to be re-worked to be based on an NSURLCache performance cache alongside of a new class which provides persistent offline storage for map tiles. It should be possible for MBXCacheManagerProtocol and MBXCacheManager to go away once all the URL generating code gets moved back out to MBXRasterTileOverlay and MBXSimplestyle.

@incanus

incanus commented Mar 11, 2014

Copy link
Copy Markdown
Contributor Author

This all sounds pretty good.

I think the whole caching system needs to be re-worked

Just keep in mind that the biggest win we have with this project over the SDK is really simple configuration, which we want to continue.

@ghost

ghost commented Mar 11, 2014

Copy link
Copy Markdown

really simple configuration

Yeah, I think that would happen with what I have in mind. I need to see what the code looks like once I start playing with it, but I'm imagining the existing surface area of the API in my issue61 branch getting drastically reduced as a consequence of incorporating NSURLCache.

@ghost ghost mentioned this pull request Mar 11, 2014
5 tasks done
@ghost

ghost commented Mar 11, 2014

Copy link
Copy Markdown

@incanus Are you cool with closing this in favor of #73?

@incanus

incanus commented Mar 11, 2014

Copy link
Copy Markdown
Contributor Author

Ayup.

@ghost ghost closed this Mar 11, 2014
@ghost ghost mentioned this pull request Apr 23, 2014
10 tasks done
@ghost
ghost deleted the issue61 branch May 2, 2014 16:01
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discuss how to re-factor TileOverlay layers

2 participants