Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions Examples/CloudKitDemo/CountersListFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import CloudKit
import SQLiteData
import SwiftUI

let initialAssetSize = 10

struct CountersListView: View {
@FetchAll(
Counter
.leftJoin(SyncMetadata.all) { $0.syncMetadataID.eq($1.id) }
.leftJoin(CounterAsset.all) { $0.id.eq($1.counterID) }
.leftJoin(SyncMetadata.all) { $0.syncMetadataID.eq($2.id) }
.select {
Row.Columns(counter: $0, isShared: $1.isShared.ifnull(false))
Row.Columns(counter: $0, counterAsset: $1, isShared: $2.isShared.ifnull(false))
}
) var rows
@Dependency(\.defaultDatabase) var database
@Dependency(\.defaultSyncEngine) var syncEngine

@Selection struct Row {
let counter: Counter
let counterAsset: CounterAsset?
let isShared: Bool
}

Expand All @@ -39,10 +43,13 @@ struct CountersListView: View {
Task {
withErrorReporting {
try database.write { db in
try Counter.insert {
let counterID = try Counter.insert {
Counter.Draft()
}
.execute(db)
}.returning(\.id)
.fetchOne(db)!
try CounterAsset.insert {
CounterAsset(counterID: counterID, assetData: Data(count: initialAssetSize))
}.execute(db)
}
}
}
Expand All @@ -66,6 +73,7 @@ struct CountersListView: View {
struct CounterRow: View {
let row: CountersListView.Row
@State var sharedRecord: SharedRecord?
@State var updateAssetCount = 0
@Dependency(\.defaultDatabase) var database
@Dependency(\.defaultSyncEngine) var syncEngine

Expand All @@ -83,6 +91,16 @@ struct CounterRow: View {
incrementButtonTapped()
}
Spacer()
if let assetData = row.counterAsset?.assetData {
Text("Asset: \(assetData.count) bytes")
} else {
Text("<no asset>").foregroundStyle(.red)
}
Spacer()
Button("Update asset") {
updateAssetCount += 1
updateAssetButtonTapped()
}
Button {
shareButtonTapped()
} label: {
Expand Down Expand Up @@ -124,6 +142,25 @@ struct CounterRow: View {
}
}
}

func updateAssetButtonTapped() {
withErrorReporting {
let sizeInBytes = initialAssetSize + updateAssetCount
let assetData = Data(count: sizeInBytes)
try database.write { db in
// This delete isn't strictly necessary, but it's
// what causes the data loss
try CounterAsset
.where { $0.counterID.eq(row.counter.id) }
.delete()
.execute(db)

try CounterAsset.upsert {
CounterAsset(counterID: row.counter.id, assetData: assetData)
}.execute(db)
}
}
}
}

#Preview {
Expand Down
20 changes: 19 additions & 1 deletion Examples/CloudKitDemo/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ nonisolated struct Counter: Identifiable {
var count = 0
}

@Table
nonisolated struct CounterAsset: Identifiable {
@Column(primaryKey: true) public let counterID: Counter.ID
public var id: Counter.ID { counterID }
public var assetData: Data
}

extension DependencyValues {
mutating func bootstrapDatabase() throws {
var configuration = Configuration()
Expand Down Expand Up @@ -37,11 +44,22 @@ extension DependencyValues {
)
.execute(db)
}
migrator.registerMigration("Add CounterAsset") { db in
try #sql(
"""
CREATE TABLE "counterAssets" (
"counterID" TEXT PRIMARY KEY NOT NULL,
"assetData" BLOB NOT NULL DEFAULT (x'')
) STRICT
"""
)
.execute(db)
}
try migrator.migrate(database)
defaultDatabase = database
defaultSyncEngine = try SyncEngine(
for: defaultDatabase,
tables: Counter.self
tables: Counter.self, CounterAsset.self
)
}
}
Expand Down