mapbox/mapbox-ios-sdk — pull request #292 by RubioV (2013-08-13 11:22:04+00:00)
Title: Moved background caching from RMTileCache to RMDatabaseCache

This pull request fixes issues https://github.com/mapbox/mapbox-ios-sdk/issues/281 and https://github.com/mapbox/mapbox-ios-sdk/issues/284.

The move of background caching to `RMDatabaseCache` was pretty straightforward. But this is still quite a significant change, so I'll be happy to discuss if there are any questions. Below is an overview of the changes.

**nil passed to inCache parameter when requesting image from tile source**
Since we are no longer performing this as part of `RMTileCache`, it will not be available to look for pre-existing cached images. As a result, in `RMDatabaseCacheDownloadOperation`, images are requested from the tile source by:
`UIImage *image = [_source imageForTile:_tile inCache:nil];`
Passing `nil` to the `inCache` parameter has two effects:
1. We will not use an `RMTileCache` to look for images first, they are always downloaded fresh. This could waste some bandwidth if the tiles have been previously downloaded into another cache, but I think the cleaner design is worth the tradeoff.
2. Passing `nil` to this parameter results in `nil` being messaged (when the tile source attempts to retrieve the images from the cache). This isn't a problem in itself, but we should make sure that tile sources can handle this.

**Fix for exceeding kWriteQueueLimit**
Before the changes in this commit, I noticed that background caching was frequently causing the `kWriteQueueLimit` to be exceeded, meaning the tile image would get downloaded, but not written to the database. This was often quite severe, sometimes resulting in more than 80% of images to be discarded. Because background download operations already live on their own `NSOperationQueue`, there is no need to again perform the tile saving on a separate queue. So I added another method:
`- (void)addImageAndWait:(UIImage *)image forTile:(RMTile)tile withCacheKey:(NSString *)aCacheKey`
This is used by the download operations to save the image to the cache, fixing the problem of skipped tiles.

**Gracefully skip existing tiles**
A boolean property, `tileExisted`, was added to `RMDatabaseCacheDownloadOperation` to indicate whether the tile was already in the cache. This is used to better support resuming of download operations, the delegate will only be notified of new tiles which have been downloaded. It is also using the new `containsTile:withCacheKey:` method which should provide lower overhead than `cachedImage:withCacheKey:`.

**Additional changes**
- Added `withIdentifier:(id)identifier` parameter to various background caching methods, an arbitrary object which will be provided back to the delegate to identify the download operation.
- Added `readOnly`, `imageType` and `jpegQuality` properties to `RMDatabaseCache`.