Couchbase Lite
Objective-C API for iOS and Mac OS
CBLView Class Reference

Detailed Description

A "view" in a CouchbaseLite database – essentially a persistent index managed by map/reduce.

The view can be queried using a CBLQuery.

Inheritance diagram for CBLView:

Instance Methods

(BOOL) - setMapBlock:reduceBlock:version:
 Defines a view's functions. More...
 
(BOOL) - setMapBlock:version:
 Defines a view that has no reduce function. More...
 
(void) - updateIndex
 Updates the view's index, then returns YES if the index changed or NO if it didn't. More...
 
(void) - updateIndexAsync:
 Asynchronously updates the view's index. More...
 
(void) - deleteIndex
 Deletes the view's persistent index. More...
 
(void) - deleteView
 Deletes the view, persistently. More...
 
(CBLQuery *) - createQuery
 Creates a new query object for this view. More...
 
(instancetype) - NS_UNAVAILABLE
 

Class Methods

(NSNumber *) + totalValues:
 Utility function to use in reduce blocks. More...
 
(void) + setCompiler:
 Registers an object that can compile map/reduce functions from source code. More...
 
(nullable id< CBLViewCompiler >) + compiler
 The registered object, if any, that can compile map/reduce functions from source code. More...
 

Properties

CBLDatabasedatabase
 The database that owns this view. More...
 
NSString * name
 The name of the view. More...
 
CBLMapBlock mapBlock
 The map function that controls how index rows are created from documents. More...
 
CBLReduceBlock reduceBlock
 The optional reduce function, which aggregates together multiple rows. More...
 
NSString * documentType
 If this property is set, only documents whose "type" property is equal to its value will be passed to the map block and indexed. More...
 
BOOL stale
 Is the view's index currently out of date? More...
 
SInt64 lastSequenceIndexed
 The last sequence number indexed so far. More...
 
NSUInteger totalRows
 Total number of rows in the view. More...
 

Method Documentation

◆ setMapBlock:reduceBlock:version:()

- (BOOL) setMapBlock: (CBLMapBlock mapBlock
reduceBlock: (nullable CBLReduceBlock reduceBlock
version: (NSString *)  version 

Defines a view's functions.

The view's definition is given as an Objective-C block (or NULL to delete the view). The body of the block should call the 'emit' block (passed in as a paramter) for every key/value pair it wants to write to the view. Since the function itself is obviously not stored in the database (only a unique string identifying it), you must re-define the view on every launch of the app! If the database needs to rebuild the view but the function hasn't been defined yet, it will fail and the view will be empty, causing weird problems later on. It is very important that this block be a law-abiding map function! As in other languages, it must be a "pure" function, with no side effects, that always emits the same values given the same input document. That means that it should not access or change any external state; be careful, since blocks make that so easy that you might do it inadvertently! The block may be called on any thread, or on multiple threads simultaneously. This won't be a problem if the code is "pure" as described above, since it will as a consequence also be thread-safe.

Parameters
mapBlockThe map function. The MAPBLOCK macro makes it easier to declare this.
reduceBlockThe reduce function, or nil for none. The REDUCEBLOCK macro makes it easier to declare this.
versionAn arbitrary string that will be stored persistently along with the index. Usually a string literal like "1". If you subsequently change the functionality of the map or reduce function, change this string as well: the call will detect that it's different and will clear the index so it can be rebuilt by the new function.
Returns
YES if the view was updated and the index cleared; NO if the version stayed the same.

◆ setMapBlock:version:()

- (BOOL) setMapBlock: (CBLMapBlock mapBlock
version: (NSString *)  version 

Defines a view that has no reduce function.

See -setMapBlock:reduceBlock:version: for more details.

◆ updateIndex()

- (void) updateIndex

Updates the view's index, then returns YES if the index changed or NO if it didn't.

Indexing scans all documents that have changed since the last time the index was updated. The body of each document is passed to the view's map block, and any emitted rows are added to the index. Any existing rows previously emitted by those documents, that weren't re-emitted this time, are removed.

◆ updateIndexAsync:()

- (void) updateIndexAsync: (void(^)())  onComplete

Asynchronously updates the view's index.

This method returns immediately, after scheduling a call to -updateIndex on a background thread; the onComplete callback block is called after indexing finishes.

◆ deleteIndex()

- (void) deleteIndex

Deletes the view's persistent index.

It will be regenerated on the next query.

◆ deleteView()

- (void) deleteView

Deletes the view, persistently.

◆ createQuery()

- (CBLQuery*) createQuery

Creates a new query object for this view.

The query can be customized and then executed.

◆ totalValues:()

+ (NSNumber*) totalValues: (NSArray *)  values

Utility function to use in reduce blocks.

Totals an array of NSNumbers.

◆ setCompiler:()

+ (void) setCompiler: (nullable id< CBLViewCompiler >)  compiler

Registers an object that can compile map/reduce functions from source code.

◆ compiler()

+ (nullable id<CBLViewCompiler>) compiler

The registered object, if any, that can compile map/reduce functions from source code.

◆ NS_UNAVAILABLE()

- (instancetype) NS_UNAVAILABLE

Property Documentation

◆ database

- (CBLDatabase*) database
readatomicassign

The database that owns this view.

◆ name

- (NSString*) name
readatomicassign

The name of the view.

◆ mapBlock

- (CBLMapBlock) mapBlock
readatomicassign

The map function that controls how index rows are created from documents.

◆ reduceBlock

- (CBLReduceBlock) reduceBlock
readatomicassign

The optional reduce function, which aggregates together multiple rows.

◆ documentType

- (NSString*) documentType
readwriteatomiccopy

If this property is set, only documents whose "type" property is equal to its value will be passed to the map block and indexed.

This can speed up indexing. Just like the map block, this property is not persistent; it needs to be set at runtime before the view is queried. And if its value changes, the view's version also needs to change.

◆ stale

- (BOOL) stale
readatomicassign

Is the view's index currently out of date?

◆ lastSequenceIndexed

- (SInt64) lastSequenceIndexed
readatomicassign

The last sequence number indexed so far.

◆ totalRows

- (NSUInteger) totalRows
readatomicassign

Total number of rows in the view.

The view's index will be updated if needed before returning the totalRows value.


The documentation for this class was generated from the following file: