You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftData结合CloudKit的笔记应用编辑器CPU占用过高的优化求助

SwiftData结合CloudKit的笔记应用编辑器CPU占用过高的优化求助

各位大佬好!我最近用SwiftData搭配CloudKit做了个笔记应用,但遇到了超头疼的问题——在笔记编辑器里输入内容时,CPU直接飙到125%,打字都卡得不行。我猜是因为直接绑定SwiftData模型,每输入一个字符就触发了本地存储更新+CloudKit同步,双重操作把CPU拉满了。试了几种延迟保存的思路,但都没搞定,想请大家帮忙看看问题出在哪,给点优化方向。

先贴一下我的核心代码:

Note模型

@Model
class Note {
    var title: String = ""
    var content: String = ""
    var creationDate: Date = Date.now
    
    init(title: String = "", content: String = "") {
        self.title = title
        self.content = content
        self.creationDate = Date.now
    }
}

笔记列表视图

struct NotesListView: View {
    @Query(sort: [SortDescriptor(\Note.creationDate, order: .reverse)])
    private var notes: [Note]
    @Binding var selection: Set<Note.ID>
    
    var body: some View {
        List(selection: $selection) {
            ForEach(notes) { note in
                NavigationLink(value: note) {
                    NoteRowCell(note: note)
                }
            }
        }
    }
    
    init(sort: SortDescriptor<Note>, searchString: String, selection: Binding<Set<Note.ID>>) {
        _notes = Query(filter: #Predicate {
            if searchString.isEmpty {
                return true
            } else {
                return $0.content.localizedStandardContains(searchString)
            }
        }, sort: [sort])
        _selection = selection
    }
}

主ContentView

struct ContentView: View {
    @Environment(\.modelContext) private var modelContext
    @State private var sortOrder = SortDescriptor(\Note.creationDate, order: .reverse)
    @State private var isShowingSearch = false
    @State private var searchText = ""
    @State private var path = [Note]()
    @State private var selection = Set<Note.ID>()
    
    var body: some View {
        NavigationStack(path: $path) {
            NotesListView(
                sort: sortOrder,
                searchString: searchText,
                selection: $selection
            )
            .navigationDestination(for: Note.self) { note in
                NoteEditorView(note: note)
            }
            .toolbar {
                Button("Add Note") {
                    let note = Note()
                    modelContext.insert(note)
                    path = [note]
                }
            }
            .searchable(
                text: $searchText,
                isPresented: $isShowingSearch,
                prompt: "Search"
            )
        }
    }
}

笔记编辑器(我怀疑问题核心在这)

struct NoteEditor: View {
    @Bindable var note: Note
    
    var body: some View {
        VStack {
            TextField("Note title", text: $note.title)
            Divider()
            TextEditor(text: $note.content)
        }
        .padding(.horizontal, 25)
    }
}

现在只要在编辑器里打字,CPU直接冲到125%,明显是实时写入SwiftData导致的——毕竟每输入一个字符,SwiftData都会触发本地状态更新,同时还要同步到CloudKit,这俩操作叠加起来肯定耗资源。我尝试过用Timer或者Combine的debounce做延迟保存,但没搞对方法,还是没法解决卡顿问题。有没有大佬能指点下正确的优化思路?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 12:08:04