下列範例說明如何將 Android、Apple 平台和 Web SDK 連線至 Firestore 模擬器:
Android
// 10.0.2.2 is the special IP address to connect to the 'localhost' of// the host computer from an Android emulator.FirebaseFirestorefirestore=FirebaseFirestore.getInstance();firestore.useEmulator("10.0.2.2",8080);FirebaseFirestoreSettingssettings=newFirebaseFirestoreSettings.Builder().setPersistenceEnabled(false).build();firestore.setFirestoreSettings(settings);
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Use Firestore emulator locally\n==============================\n\nThe [Google Cloud CLI](/sdk) provides a local, in-memory emulator for\nFirestore that you can use to test your application. You can use the\nemulator with all Firestore client libraries. You should use\nthe emulator only for local testing.\n\nDo not use the emulator for production deployments. Because the emulator stores\ndata only in memory, it will not persist data across runs.\n| **Tip:** The instructions on this page show you how to run the Firestore emulator using the gcloud CLI. The same Firestore emulator is also available through the [Firebase Local Emulator Suite](https://firebase.google.com/docs/emulator-suite#what-is-emulator-suite). The emulator suite bundles several emulators and helps you test these services together. To access the Firestore emulator through the emulator suite, see [Introduction to Firebase Local Emulator Suite](https://firebase.google.com/docs/emulator-suite).\n\nInstall the emulator\n--------------------\n\n| **Note:** The Firestore emulator will be updated to require Java 21 in the upcoming Google Cloud CLI release 528.0.0. Upgrade to Java 21 or later to continue using the latest version of the emulator.\n\nTo install the Firestore emulator, install and update the\ngcloud CLI:\n\n1. [Install the gcloud CLI](/sdk/docs/install).\n\n2. Update your gcloud CLI installation to get the latest\n features:\n\n gcloud components update\n\nRun the emulator\n----------------\n\n1. Run the following command to start the emulator:\n\n gcloud emulators firestore start\n\n The emulator prints the host and port number where it is running.\n\n By default, the emulator attempts to use `127.0.0.1:8080`. To bind the\n emulator to a specific host and port, use the optional `--host-port` flag,\n replacing \u003cvar translate=\"no\"\u003eHOST\u003c/var\u003e and \u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e: \n\n gcloud emulators firestore start --host-port=\u003cvar translate=\"no\"\u003eHOST\u003c/var\u003e:\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e\n\n2. Type `Control + C` to stop the emulator. The emulator may also be stopped\n with a POST to `/shutdown`. For example:\n\n curl -d '' \u003cvar translate=\"no\"\u003eHOST\u003c/var\u003e:\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e/shutdown\n\nConnect to the emulator\n-----------------------\n\nHow you connect to the emulator depends on the type of client library, server\nclient library, or mobile/web SDK.\n\n### Server client libraries\n\nTo connect a Firestore server client\nlibrary (C#, Go, Java, Node.js, PHP, Python, and Ruby),\nset the `FIRESTORE_EMULATOR_HOST` environment variable. When this environment\nvariable is set, the server client libraries automatically connect to the emulator. \n\n export FIRESTORE_EMULATOR_HOST=\"\u003cvar translate=\"no\"\u003eHOST\u003c/var\u003e:\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e\"\n\n### Android, Apple platforms, and Web SDKs\n\nThe following examples demonstrate how to connect the\nAndroid, Apple platforms, and Web SDKs to the Firestore\nemulator: \n\n##### Android\n\n```carbon\n// 10.0.2.2 is the special IP address to connect to the 'localhost' of\n// the host computer from an Android emulator.\nFirebaseFirestore firestore = FirebaseFirestore.getInstance();\nfirestore.useEmulator(\"10.0.2.2\", 8080);\n\nFirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()\n .setPersistenceEnabled(false)\n .build();\nfirestore.setFirestoreSettings(settings);https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/firestore/app/src/main/java/com/google/example/firestore/EmulatorSuite.java#L10-L18\n```\n\n##### Swift\n\n```swift\nlet settings = Firestore.firestore().settings\nsettings.host = \"127.0.0.1:8080\"\nsettings.cacheSettings = MemoryCacheSettings()\nsettings.isSSLEnabled = false\nFirestore.firestore().settings = settings \nhttps://github.com/firebase/snippets-ios/blob/cdce007fedb3bb90dd3a70ce03066178236e1deb/firestore/swift/firestore-smoketest/ViewController.swift#L1251-L1255\n```\n\n### Web version 9\n\n```javascript\nimport { getFirestore, connectFirestoreEmulator } from \"firebase/firestore\";\n\n// firebaseApps previously initialized using initializeApp()\nconst db = getFirestore();\nconnectFirestoreEmulator(db, '127.0.0.1', 8080);https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/firestore-next/emulator-suite/fs_emulator_connect.js#L8-L12\n```\n\n### Web version 8\n\n| [Learn\nmore](//firebase.google.com/docs/web/learn-more#modular-version) about the tree-shakeable modular Web API and its advantages over the namespaced API. \n\n```javascript\n// Firebase previously initialized using firebase.initializeApp().\nvar db = firebase.firestore();\nif (location.hostname === \"localhost\") {\n db.useEmulator(\"127.0.0.1\", 8080);\n}https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/firestore/emulator-suite.js#L6-L10\n```\n\nThe Firestore in Native Mode emulator clears database contents when shut down. Since\nthe offline cache of the Firestore SDK is not automatically cleared, you may\nwant to disable local persistence in your emulator configuration to avoid\ndiscrepancies between the emulated database and local caches; in the Web SDK,\npersistence is disabled by default.\n\nClear emulator data\n-------------------\n\nThe Firestore emulator includes a REST endpoint for deleting all\nthe data currently in the emulator. You can use this endpoint to clear data\nbetween tests without shutting down the emulator.\n\nTo delete all data in the emulator, perform an HTTP `DELETE` operation against\nthe following endpoint, replacing \u003cvar translate=\"no\"\u003eHOST\u003c/var\u003e and \u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e with the\nhost and port you selected and replacing \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your\nown project ID: \n\n http://\u003cvar translate=\"no\"\u003eHOST\u003c/var\u003e:\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e/emulator/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/databases/(default)/documents\n\nAdjust the host and port if the emulator does not use `127.0.0.1:8080`.\nYour code should await REST confirmation that the deletion finished or failed.\n\nYou can perform this operation from the shell using `curl`: \n\n $ curl -v -X DELETE \"http://\u003cvar translate=\"no\"\u003eHOST\u003c/var\u003e:\u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e/emulator/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/databases/(default)/documents\"\n\nHow the Firestore emulator differs from production\n--------------------------------------------------\n\nThe Firestore emulator attempts to faithfully replicate the\nbehavior of the production service with some notable limitations.\n\n### Transactions\n\nThe emulator doesn't attempt to mimic the transaction behavior that's seen in\nproduction. It uses a simple locking approach and doesn't attempt to mirror\nthe various concurrency modes offered in the production environment.\n\nWhen you're testing features that involve multiple concurrent writes to one\ndocument, the emulator might be slow to complete write requests. It might take\nup to 30 seconds for the emulator to release the locks. If you need to adjust\nthe test timeout intervals, then do so.\n\nThe emulator doesn't also attempt to mimic all of the\n[production limits](/firestore/quotas#writes_and_transactions), such as\ntimeouts \\& size limits, which involve transactions. If you test features that\ndepend on these production limits, then we recommend that you use a production\nenvironment instead of an emulator.\n\n### Indexes\n\nThe emulator does not track composite indexes and will instead execute any\nvalid query. Make sure to test your app against a real Firestore\ninstance to determine which indexes you require.\n\n### Limits\n\nThe emulator does not enforce all limits enforced in production. For example,\nthe emulator may allow transactions that would be rejected as too large by the\nproduction service. Make sure you are familiar with the\n[documented limits](/firestore/docs/quotas) and that you design your app to\nproactively avoid them."]]