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

Detailed Description

A CouchbaseLite database.

Inheritance diagram for CBLDatabase:

Instance Methods

(BOOL) - close:
 Closes a database. More...
 
(BOOL) - compact:
 Compacts the database file by purging non-current JSON bodies, pruning revisions older than the maxRevTreeDepth, deleting unused attachment files, and vacuuming the SQLite database. More...
 
(BOOL) - deleteDatabase:
 Deletes the database. More...
 
(BOOL) - replaceUUIDs:
 Changes the database's unique IDs to new random values. More...
 
(BOOL) - changeEncryptionKey:error:
 Changes the database's encryption key, or removes encryption if the new key is nil. More...
 
(nullable CBLDocument *) - documentWithID:
 Instantiates a CBLDocument object with the given ID. More...
 
(nullable CBLDocument *) - existingDocumentWithID:
 Instantiates a CBLDocument object with the given ID. More...
 
(nullable CBLDocument *) - objectForKeyedSubscript:
 Same as -documentWithID:. More...
 
(CBLDocument *) - createDocument
 Creates a new CBLDocument object with no properties and a new (random) UUID. More...
 
(nullable CBLJSONDict *) - existingLocalDocumentWithID:
 Returns the contents of the local document with the given ID, or nil if none exists. More...
 
(BOOL) - putLocalDocument:withID:error:
 Sets the contents of the local document with the given ID. More...
 
(BOOL) - deleteLocalDocumentWithID:error:
 Deletes the local document with the given ID. More...
 
(CBLQuery *) - createAllDocumentsQuery
 Returns a query that matches all documents in the database. More...
 
(CBLQuery *) - slowQueryWithMap:
 Creates a one-shot query with the given map function. More...
 
(CBLView *) - viewNamed:
 Returns a CBLView object for the view with the given name. More...
 
(nullable CBLView *) - existingViewNamed:
 Returns the existing CBLView with the given name, or nil if none. More...
 
(void) - setValidationNamed:asBlock:
 Defines or clears a named document validation function. More...
 
