Database

public final class Database

Thread-safe Database object

  • Init a database from path.
    Note that all database objects with same path share the same core. So you can create multiple database objects. WCDB will manage them automatically.
    Note that WCDB will not generate a sqlite handle until the first operation, which is also called as lazy initialization.

    Declaration

    Swift

    public convenience init(withPath path: String)

    Parameters

    path

    Path to your database

  • Init a database from file url.
    Note that all database objects with same path share the same core. So you can create multiple database objects. WCDB will manage them automatically.
    Note that WCDB will not generate a sqlite handle until the first operation, which is also called as lazy initialization.

    Declaration

    Swift

    public init(withFileURL url: URL)

    Parameters

    url

    File url to your database

  • Init a database from existing tag.
    Note that all database objects with same path share the same core. So you can create multiple database objects. WCDB will manage them automatically.
    Note that WCDB will not generate a sqlite handle until the first operation, which is also called as lazy initialization.

    Throws

    Error while tag is not exists

    Declaration

    Swift

    public init(with tag: Tag) throws

    Parameters

    tag

    An existing tag.

  • Since WCDB is using lazy initialization, init(withPath:), init(withFileURL:) never failed even the database can’t open. So you can call this to check whether the database can be opened.
    Return false if an error occurs during sqlite handle initialization.

    Declaration

    Swift

    public var canOpen: Bool
  • Check database is already opened.

    Declaration

    Swift

    public var isOpened: Bool
  • Check whether database is blockaded.

    Declaration

    Swift

    public var isBlockaded: Bool
  • Close the database.
    Since Multi-threaded operation is supported in WCDB, other operations in different thread can open the closed database. So this method can make sure database is closed in the onClosed block. All other operations will be blocked until this method returns.

    A close operation consists of 4 steps:
    1. blockade, which blocks all other operations.
    2. close, which waits until all sqlite handles return and closes them.
    3. onClosed, which trigger the callback.
    4. unblokade, which unblocks all other opreations.

    You can simply call close: to do all steps above or call these separately.
    Since this method will wait until all sqlite handles return, it may lead to deadlock in some bad practice. The key to avoid deadlock is to make sure all WCDB objects in current thread is dealloced. In detail:
    1. You should not keep WCDB objects, including Insert, Delete, Update, Select, RowSelect, MultiSelect, CoreStatement, Transaction. These objects should not be kept.
    You should get them, use them, then release them right away.
    2. WCDB objects may not be out of its’ scope.
    The best practice is to call close: in sub-thread and display a loading animation in main thread.

    //close directly
    database.close(onClosed: { () throws -> Void in
        //do something on this closed database
    })
    
    //close separately
    database.blockade()
    database.close()
    //do something on this closed database
    database.unblockade()
    

    Throws

    Rethrows your error.

    Declaration

    Swift

    public func close(onClosed: OnClosed) rethrows

    Parameters

    onClosed

    Trigger on database closed.

  • Close the database.

    Declaration

    Swift

    public func close()
  • Blockade the database.

    Declaration

    Swift

    public func blockade()
  • Unblockade the database.

    Declaration

    Swift

    public func unblockade()
  • Purge all free handles of this database.
    WCDB will cache and reuse some sqlite handles to improve performance.
    The max count of free sqlite handles is same as the number of concurrent threads supported by the hardware implementation.
    You can call it to save some memory.

    Declaration

    Swift

    public func purgeFreeHandles()
  • Purge all free handles of all databases.
    Note that WCDB will call this interface automatically while it receives memory warning on iOS.

    Declaration

    Swift

    public static func purgeFreeHandlesInAllDatabase()