با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
کتابخانه DataStore داده ها را به صورت ناهمزمان، پیوسته و به صورت تراکنشی ذخیره می کند و بر برخی از اشکالات SharedPreferences غلبه می کند. این صفحه بر ایجاد DataStore در پروژه های چند پلتفرمی Kotlin (KMP) تمرکز دارد. برای اطلاعات بیشتر در مورد DataStore، به اسناد اولیه برای DataStore و نمونههای رسمی مراجعه کنید.
وابستگی ها را تنظیم کنید
برای راه اندازی DataStore در پروژه KMP خود، وابستگی های مصنوعات را در فایل build.gradle.kts برای ماژول خود اضافه کنید:
commonMain.dependencies{// DataStore libraryimplementation("androidx.datastore:datastore:1.1.7")// The Preferences DataStore libraryimplementation("androidx.datastore:datastore-preferences:1.1.7")}
کلاس های DataStore را تعریف کنید
می توانید کلاس DataStore با DataStoreFactory در منبع مشترک ماژول KMP مشترک خود تعریف کنید. قرار دادن این کلاس ها در منابع مشترک به آنها اجازه می دهد تا در تمام پلتفرم های هدف به اشتراک گذاشته شوند. میتوانید از اعلانهای actual و expect برای ایجاد پیادهسازیهای خاص پلتفرم استفاده کنید.
نمونه DataStore را ایجاد کنید
شما باید نحوه نمونه سازی آبجکت DataStore را در هر پلتفرم تعریف کنید. این تنها بخشی از API است که به دلیل تفاوت در API های سیستم فایل، باید در مجموعه های منبع پلتفرم خاص باشد.
مشترک
// shared/src/commonMain/kotlin/createDataStore.kt/** * Gets the singleton DataStore instance, creating it if necessary. */funcreateDataStore(producePath:()->String):DataStore<Preferences>=PreferenceDataStoreFactory.createWithPath(produceFile={producePath().toPath()})internalconstvaldataStoreFileName="dice.preferences_pb"
اندروید
برای ایجاد نمونه DataStore در اندروید، به یک Context به همراه مسیر نیاز دارید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# DataStore (Kotlin Multiplatform)\n\nThe DataStore library stores data asynchronously, consistently, and\ntransactionally, overcoming some of the drawbacks of SharedPreferences. This\npage focuses on creating DataStore in Kotlin Multiplatform (KMP) projects. For\nmore information on DataStore, see the [primary documentation for DataStore](/topic/libraries/architecture/datastore)\nand [official samples](https://github.com/android/kotlin-multiplatform-samples).\n| **Note:** Only [DataStore Preferences](/topic/libraries/architecture/datastore#preferences-datastore) is supported in KMP projects.\n\nSet up dependencies\n-------------------\n\n| **Note:** DataStore supports KMP in versions 1.1.0 and higher.\n\nTo set up DataStore in your KMP project, add the dependencies for the artifacts\nin the `build.gradle.kts` file for your module: \n\n commonMain.dependencies {\n // DataStore library\n implementation(\"androidx.datastore:datastore:1.1.7\")\n // The Preferences DataStore library\n implementation(\"androidx.datastore:datastore-preferences:1.1.7\")\n }\n\nDefine the DataStore classes\n----------------------------\n\nYou can define the `DataStore` class with `DataStoreFactory` inside the common\nsource of your shared KMP module. Placing these classes in common sources allows\nthem to be shared across all target platforms. You can use [`actual` and\n`expect` declarations](https://kotlinlang.org/docs/multiplatform-expect-actual.html) to create platform-specific\nimplementations.\n\nCreate the DataStore instance\n-----------------------------\n\nYou need to define how to instantiate the DataStore object on each platform.\nThis is the only part of the API that is required to be in the specific platform\nsource sets due to the differences in file system APIs.\n\n### Common\n\n // shared/src/commonMain/kotlin/createDataStore.kt\n\n /**\n * Gets the singleton DataStore instance, creating it if necessary.\n */\n fun createDataStore(producePath: () -\u003e String): DataStore\u003cPreferences\u003e =\n PreferenceDataStoreFactory.createWithPath(\n produceFile = { producePath().toPath() }\n )\n\n internal const val dataStoreFileName = \"dice.preferences_pb\"\n\n### Android\n\nTo create the `DataStore` instance on Android, you need a [`Context`](/reference/android/content/Context) along\nwith the path. \n\n // shared/src/androidMain/kotlin/createDataStore.android.kt\n\n fun createDataStore(context: Context): DataStore\u003cPreferences\u003e = createDataStore(\n producePath = { context.filesDir.resolve(dataStoreFileName).absolutePath }\n )\n\n### iOS\n\nOn iOS, you can retrieve the path from the `NSDocumentDirectory`: \n\n // shared/src/iosMain/kotlin/createDataStore.ios.kt\n\n fun createDataStore(): DataStore\u003cPreferences\u003e = createDataStore(\n producePath = {\n val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory(\n directory = NSDocumentDirectory,\n inDomain = NSUserDomainMask,\n appropriateForURL = null,\n create = false,\n error = null,\n )\n requireNotNull(documentDirectory).path + \"/$dataStoreFileName\"\n }\n )\n\n### JVM (Desktop)\n\nTo create the `DataStore` instance on JVM (Desktop), provide a path using Java\nor Kotlin APIs: \n\n // shared/src/jvmMain/kotlin/createDataStore.desktop.kt\n\n fun createDataStore(): DataStore\u003cPreferences\u003e = createDataStore(\n producePath = {\n val file = File(System.getProperty(\"java.io.tmpdir\"), dataStoreFileName)\n file.absolutePath\n }\n )\n\n| **Note:** `System.getProperty(\"java.io.tmpdir\")` points to the temporary folder on the system, which might be cleaned upon reboot. On macOS, you can instead use the `~/Library/Application Support/[your-app]` folder."]]