(nullable CBLValidationBlock- validationNamed:
 Returns the existing document validation function (block) registered with the given name. More...
 
(void) - setFilterNamed:asBlock:
 Defines or clears a named filter function. More...
 
(nullable CBLFilterBlock- filterNamed:
 Returns the existing filter function (block) registered with the given name. More...
 
(BOOL) - inTransaction:
 Runs the block within a transaction. More...
 
(void) - doAsync:
 Runs the block asynchronously on the database's dispatch queue or thread. More...
 
(void) - doSync:
 Runs the block synchronously on the database's dispatch queue or thread: this method does not return until after the block has completed. More...
 
(CBLArrayOf(CBLReplication *) - allReplications
 Returns an array of all current, running CBLReplications involving this database. More...
 
(CBLReplication *) - createPushReplication:
 Creates a replication that will 'push' this database to a remote database at the given URL. More...
 
(CBLReplication *) - createPullReplication:
 Creates a replication that will 'pull' from a remote database at the given URL to this database. More...
 
(instancetype) - NS_UNAVAILABLE
 
() - CBLArrayOf
 All CBLModels associated with this database whose needsSave is true. More...
 
(BOOL) - saveAllModels:
 Saves changes to all CBLModels associated with this database whose needsSave is true. More...
 
(BOOL) - autosaveAllModels:
 Immediately runs any pending autosaves for all CBLModels associated with this database. More...
 

Class Methods

(void) + setFilterCompiler:
 Registers an object that can compile source code into executable filter blocks. More...
 
(nullable id< CBLFilterCompiler >) + filterCompiler
 Returns the currently registered filter compiler (nil by default). More...
 

Properties

NSString * name
 The database's name. More...
 
CBLManagermanager
 The database manager that owns this database. More...
 
NSUInteger documentCount
 The number of documents in the database. More...
 
SInt64 lastSequenceNumber
 The latest sequence number used. More...
 
NSURL * internalURL
 The URL of the database in the REST API. More...
 
NSUInteger maxRevTreeDepth
 The maximum depth of a document's revision tree (or, max length of its revision history.) Revisions older than this limit will be deleted during a -compact: operation. More...
 
CBLModelFactorymodelFactory
 The CBLModel factory object to be used by this database. More...
 

Method Documentation

◆ close:()

- (BOOL) close: (NSError **)  error

Closes a database.

This first stops all replications, and calls -saveAllModels: to save changes to CBLModel objects. It returns NO if some models failed to save.

◆ compact:()

- (BOOL) compact: (NSError **)  outError

Compacts the database file by purging non-current JSON bodies, pruning revisions older than the maxRevTreeDepth, deleting unused attachment files, and vacuuming the SQLite database.

◆ deleteDatabase:()

- (BOOL) deleteDatabase: (NSError **)  outError

Deletes the database.

◆ replaceUUIDs:()

- (BOOL) replaceUUIDs: (NSError **)  outError

Changes the database's unique IDs to new random values.

Ordinarily you should never need to call this method; it's only made public to fix databases that are already affected by bug github.com/couchbase/couchbase-lite-ios/issues/145 . Make sure you only call this once, to fix that problem, because every call has the side effect of resetting all replications, making them run slow the next time.

◆ changeEncryptionKey:error:()

- (BOOL) changeEncryptionKey: (nullable id)  keyOrPassword
error: (NSError **)  error 

Changes the database's encryption key, or removes encryption if the new key is nil.

To use this API, the database storage engine must support encryption. In the case of SQLite, this means the application must be linked with SQLCipher http://sqlcipher.net instead of regular SQLite. Otherwise opening the database will fail with an error.

Parameters
keyOrPasswordThe encryption key in the form of an NSString (a password) or an NSData object exactly 32 bytes in length (a raw AES key.) If a string is given, it will be internally converted to a raw key using 64,000 rounds of PBKDF2 hashing. A nil value will decrypt the database.
errorIf an error occurs, it will be stored here if this parameter is non-NULL.
Returns
YES if the database was successfully re-keyed, or NO on error.

◆ documentWithID:()

- (nullable CBLDocument*) documentWithID: (NSString *)  docID

Instantiates a CBLDocument object with the given ID.

Doesn't touch the on-disk database; a document with that ID doesn't even need to exist yet. CBLDocuments are cached, so there will never be more than one instance (in this database) at a time with the same documentID.

◆ existingDocumentWithID:()

- (nullable CBLDocument*) existingDocumentWithID: (NSString *)  docID

Instantiates a CBLDocument object with the given ID.

Unlike -documentWithID: this method loads the document from the database, and returns nil if no such document exists. CBLDocuments are cached, so there will never be more than one instance (in this database) at a time with the same documentID.

◆ objectForKeyedSubscript:()

- (nullable CBLDocument*) objectForKeyedSubscript: (NSString *)  key

Same as -documentWithID:.

Enables "[]" access in Xcode 4.4+

◆ createDocument()

- (CBLDocument*) createDocument

Creates a new CBLDocument object with no properties and a new (random) UUID.

The document will be saved to the database when you call -putProperties: on it.

◆ existingLocalDocumentWithID:()

- (nullable CBLJSONDict*) existingLocalDocumentWithID: (NSString *)  localDocID

Returns the contents of the local document with the given ID, or nil if none exists.

◆ putLocalDocument:withID:error:()

- (BOOL) putLocalDocument: (nullable CBLJSONDict *)  properties
withID: (NSString *)  localDocID
error: (NSError **)  outError 

Sets the contents of the local document with the given ID.

Unlike CouchDB, no revision-ID checking is done; the put always succeeds. If the properties dictionary is nil, the document will be deleted.

◆ deleteLocalDocumentWithID:error:()

- (BOOL) deleteLocalDocumentWithID: (NSString *)  localDocID
error: (NSError **)  outError 

Deletes the local document with the given ID.

◆ createAllDocumentsQuery()

- (CBLQuery*) createAllDocumentsQuery

Returns a query that matches all documents in the database.

This is like querying an imaginary view that emits every document's ID as a key.

◆ slowQueryWithMap:()

- (CBLQuery*) slowQueryWithMap: (CBLMapBlock mapBlock

Creates a one-shot query with the given map function.

This is equivalent to creating an anonymous CBLView and then deleting it immediately after querying it. It may be useful during development, but in general this is inefficient if this map will be used more than once, because the entire view has to be regenerated from scratch every time.

◆ viewNamed:()

- (CBLView*) viewNamed: (NSString *)  name

Returns a CBLView object for the view with the given name.

(This succeeds even if the view doesn't already exist, but the view won't be added to the database until the CBLView is assigned a map function.)

◆ existingViewNamed:()

- (nullable CBLView*) existingViewNamed: (NSString *)  name

Returns the existing CBLView with the given name, or nil if none.

◆ setValidationNamed:asBlock:()

- (void) setValidationNamed: (NSString *)  validationName
asBlock: (nullable CBLValidationBlock validationBlock 

Defines or clears a named document validation function.

Before any change to the database, all registered validation functions are called and given a chance to reject it. (This includes incoming changes from a pull replication.)

◆ validationNamed:()

- (nullable CBLValidationBlock) validationNamed: (NSString *)  validationName

Returns the existing document validation function (block) registered with the given name.

Note that validations are not persistent – you have to re-register them on every launch.

◆ setFilterNamed:asBlock:()

- (void) setFilterNamed: (NSString *)  filterName
asBlock: (nullable CBLFilterBlock filterBlock 

Defines or clears a named filter function.

Filters are used by push replications to choose which documents to send.

◆ filterNamed:()

- (nullable CBLFilterBlock) filterNamed: (NSString *)  filterName

Returns the existing filter function (block) registered with the given name.

Note that filters are not persistent – you have to re-register them on every launch.

◆ setFilterCompiler:()

+ (void) setFilterCompiler: (nullable id< CBLFilterCompiler >)  compiler

Registers an object that can compile source code into executable filter blocks.

◆ filterCompiler()

+ (nullable id<CBLFilterCompiler>) filterCompiler

Returns the currently registered filter compiler (nil by default).

◆ inTransaction:()

- (BOOL) inTransaction: (BOOL(^)(void))  block

Runs the block within a transaction.

If the block returns NO, the transaction is rolled back. Use this when performing bulk write operations like multiple inserts/updates; it saves the overhead of multiple SQLite commits, greatly improving performance.

◆ doAsync:()

- (void) doAsync: (void(^)())  block

Runs the block asynchronously on the database's dispatch queue or thread.

Unlike the rest of the API, this can be called from any thread, and provides a limited form of multithreaded access to Couchbase Lite.

◆ doSync:()

- (void) doSync: (void(^)())  block

Runs the block synchronously on the database's dispatch queue or thread: this method does not return until after the block has completed.

Unlike the rest of the API, this can only be called from other threads/queues: If you call it from the same thread or dispatch queue that the database runs on, it will deadlock!

◆ allReplications()

- (CBLArrayOf(CBLReplication*) allReplications

Returns an array of all current, running CBLReplications involving this database.

◆ createPushReplication:()

- (CBLReplication*) createPushReplication: (NSURL *)  url

Creates a replication that will 'push' this database to a remote database at the given URL.

This always creates a new replication, even if there is already one to the given URL. You must call -start on the replication to start it.

◆ createPullReplication:()

- (CBLReplication*) createPullReplication: (NSURL *)  url

Creates a replication that will 'pull' from a remote database at the given URL to this database.

This always creates a new replication, even if there is already one from the given URL. You must call -start on the replication to start it.

◆ NS_UNAVAILABLE()

- (instancetype) NS_UNAVAILABLE

◆ CBLArrayOf()

- CBLArrayOf (CBLModel *) 

All CBLModels associated with this database whose needsSave is true.

Provided by category CBLDatabase(CBLModel).

◆ saveAllModels:()

- (BOOL) saveAllModels: (NSError **)  outError

Saves changes to all CBLModels associated with this database whose needsSave is true.

Provided by category CBLDatabase(CBLModel).

◆ autosaveAllModels:()

- (BOOL) autosaveAllModels: (NSError **)  outError

Immediately runs any pending autosaves for all CBLModels associated with this database.

(On iOS, this will automatically be called when the application is about to quit or go into the background. On Mac OS it is NOT called automatically.)

Provided by category CBLDatabase(CBLModel).

Property Documentation

◆ name

- (NSString*) name
readatomicassign

The database's name.

◆ manager

- (CBLManager*) manager
readatomicassign

The database manager that owns this database.

◆ documentCount

- (NSUInteger) documentCount
readatomicassign

The number of documents in the database.

◆ lastSequenceNumber

- (SInt64) lastSequenceNumber
readatomicassign

The latest sequence number used.

Every new revision is assigned a new sequence number, so this property increases monotonically as changes are made to the database. It can be used to check whether the database has changed between two points in time.

◆ internalURL

- (NSURL*) internalURL
readatomicassign

The URL of the database in the REST API.

You can access this URL within this process, using NSURLConnection or other APIs that use that (such as XMLHTTPRequest inside a WebView), but it isn't available outside the process. This method is only available if you've linked with the CouchbaseLiteListener framework.

◆ maxRevTreeDepth

- (NSUInteger) maxRevTreeDepth
readwriteatomic

The maximum depth of a document's revision tree (or, max length of its revision history.) Revisions older than this limit will be deleted during a -compact: operation.

Smaller values save space, at the expense of making document conflicts somewhat more likely.

◆ modelFactory

- (CBLModelFactory*) modelFactory
readwriteatomicretain

The CBLModel factory object to be used by this database.

Every database has its own instance by default, but you can set this property to use a different one – either to use a custom subclass, or to share a factory among multiple databases, or both.

Provided by category CBLDatabase(CBLModelFactory).


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