SwiftConf 2025 in Cologne gathered iOS, macOS, and server-side Swift engineers following the May 2024 publication of RFC 9562. Sessions addressed the gap in Swift’s Foundation library (no UUID v7 initialiser) and established community patterns for time-sortable UUIDs in iOS apps. See UUID in Swift for the implementation reference.
Key topics covered
Foundation.UUID and UUID v7 — Swift’s built-in UUID() generates UUID v4. The community pattern for UUID v7:
import Foundation
struct UUIDV7 {
static func generate() -> UUID {
var bytes = [UInt8](repeating: 0, count: 16)
// 48-bit Unix ms timestamp
let ms = UInt64(Date().timeIntervalSince1970 * 1000)
bytes[0] = UInt8((ms >> 40) & 0xFF)
bytes[1] = UInt8((ms >> 32) & 0xFF)
bytes[2] = UInt8((ms >> 24) & 0xFF)
bytes[3] = UInt8((ms >> 16) & 0xFF)
bytes[4] = UInt8((ms >> 8) & 0xFF)
bytes[5] = UInt8( ms & 0xFF)
// Random bits for rand_a and rand_b
var rng = SystemRandomNumberGenerator()
for i in 6..<16 { bytes[i] = UInt8.random(in: 0...255, using: &rng) }
// Set version (nibble 12–15 of byte 6) = 7
bytes[6] = (bytes[6] & 0x0F) | 0x70
// Set variant (bits 6–7 of byte 8) = 10
bytes[8] = (bytes[8] & 0x3F) | 0x80
return UUID(uuid: (
bytes[0],bytes[1],bytes[2],bytes[3],bytes[4],bytes[5],bytes[6],bytes[7],
bytes[8],bytes[9],bytes[10],bytes[11],bytes[12],bytes[13],bytes[14],bytes[15]
))
}
}
let id = UUIDV7.generate()
print(id.uuidString) // e.g. 018FBE3A-4C5D-7B12-8ABC-0123456789AB
Sessions noted that using SystemRandomNumberGenerator ensures cryptographic quality randomness. See CSPRNG for the cryptographic random source detail.
Core Data — UUID v7 primary key
// Managed Object with UUID v7 id
class Order: NSManagedObject {
@NSManaged var id: UUID
@NSManaged var status: String
@NSManaged var createdAt: Date
override func awakeFromInsert() {
super.awakeFromInsert()
if id == UUID(uuidString: "00000000-0000-0000-0000-000000000000")! {
id = UUIDV7.generate()
}
}
}
awakeFromInsert() fires when Core Data creates a new managed object instance — the UUID v7 is assigned before the object is saved. ORDER BY id in a Core Data fetch request returns records in creation-time order because UUID v7 sorts lexicographically by timestamp. See UUID in Mobile Apps.
CloudKit — UUID v7 record name
let recordID = CKRecord.ID(recordName: UUIDV7.generate().uuidString)
let record = CKRecord(recordType: "Order", recordID: recordID)
record["status"] = "pending"
CKContainer.default().privateCloudDatabase.save(record) { saved, error in
// record.recordID.recordName is the UUID v7 string
}
Using UUID v7 as the CloudKit record name makes records from multiple devices sort chronologically after sync — CloudKit’s sort-by-ID view shows records in creation order without any additional sort field.
Offline sync — UUID v7 conflict resolution
Sessions demonstrated offline-first sync with UUID v7 record IDs. Because UUID v7 embeds a millisecond timestamp, the server can detect approximate creation time from the ID alone — useful for last-write-wins conflict resolution without requiring a trusted server clock:
func newerRecord(_ a: CKRecord, _ b: CKRecord) -> CKRecord {
// Compare UUID v7 strings lexicographically — larger = more recent
return a.recordID.recordName > b.recordID.recordName ? a : b
}
See Time Precision for the 48-bit millisecond timestamp detail.
Further reading
- UUID in Swift — full guide
- UUID in Mobile Apps
- RFC 9562 — the current UUID standard
- CSPRNG — cryptographic random source
- Time Precision — UUID v7 millisecond timestamp
External references
Frequently asked questions
Does Swift have a native UUID v7 type?
Swift's Foundation.UUID generates UUID v4 via UUID(). There is no native UUID v7 initialiser as of Swift 5.10. Sessions at SwiftConf 2025 discussed adding UUID v7 to Swift's Foundation library following Python 3.13's precedent. For now, use a Swift package — swift-uuid or implement directly using Clock.now and SystemRandomNumberGenerator. See UUID in Swift.
How is UUID v7 used in offline-first iOS apps?
In offline-first apps, records are created on-device without a server round-trip. UUID v7 is ideal because it is globally unique (no coordination with the server), time-sortable (synced records sort in creation order on the server), and works within CloudKit, Core Data, and SQLite. When the device syncs, UUID v7 primary keys do not conflict with server-generated keys — merges are based on ID equality, not sequence position. See UUID in Mobile Apps.