Replies: 2 comments
-
Hi @MMorit00, can you please provide a full project that demonstrates the problem? It's difficult to know what is happening by this partial code snippet alone. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Of course!!!, I would be even more grateful if you could point out any issues I have in the project, in addition to the preview.
Sent from my iPhone
…------------------ Original ------------------
From: Brandon Williams ***@***.***>
Date: Sat,Jul 5,2025 5:15 AM
To: pointfreeco/sharing-grdb ***@***.***>
Cc: MOOZA ***@***.***>, Mention ***@***.***>
Subject: Re: [pointfreeco/sharing-grdb] Preview crash question in SwiftUI(Discussion #86)
Hi @MMorit00, can you please provide a full project that demonstrates the problem? It's difficult to know what is happening by this partial code snippet alone.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
从QQ邮箱发来的超大附件
LumiNote.zip (1.5GB, 2025年8月5日 12:30) 进入下载页面 :https://wx.mail.qq.com/ftn/download?func=3&k=c7c70c316da554d0f5b44a313a346331bec3206e383463311017095717050500095e5c09081902080f0c49050b02511c015d5c54155602045c0c0155090d00035d34647d4d590a7f574c011f425d13259a82886ceed358e05532a038a9b2f47503ab5f743934633138386431&key=c7c70c316da554d0f5b44a313a346331bec3206e383463311017095717050500095e5c09081902080f0c49050b02511c015d5c54155602045c0c0155090d00035d34647d4d590a7f574c011f425d13259a82886ceed358e05532a038a9b2f47503ab5f743934633138386431&code=88d184c1&from=
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm just starting out, this is a very simple application, the preview is having an issue, how should I resolve it? ( it can be successfully built)
import Dependencies
import Foundation
import GRDB
import SharingGRDB
// 全局数据库配置和初始化
func appDatabase() throws -> any DatabaseWriter {
var configuration = Configuration()
configuration.foreignKeysEnabled = true
#if DEBUG
configuration.prepareDatabase { db in
db.trace(options: .profile) { event in
// 使用 expandedDescription 获取完整信息
let description = event.expandedDescription
print("🗄️ Database Query: (description)")
print("---")
}
}
#endif
// 根据环境选择适当的数据库实现
@dependency(.context) var context
let database: any DatabaseWriter
if context == .live {
// 生产环境使用DatabasePool,支持并发读取
let path = URL.documentsDirectory.appending(component: "notes.sqlite").path
database = try DatabasePool(path: path, configuration: configuration)
} else {
// 测试环境使用DatabaseQueue,更轻量级
database = try DatabaseQueue(configuration: configuration)
}
// 数据库迁移
var migrator = DatabaseMigrator()
#if DEBUG
// 调试模式下,当模式变更时自动重置数据库
migrator.eraseDatabaseOnSchemaChange = true
#endif
// 注册迁移:创建notes表
migrator.registerMigration("Create notes table") { db in
try db.create(table: "notes") { t in
t.autoIncrementedPrimaryKey("id")
t.column("content", .text).notNull()
t.column("createdAt", .datetime).notNull()
t.column("updatedAt", .datetime).notNull()
t.column("isMarked", .boolean).notNull().defaults(to: false)
}
}
// 注册迁移:创建cards表
migrator.registerMigration("Create cards table") { db in
try db.create(table: "cards") { t in
t.autoIncrementedPrimaryKey("id")
t.column("title", .text).notNull()
t.column("description", .text).notNull()
t.column("startDate", .datetime).notNull()
t.column("upfrontFinishedDate", .datetime).notNull()
t.column("finishedDate", .datetime)
t.column("imagePath", .text)
t.column("messageForFinished", .text)
t.column("createdAt", .datetime).notNull()
t.column("updatedAt", .datetime).notNull()
}
}
// 注册迁移:添加笔记和卡片关联
migrator.registerMigration("addCardIdToNotes") { db in
try db.alter(table: "notes") { t in
t.add(column: "cardId", .integer).references("cards", onDelete: .setNull)
}
// 执行迁移
try migrator.migrate(database)
return database
}
// 创建测试数据库
func testDatabase() -> any DatabaseWriter {
let database = try! DatabaseQueue()
var migrator = DatabaseMigrator()
// 为测试环境创建相同的表结构
migrator.registerMigration("Create notes table") { db in
try! db.create(table: "notes") { t in
t.autoIncrementedPrimaryKey("id")
t.column("content", .text).notNull()
t.column("createdAt", .datetime).notNull()
t.column("updatedAt", .datetime).notNull()
t.column("isMarked", .boolean).notNull().defaults(to: false)
}
}
// 为测试环境创建cards表
migrator.registerMigration("Create cards table") { db in
try! db.create(table: "cards") { t in
t.autoIncrementedPrimaryKey("id")
t.column("title", .text).notNull()
t.column("description", .text).notNull()
t.column("startDate", .datetime).notNull()
t.column("upfrontFinishedDate", .datetime).notNull()
t.column("finishedDate", .datetime)
t.column("imagePath", .text)
t.column("messageForFinished", .text)
t.column("createdAt", .datetime).notNull()
t.column("updatedAt", .datetime).notNull()
}
}
// 测试环境添加笔记和卡片关联
migrator.registerMigration("addCardIdToNotes") { db in
try! db.alter(table: "notes") { t in
t.add(column: "cardId", .integer).references("cards", onDelete: .setNull)
}
try! migrator.migrate(database)
return database
}
// Creating the preview database
func previewDatabase() -> any DatabaseWriter {
let database = try! DatabaseQueue() // Using an in-memory queue for previews
var migrator = DatabaseMigrator()
// Create the same table structure for the preview environment
migrator.registerMigration("Create notes table") { db in
try! db.create(table: "notes") { t in
t.autoIncrementedPrimaryKey("id")
t.column("content", .text).notNull()
t.column("createdAt", .datetime).notNull()
t.column("updatedAt", .datetime).notNull()
t.column("isMarked", .boolean).notNull().defaults(to: false)
}
}
// Create cards table for preview environment
migrator.registerMigration("Create cards table") { db in
try! db.create(table: "cards") { t in
t.autoIncrementedPrimaryKey("id")
t.column("title", .text).notNull()
t.column("description", .text).notNull()
t.column("startDate", .datetime).notNull()
t.column("upfrontFinishedDate", .datetime).notNull()
t.column("finishedDate", .datetime)
t.column("imagePath", .text)
t.column("messageForFinished", .text)
t.column("createdAt", .datetime).notNull()
t.column("updatedAt", .datetime).notNull()
}
}
// 预览环境添加笔记和卡片关联
migrator.registerMigration("addCardIdToNotes") { db in
try! db.alter(table: "notes") { t in
t.add(column: "cardId", .integer).references("cards", onDelete: .setNull)
}
print("🗄️ Preview Database: (database)")
try! migrator.migrate(database)
// Add some sample data for the preview environment
try! database.write { db in
var note1 = Note(
id: nil, content: "示例笔记 1 📝", createdAt: Date().addingTimeInterval(-3600),
updatedAt: Date().addingTimeInterval(-3600), isMarked: false)
try note1.insert(db) // Call insert on the mutable variable
}
return database
}
// 配置测试数据库依赖的辅助函数
public func configureTestDatabase() {
prepareDependencies { dependencies in
dependencies.defaultDatabase = testDatabase()
}
}
// 配置预览数据库依赖的辅助函数
public func configurePreviewDatabase() {
prepareDependencies { dependencies in
dependencies.defaultDatabase = previewDatabase()
}
}
public func configureDatabase() {
// 使用prepareDependencies函数配置defaultDatabase依赖
prepareDependencies { dependencies in
dependencies.defaultDatabase = try! appDatabase()
}
}
import ComposableArchitecture
import SwiftUI
// 主视图
public struct NotesView: View {
@bindable var store: StoreOf
public var body: some View {
ZStack {
// 黄色背景
BG.yellow
.ignoresSafeArea()
}
}
// MARK: - 子视图
extension NotesView {
fileprivate var headerView: some View {
HStack {
HStack {
Text("2024")
.font(.system(.title2, design: .rounded))
Text("-")
Text("2")
.foregroundColor(.primary700)
}
.font(.system(.title, design: .rounded))
}
fileprivate var statisticsView: some View {
HStack(spacing: 12) {
// 左侧统计
statisticItem(
title: "记录",
subtitle: "今年",
iconName: "sparkles.rectangle.stack.fill",
value: "24"
)
}
fileprivate func statisticItem(title: String, subtitle: String, iconName: String, value: String)
-> some View
{
HStack {
VStack(alignment: .center) {
Text(title)
.font(.system(.callout, design: .rounded))
Text(subtitle)
.font(.system(.caption, design: .rounded))
.foregroundColor(.gray300)
}
}
fileprivate var weekCalendarView: some View {
VStack(spacing: 8) {
weekdayHeader
dateRow
}
.padding(.vertical, 12)
.padding(.horizontal)
}
fileprivate var weekdayHeader: some View {
let weekdays = ["日", "一", "二", "三", "四", "五", "六"]
}
fileprivate var dateRow: some View {
let dates = ["05", "06", "07", "08", "09", "10", "11"]
}
fileprivate func dateItem(date: String, isSelected: Bool) -> some View {
VStack(spacing: 0) {
Text(date)
.font(.system(.callout, design: .rounded))
.fontWeight(isSelected ? .medium : .light)
.foregroundColor(isSelected ? .primary : .gray700)
.frame(width: 40, height: 40)
.overlay {
if isSelected {
Circle()
.fill(.secondary300)
.frame(width: 4, height: 4)
.offset(y: 14)
}
}
.background {
RoundedRectangle(cornerRadius: 16)
.fill(Color.white)
.shadow(color: Color.black.opacity(0.1), radius: 1)
}
fileprivate var noteInputView: some View {
VStack(alignment: .center) {
noteToolbar
emptyStateNote
}
.padding(.bottom, 32)
.background {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.foregroundStyle(.white)
.frame(maxWidth: .infinity)
.padding(.horizontal, 12)
.shadow(color: .black.opacity(0.1), radius: 5, x: 0, y: 3)
}
}
fileprivate var noteToolbar: some View {
HStack(alignment: .top, spacing: 12) {
Spacer()
}
fileprivate var emptyStateNote: some View {
VStack {
Text("✨想记点什么?😊✨")
.font(.system(size: 16, weight: .semibold))
.frame(maxWidth: .infinity)
.foregroundColor(.gray700)
.padding(.vertical, 16)
}
}
#Preview("NotesView") {
return NotesView(
store: Store(
initialState: NotesFeature.State()
) {
NotesFeature()
}
)
}
Beta Was this translation helpful? Give feedback.
All reactions