Skip to content
Merged
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
219 changes: 105 additions & 114 deletions Examples/RemindersTests/SearchRemindersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,101 +28,95 @@ extension BaseTestSuite {
model.searchText = "Take"
try await model.searchTask?.value
#expect(model.searchResults.completedCount == 1)
withKnownIssue(
"'@Fetch' introduces an escaping closure and loses the task-local dependency"
) {
assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) {
"""
[
[0]: SearchRemindersModel.Row(
isPastDue: false,
assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) {
"""
[
[0]: SearchRemindersModel.Row(
isPastDue: false,
notes: "",
reminder: Reminder(
id: UUID(00000000-0000-0000-0000-00000000000A),
dueDate: Date(2009-02-17T23:31:30.000Z),
isFlagged: false,
notes: "",
reminder: Reminder(
id: UUID(00000000-0000-0000-0000-00000000000A),
dueDate: Date(2009-02-17T23:31:30.000Z),
isCompleted: false,
isFlagged: false,
notes: "",
position: 8,
priority: .high,
remindersListID: UUID(00000000-0000-0000-0000-000000000001),
title: "Take out trash"
),
remindersList: RemindersList(
id: UUID(00000000-0000-0000-0000-000000000001),
color: 3985191935,
position: 2,
title: "Family"
),
tags: "",
title: "**Take** out trash"
)
]
"""
}
position: 8,
priority: .high,
remindersListID: UUID(00000000-0000-0000-0000-000000000001),
status: .incomplete,
title: "Take out trash"
),
remindersList: RemindersList(
id: UUID(00000000-0000-0000-0000-000000000001),
color: 3985191935,
position: 2,
title: "Family"
),
tags: "",
title: "**Take** out trash"
)
]
"""
}
}

@Test func showCompleted() async throws {
let model = SearchRemindersModel()
model.searchText = "Take"
try await model.showCompletedButtonTapped()
try await model.searchTask?.value
try await model.$searchResults.load()

withKnownIssue(
"'@Fetch' introduces an escaping closure and loses the task-local dependency"
) {
assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) {
"""
[
[0]: SearchRemindersModel.Row(
isPastDue: false,
assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) {
"""
[
[0]: SearchRemindersModel.Row(
isPastDue: false,
notes: "",
reminder: Reminder(
id: UUID(00000000-0000-0000-0000-00000000000A),
dueDate: Date(2009-02-17T23:31:30.000Z),
isFlagged: false,
notes: "",
reminder: Reminder(
id: UUID(00000000-0000-0000-0000-00000000000A),
dueDate: Date(2009-02-17T23:31:30.000Z),
isCompleted: false,
isFlagged: false,
notes: "",
position: 8,
priority: .high,
remindersListID: UUID(00000000-0000-0000-0000-000000000001),
title: "Take out trash"
),
remindersList: RemindersList(
id: UUID(00000000-0000-0000-0000-000000000001),
color: 3985191935,
position: 2,
title: "Family"
),
tags: "",
title: "**Take** out trash"
position: 8,
priority: .high,
remindersListID: UUID(00000000-0000-0000-0000-000000000001),
status: .incomplete,
title: "Take out trash"
),
remindersList: RemindersList(
id: UUID(00000000-0000-0000-0000-000000000001),
color: 3985191935,
position: 2,
title: "Family"
),
[1]: SearchRemindersModel.Row(
isPastDue: false,
tags: "",
title: "**Take** out trash"
),
[1]: SearchRemindersModel.Row(
isPastDue: false,
notes: "",
reminder: Reminder(
id: UUID(00000000-0000-0000-0000-000000000006),
dueDate: Date(2008-08-07T23:31:30.000Z),
isFlagged: false,
notes: "",
reminder: Reminder(
id: UUID(00000000-0000-0000-0000-000000000006),
dueDate: Date(2008-08-07T23:31:30.000Z),
isCompleted: true,
isFlagged: false,
notes: "",
position: 4,
priority: nil,
remindersListID: UUID(00000000-0000-0000-0000-000000000000),
title: "Take a walk"
),
remindersList: RemindersList(
id: UUID(00000000-0000-0000-0000-000000000000),
color: 1218047999,
position: 1,
title: "Personal"
),
tags: "#car #kids #social",
title: "**Take** a walk"
)
]
"""
}
position: 4,
priority: nil,
remindersListID: UUID(00000000-0000-0000-0000-000000000000),
status: .completed,
title: "Take a walk"
),
remindersList: RemindersList(
id: UUID(00000000-0000-0000-0000-000000000000),
color: 1218047999,
position: 1,
title: "Personal"
),
tags: "#car #kids #social",
title: "**Take** a walk"
)
]
"""
}
}

Expand All @@ -131,40 +125,37 @@ extension BaseTestSuite {
model.searchText = "Take"
try await model.showCompletedButtonTapped()
model.deleteCompletedReminders()
try await model.searchTask?.value
try await model.$searchResults.load()
#expect(model.searchResults.completedCount == 0)
withKnownIssue(
"'@Fetch' introduces an escaping closure and loses the task-local dependency"
) {
assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) {
"""
[
[0]: SearchRemindersModel.Row(
isPastDue: false,
assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) {
"""
[
[0]: SearchRemindersModel.Row(
isPastDue: false,
notes: "",
reminder: Reminder(
id: UUID(00000000-0000-0000-0000-00000000000A),
dueDate: Date(2009-02-17T23:31:30.000Z),
isFlagged: false,
notes: "",
reminder: Reminder(
id: UUID(00000000-0000-0000-0000-00000000000A),
dueDate: Date(2009-02-17T23:31:30.000Z),
isCompleted: false,
isFlagged: false,
notes: "",
position: 8,
priority: .high,
remindersListID: UUID(00000000-0000-0000-0000-000000000001),
title: "Take out trash"
),
remindersList: RemindersList(
id: UUID(00000000-0000-0000-0000-000000000001),
color: 3985191935,
position: 2,
title: "Family"
),
tags: "",
title: "**Take** out trash"
)
]
"""
}
position: 8,
priority: .high,
remindersListID: UUID(00000000-0000-0000-0000-000000000001),
status: .incomplete,
title: "Take out trash"
),
remindersList: RemindersList(
id: UUID(00000000-0000-0000-0000-000000000001),
color: 3985191935,
position: 2,
title: "Family"
),
tags: "",
title: "**Take** out trash"
)
]
"""
}
}
}
Expand Down
35 changes: 22 additions & 13 deletions Sources/SQLiteData/Internal/FetchKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct FetchKey<Value: Sendable>: SharedReaderKey {
#if DEBUG
let isDefaultDatabase: Bool
#endif
@Dependency(\.self) var dependencies

public typealias ID = FetchKeyID

Expand Down Expand Up @@ -86,18 +87,22 @@ struct FetchKey<Value: Sendable>: SharedReaderKey {
return
}
let scheduler: any ValueObservationScheduler = scheduler ?? ImmediateScheduler()
database.asyncRead { dbResult in
let result = dbResult.flatMap { db in
Result {
try request.fetch(db)
withEscapedDependencies { dependencies in
database.asyncRead { dbResult in
let result = dbResult.flatMap { db in
Result {
try dependencies.yield {
try request.fetch(db)
}
}
}
}
scheduler.schedule {
switch result {
case .success(let value):
continuation.resume(returning: value)
case .failure(let error):
continuation.resume(throwing: error)
scheduler.schedule {
switch result {
case .success(let value):
continuation.resume(returning: value)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
Expand All @@ -111,8 +116,12 @@ struct FetchKey<Value: Sendable>: SharedReaderKey {
return SharedSubscription {}
}
#endif
let observation = ValueObservation.tracking { db in
Result { try request.fetch(db) }
let observation = withEscapedDependencies { dependencies in
ValueObservation.tracking { db in
dependencies.yield {
Result { try request.fetch(db) }
}
}
}

let scheduler: any ValueObservationScheduler = scheduler ?? ImmediateScheduler()
Expand Down