couchbaselabs/CouchCocoa — pull request #40 by pegli (2012-08-31 17:18:21+00:00)
Title: CouchDesignDocument stale view cache

This pull fixes an edge case with querying a view before the view's design document is fetched via replication. Here's what my app is doing:

1. Start a pull replication with a remote database. The remote database has a design doc named `_design/books`.
1. While replication is running, get the design doc with `ddoc = [CouchDatabase designDocumentWithName:@"books"]`. The ddoc doesn't exist locally yet, so it is created.
1. Calling `[ddoc mapFunctionOfViewNamed:@"by_author"]` returns null, but this populates the `_views` cache in CouchDesignDocument.
1. Replication finishes and the local design document is saved. In CouchDesignDocument, the `_viewsRevisionID` and `self.currentRevisionID` are equal and null, so the view cache is not cleared.
1. Subsequent calls to `mapFunctionOfViewNamed` continue to return null even though there is a map function.

If I restart the app at this point, the map function is returned from CouchDesignDocument as expected. I wasn't able to figure out why the currentRevisionID of the design document wasn't updated during the replication process. My workaround was to not cache the views for a design document unless that document has a non-null `_viewRevisionID`.

This probably hasn't come up before because most people aren't using map and filter functions stored in a design document due to the restriction on downloadable code in an iOS app. My code provides a JavaScript view compiler, so it is important to me to be able to replicate ddoc view functions. Let me know what you think or if you have any alternative suggestions for a workaround. Thanks!