【发布时间】:2021-07-05 23:40:34
【问题描述】:
我刚刚更新到 Xcode 12.5.1,现在每当我编辑特定文件时,我的 SourceKitService 都会占用大量 CPU。在对这个文件进行任何程度的编辑后,我的 CPU 使用率飙升,代码完成等基本服务停止工作。我已经在网上尝试了有关此问题的大多数解决方案,但没有任何帮助。有没有人对此有任何想法?谢谢。
我将把所有文件的代码放在这里,因为我不确定问题可能出在哪里。
//
// ScheduleView.swift
// ClassWidget
//
// Created by Ben K on 6/17/21.
//
import SwiftUI
import CoreData
struct ScheduleView: View {
@Environment(\.managedObjectContext) var moc
@ObservedObject var schedule: Schedule
@State private var showingAddPeriod = false
@State private var showingEditPeriod = false
@State private var editPeriod: Period?
@State private var isEditMode: EditMode = .inactive
@State private var showingSettings = false
@State private var showingPreview = false
@State private var showingWarning = false
@State private var warningPeriod: Period?
var timeFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter
}
var body: some View {
ZStack {
Text("\(editPeriod?.uName ?? "")")
.hidden()
List {
Section(header: Text("Classes")) {
if !schedule.periods.isEmpty {
ForEach(schedule.periods) { period in
Button(action: { editPeriod = period; isEditMode = .inactive; showingEditPeriod = true }) {
HStack {
VStack {
Text(timeFormatter.string(from: period.uStart))
Text("to")
Text(timeFormatter.string(from: period.uEnd))
}
.font(.caption)
.padding(.trailing, 10)
.padding(6)
Divider()
.frame(height: 35)
.padding(.trailing)
VStack(alignment: .leading) {
Text(period.uName)
if period.uTeacher != "" && period.uRoom != "" {
Text("\(period.uTeacher) • \(period.uRoom)")
.font(.caption)
.foregroundColor(.secondary)
} else if period.uTeacher != "" {
Text("\(period.uTeacher)")
.font(.caption)
.foregroundColor(.secondary)
} else if period.uRoom != "" {
Text("\(period.uRoom)")
.font(.caption)
.foregroundColor(.secondary)
}
}
Spacer()
Image(systemName: "chevron.right")
.renderingMode(.template)
.padding(.trailing, 10)
.opacity(0.5)
}
.foregroundColor(.primary)
}
}
.onDelete(perform: delete)
} else {
VStack(alignment: .leading) {
Text("No classes yet")
.font(.headline)
Text("Start adding classes to create this schedule!")
.font(.caption)
.italic()
}
.padding(8)
}
}
Section {
Button(action: {
showingSettings = true
}) {
HStack {
Text("Settings")
Spacer()
Image(systemName: "chevron.right")
.padding(.trailing, 10)
.opacity(0.5)
}
.foregroundColor(.primary)
}
}
Button("Preview Widget") {
showingPreview = true
}
}
.listStyle(InsetGroupedListStyle())
}
.navigationTitle(schedule.uName)
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(trailing: Button(action: {
showingAddPeriod = true
}) {
Image(systemName: "plus").padding([.vertical, .leading])
})
.sheet(isPresented: $showingAddPeriod) {
AddPeriod(schedule: schedule)
.environment(\.managedObjectContext, self.moc)
}
/*.sheet(isPresented: $showingEditPeriod) {
if let period = editPeriod {
AddPeriod(period: period)
.environment(\.managedObjectContext, self.moc)
}
}*/
.fullScreenCover(isPresented: $showingEditPeriod, onDismiss: dismissedSheet) {
if let period = editPeriod {
AddPeriod(period: period)
.environment(\.managedObjectContext, self.moc)
}
}
.fullScreenCover(isPresented: $showingSettings) {
ScheduleSettingsView(schedule: schedule)
.environment(\.managedObjectContext, self.moc)
}
.sheet(isPresented: $showingPreview) {
PreviewWidget(schedule: schedule)
}
.alert(isPresented: $showingWarning) {
Alert(title: Text("Delete \(warningPeriod?.uName ?? "")"), message: Text("Are you sure?"), primaryButton: .destructive(Text("Delete")) {
try? moc.save()
}, secondaryButton: .cancel() {
if let period = warningPeriod {
readdPeriod(period: period)
}
})
}
.environment(\.editMode, self.$isEditMode)
}
func delete(at offsets: IndexSet) {
for offset in offsets {
warningPeriod = schedule.periods[offset]
moc.delete(schedule.periods[offset])
showingWarning = true
}
}
func readdPeriod(period: Period) {
let newPeriod = Period(period: period, context: moc)
newPeriod.schedule = schedule
try? moc.save()
}
func dismissedSheet() {
schedule.objectWillChange.send()
}
}
struct ScheduleView_Previews: PreviewProvider {
static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
static var previews: some View {
let schedule = Schedule(name: "Example Schedule", number: 0, context: moc)
NavigationView {
ScheduleView(schedule: schedule)//.preferredColorScheme(.dark)
}
}
}
【问题讨论】:
-
请张贴你的代码
-
好的,刚刚添加了代码。对不起,如果它写得不好,我是这个lol的新手
-
你的代码距离Minimal Reproducible Example还有很长的路要走,不可能帮你排错。通过清理它并在一个干净的项目中复制问题,您可能会找到触发器。如果可以,请向 Apple 报告。通过注释掉特性来统计。
-
对于询问发布代码的人,您能否解释一下为什么您认为在此处包含代码很重要,以及您从阅读可能解释为何 SourceKitService 流程会以这种方式运行的代码中得出的结论?我非常感兴趣,因为我也遇到了这项服务的问题。
-
给我看看能不能复制。到目前为止,我还远未复制,但我注意到一些事情,例如使用
.primary等。他们是否有Color扩展名,或者他们是否以某种方式利用 Xcode 13 iOS 15.primary。此外,CoreData 对象的那些init是有问题的。我不知道为什么它会这样,但清理构建有时会起作用,而且我的 CPU/SourceKitService 超过了 SwiftUI 的复杂性和可怕的“编译时间太长”消息。不久前我在搜索时看到了一条推文,说这是因为缺少了一个美元。