ไปที่คอนโซล

FirebaseFirestore Framework Reference

Firestore

class Firestore : NSObject

FIRFirestore represents a Firestore Database and is the entry point for all Firestore operations.

  • Creates, caches, and returns a FIRFirestore using the default FIRApp. Each subsequent invocation returns the same FIRFirestore object.

    Declaration

    Swift

    class func firestore() -> Self

    Return Value

    The FIRFirestore instance.

  • Creates, caches, and returns a FIRFirestore object for the specified app. Each subsequent invocation returns the same FIRFirestore object.

    Declaration

    Swift

    class func firestore(app: FIRApp) -> Self

    Parameters

    app

    The FIRApp instance to use for authentication and as a source of the Google Cloud Project ID for your Firestore Database. If you want the default instance, you should explicitly set it to [FIRApp defaultApp].

    Return Value

    The FIRFirestore instance.

  • Custom settings used to configure this FIRFirestore object.

    Declaration

    Swift

    @NSCopying var settings: FIRFirestoreSettings { get set }
  • app

    The Firebase App associated with this Firestore instance.

    Declaration

    Swift

    var app: FIRApp { get }
  • Gets a FIRCollectionReference referring to the collection at the specified path within the database.

    Declaration

    Swift

    func collection(_ collectionPath: String) -> FIRCollectionReference

    Parameters

    collectionPath

    The slash-separated path of the collection for which to get a FIRCollectionReference.

    Return Value

    The FIRCollectionReference at the specified collectionPath.

  • Gets a FIRDocumentReference referring to the document at the specified path within the database.

    Declaration

    Swift

    func document(_ documentPath: String) -> FIRDocumentReference

    Parameters

    documentPath

    The slash-separated path of the document for which to get a FIRDocumentReference.

    Return Value

    The FIRDocumentReference for the specified documentPath.

  • Creates and returns a new Query that includes all documents in the database that are contained in a collection or subcollection with the given collectionID.

    Declaration

    Swift

    func collectionGroup(_ collectionID: String) -> FIRQuery

    Parameters

    collectionID

    Identifies the collections to query over. Every collection or subcollection with this ID as the last segment of its path will be included. Cannot contain a slash.

    Return Value

    The created Query.

  • Executes the given updateBlock and then attempts to commit the changes applied within an atomic transaction.

    The maximum number of writes allowed in a single transaction is 500, but note that each usage of FieldValue.serverTimestamp(), FieldValue.arrayUnion(), FieldValue.arrayRemove(), or FieldValue.increment() inside a transaction counts as an additional write.

    In the updateBlock, a set of reads and writes can be performed atomically using the FIRTransaction object passed to the block. After the updateBlock is run, Firestore will attempt to apply the changes to the server. If any of the data read has been modified outside of this transaction since being read, then the transaction will be retried by executing the updateBlock again. If the transaction still fails after 5 retries, then the transaction will fail.

    Since the updateBlock may be executed multiple times, it should avoiding doing anything that would cause side effects.

    Any value maybe be returned from the updateBlock. If the transaction is successfully committed, then the completion block will be passed that value. The updateBlock also has an NSError out parameter. If this is set, then the transaction will not attempt to commit, and the given error will be passed to the completion block.

    The FIRTransaction object passed to the updateBlock contains methods for accessing documents and collections. Unlike other firestore access, data accessed with the transaction will not reflect local changes that have not been committed. For this reason, it is required that all reads are performed before any writes. Transactions must be performed while online. Otherwise, reads will fail, the final commit will fail, and the completion block will return an error.

    Declaration

    Swift

    func runTransaction(_ updateBlock: @escaping (FIRTransaction, NSErrorPointer) -> Any?, completion: @escaping (Any?, Error?) -> Void)

    Parameters

    updateBlock

    The block to execute within the transaction context.

    completion

    The block to call with the result or error of the transaction. This block will run even if the client is offline, unless the process is killed.

  • Creates a write batch, used for performing multiple writes as a single atomic operation.

    The maximum number of writes allowed in a single batch is 500, but note that each usage of FieldValue.serverTimestamp(), FieldValue.arrayUnion(), FieldValue.arrayRemove(), or FieldValue.increment() inside a batch counts as an additional write.

    Unlike transactions, write batches are persisted offline and therefore are preferable when you don’t need to condition your writes on read data.

    Declaration

    Swift

    func batch() -> FIRWriteBatch
  • Enables or disables logging from the Firestore client.

    Declaration

    Swift

    class func enableLogging(_ logging: Bool)
  • Re-enables usage of the network by this Firestore instance after a prior call to disableNetworkWithCompletion. Completion block, if provided, will be called once network uasge has been enabled.

    Declaration

    Swift

    func enableNetwork(completion: ((Error?) -> Void)? = nil)
  • Disables usage of the network by this Firestore instance. It can be re-enabled by via enableNetworkWithCompletion. While the network is disabled, any snapshot listeners or get calls will return results from cache and any write operations will be queued until the network is restored. The completion block, if provided, will be called once network usage has been disabled.

    Declaration

    Swift

    func disableNetwork(completion: ((Error?) -> Void)? = nil)
  • Clears the persistent storage. This includes pending writes and cached documents.

    Must be called while the firestore instance is not started (after the app is shutdown or when the app is first initialized). On startup, this method must be called before other methods (other than FIRFirestore.settings). If the firestore instance is still running, the function will complete with an error code of FailedPrecondition.

    Note: clearPersistence(completion:) is primarily intended to help write reliable tests that use Firestore. It uses the most efficient mechanism possible for dropping existing data but does not attempt to securely overwrite or otherwise make cached data unrecoverable. For applications that are sensitive to the disclosure of cache data in between user sessions we strongly recommend not to enable persistence in the first place.

    Declaration

    Swift

    func clearPersistence(completion: ((Error?) -> Void)? = nil)
  • Waits until all currently pending writes for the active user have been acknowledged by the backend.

    The completion block is called immediately without error if there are no outstanding writes. Otherwise, the completion block is called when all previously issued writes (including those written in a previous app session) have been acknowledged by the backend. The completion block does not wait for writes that were added after the method is called. If you wish to wait for additional writes, you have to call waitForPendingWritesWithCompletion again.

    Any outstanding waitForPendingWritesWithCompletion completion blocks are called with an error during user change.

    Declaration

    Swift

    func waitForPendingWrites(completion: @escaping (Error?) -> Void)
  • Attaches a listener for a snapshots-in-sync event. Server-generated updates and local changes can affect multiple snapshot listeners. The snapshots-in-sync event indicates that all listeners affected by a given change have fired.

    NOTE: The snapshots-in-sync event only indicates that listeners are in sync with each other, but does not relate to whether those snapshots are in sync with the server. Use SnapshotMetadata in the individual listeners to determine if a snapshot is from the cache or the server.

    Declaration

    Swift

    func addSnapshotsInSyncListener(_ listener: @escaping () -> Void) -> ListenerRegistration

    Parameters

    listener

    A callback to be called every time all snapshot listeners are in sync with each other.

    Return Value

    A FIRListenerRegistration object that can be used to remove the listener.

  • Terminates this FIRFirestore instance.

    After calling terminate only the clearPersistence method may be used. Any other method will throw an error.

    To restart after termination, simply create a new instance of FIRFirestore with firestore or firestoreForApp methods.

    Termination does not cancel any pending writes and any tasks that are awaiting a response from the server will not be resolved. The next time you start this instance, it will resume attempting to send these writes to the server.

    Note: Under normal circumstances, calling this method is not required. This method is useful only when you want to force this instance to release all of its resources or in combination with clearPersistence to ensure that all local state is destroyed between test runs.

    Declaration

    Swift

    func terminate(completion: ((Error?) -> Void)? = nil)

    Parameters

    completion

    A block to execute once everything has been terminated.