Mobile apps face a unique identifier challenge: records are often created offline — no network, no server, no sequence counter. UUIDs are the natural solution. The question is which version to use and how to store them efficiently on device.
iOS — Swift
UUID v4 (standard library):
import Foundation
let id = UUID() // UUID v4
let str = id.uuidString // "550E8400-E29B-41D4-A716-446655440000"
Note: Apple’s UUID type returns uppercase hex — most other ecosystems use lowercase. Both are valid per RFC 9562, but be consistent when comparing IDs across platforms.
Storing UUIDs in Core Data:
// Core Data entity attribute type: UUID
// In NSManagedObject subclass:
@NSManaged var id: UUID
Core Data stores UUID as a 16-byte BLOB internally (efficient). In SwiftUI previews and tests, generate deterministic UUIDs:
let testId = UUID(uuidString: "00000000-0000-0000-0000-000000000001")!
SQLite on iOS (GRDB.swift):
GRDB.swift is the most popular type-safe SQLite library for Swift:
import GRDB
struct Order: Codable, FetchableRecord, PersistableRecord {
var id: UUID = UUID() // v4; swap for v7 generator when available
var status: String
}
Android — Kotlin
UUID v4:
import java.util.UUID
val id = UUID.randomUUID()
val str = id.toString() // "550e8400-e29b-41d4-a716-446655440000"
UUID v7 with java-uuid-generator:
// build.gradle.kts
dependencies {
implementation("com.fasterxml.uuid:java-uuid-generator:5.0.0")
}
import com.fasterxml.uuid.Generators
val id = Generators.timeBasedEpochGenerator().generate() // UUID v7
Room database:
@Entity(tableName = "orders")
data class Order(
@PrimaryKey val id: UUID = Generators.timeBasedEpochGenerator().generate(),
val status: String,
)
// In AppDatabase:
@TypeConverters(UuidConverter::class)
abstract class AppDatabase : RoomDatabase()
class UuidConverter {
@TypeConverter fun fromUuid(uuid: UUID): String = uuid.toString()
@TypeConverter fun toUuid(str: String): UUID = UUID.fromString(str)
}
Room stores the UUID as TEXT by default via the type converter. For BLOB storage (more efficient), convert to/from ByteArray.
React Native
React Native runs JavaScript in a Hermes or JavaScriptCore engine. crypto.randomUUID() is available in React Native 0.73+ (Hermes with the Web Crypto API polyfill). For UUID v7:
import { v7 as uuidv7 } from 'uuid';
// uuid npm package works in React Native via the react-native-get-random-values polyfill
import 'react-native-get-random-values'; // must be imported before uuid
import { v7 as uuidv7 } from 'uuid';
const id = uuidv7();
react-native-get-random-values bridges React Native’s Math.random() environment to the native CSPRNG, providing the entropy source the uuid package requires.
Offline-First Sync Patterns
Mobile apps create records offline, then sync to the server. UUID v7 simplifies sync because:
- The client generates the UUID before network connectivity is available
- The UUID’s timestamp indicates when the record was created offline
- The server can detect records created far in the past (large timestamp gap) and apply conflict resolution rules
- Sorted display by primary key gives approximate creation order — useful for showing “recently created” lists
For conflict resolution in collaborative apps, pair UUIDs with a vector clock or last-write-wins timestamp rather than relying on UUID ordering alone.
Related Resources
- Apple Foundation UUID documentation
- GRDB.swift — SQLite library for iOS
- java-uuid-generator (JUG) — UUID v7 for Android/JVM
- react-native-get-random-values — CSPRNG polyfill for React Native
- Android Room database documentation
Frequently asked questions
How do I generate a UUID in Swift (iOS)?
Swift's Foundation framework includes UUID() which generates a UUID v4: let id = UUID().uuidString. Note: Apple's UUID type returns uppercase hex — most other ecosystems use lowercase. Both are valid per RFC 9562. For UUID v7 in iOS, use a third-party package or generate via a custom implementation reading Date().timeIntervalSince1970.
Should I use v4 or v7?
Use v7 for database primary keys (time-sortable, index-friendly) and v4 for anything where creation order could leak information, like tokens or share links.
What column type should I use to store a UUID in a database?
Use the native type where one exists — uuid in PostgreSQL, uniqueidentifier in SQL Server. In MySQL use BINARY(16) and store raw bytes. Avoid VARCHAR(36): it costs 36 bytes per row versus 16 for binary and makes every comparison a string operation.
How do I generate a UUID in Swift (iOS)?
Swift includes UUID() in Foundation, which generates a UUID v4: let id = UUID().uuidString. For UUID v7, Swift does not have a standard library implementation — use the swift-uuid package or generate via a custom implementation reading Date().timeIntervalSince1970 for the timestamp. The Apple UUID documentation covers the built-in v4 type.
How do I generate a UUID in Kotlin (Android)?
Kotlin/Java includes java.util.UUID.randomUUID() for UUID v4. For UUID v7, use the java-uuid-generator (JUG) library: add com.fasterxml.uuid:java-uuid-generator:5.0.0 to your Gradle dependencies, then call Generators.timeBasedEpochGenerator().generate(). The result is a standard java.util.UUID compatible with Room and other Android database libraries.