|
| 1 | +--- |
| 2 | +title: Brownfield integration |
| 3 | +--- |
| 4 | + |
| 5 | +# Brownfield integration |
| 6 | + |
| 7 | +!!! info |
| 8 | + |
| 9 | + Brownfield integration is supported on **Android**, **iOS**, and **macOS**. |
| 10 | + |
| 11 | +`AsyncStorage` is built on a shared storage layer (`SharedStorage`) that can also be accessed directly from native |
| 12 | +code. |
| 13 | +This is especially useful in brownfield scenarios, where your app combines React Native and native code, allowing both |
| 14 | +layers to read from and write to the same storage consistently. |
| 15 | + |
| 16 | +All platforms provide a thread-safe singleton registry called `StorageRegistry` to manage storage instances. |
| 17 | + |
| 18 | +### Android |
| 19 | + |
| 20 | +On Android, `StorageRegistry` is a public singleton, which is used to share `SharedStorage` instances with the native module. |
| 21 | +Multiple calls with the same name return the same singleton instance, ensuring consistent access. |
| 22 | + |
| 23 | +```kotlin |
| 24 | +import android.content.Context |
| 25 | +import kotlinx.coroutines.runBlocking |
| 26 | +import org.asyncstorage.shared_storage.Entry |
| 27 | +import org.asyncstorage.shared_storage.SharedStorage |
| 28 | +import org.asyncstorage.storage.StorageRegistry |
| 29 | + |
| 30 | +// access shared storage via StorageRegistry |
| 31 | +val storage: SharedStorage = StorageRegistry.getStorage(ctx, "my-users") |
| 32 | + |
| 33 | +// runBlocking only for a demonstration |
| 34 | +runBlocking { |
| 35 | + storage.setValues(listOf(Entry("email", "john@example.com"))) |
| 36 | + val values = storage.getValues(listOf("email")) |
| 37 | + println("Stored email: ${values.firstOrNull()?.value}") |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### iOS / macOS |
| 42 | + |
| 43 | +On iOS and macOS, the `StorageRegistry` singleton provides the same functionality in Swift and Objective-C. |
| 44 | + |
| 45 | +```swift |
| 46 | +import AsyncStorage |
| 47 | +import SharedAsyncStorage |
| 48 | + |
| 49 | +// access shared storage via StorageRegistry |
| 50 | +let storage: SharedStorage = StorageRegistry.shared.getStorage(dbName: "my-users") |
| 51 | + |
| 52 | +Task { |
| 53 | + storage.setValues([Entry(key: "email", value: "john@example.com")]) |
| 54 | + let values = storage.getValues(keys: ["email"]) |
| 55 | + print("Stored email: \(values.first?.value ?? "none")") |
| 56 | +} |
| 57 | +``` |
0 commit comments