@FetchRequest崩溃预览

时间:2020-07-14 00:08:29

标签: core-data swiftui

我正在尝试将数据库中的项目加载到列表中,它在设备和模拟器上都可以正常工作,但预览将始终崩溃,并显示以下消息:

“无法在此文件中预览-连接中断:向代理发送消息”

import SwiftUI
import CoreData

struct SettingsView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: ChildProfile.entity(), sortDescriptors: []) var children: FetchedResults<ChildProfile>
    
    var body: some View {
        VStack {
            List {
                Section(header: Text("Children")) {
                    ForEach(children, id: \.id) { child in
                        ChildRow(child: child)
                    }
                }
            }
        }
    }
}

struct SettingsView_Previews: PreviewProvider {
    static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    
    static var previews: some View {
        let defaultChild = ChildProfile(context: moc)
        defaultChild.id = UUID()
        defaultChild.name = "Dale"
        return SettingsView().environment(\.managedObjectContext, moc)
    }
}

查看显示的ww预览崩溃的诊断信息:

错误域= NSCocoaErrorDomain代码= 516 ““ ChildProfile + CoreDataClass.swift”无法链接到 “中间体”,因为已经存在同名物品。” UserInfo = {NSSourceFilePathErrorKey = / Users / james / Library / Developer / Xcode / DerivedData / BedtimeClock-hehxrojiiepyadgitseedusiyozd / Build / Intermediates.noindex / Previews / Project One / Intermediates.noindex /项目 One.build/Debug-iphonesimulator/项目 One.build/DerivedSources/CoreDataGenerated/Model/ChildProfile+CoreDataClass.swift, NSUserStringVariant =( >链接>), NSDestinationFilePath = / var / folders / dp / hztzhf572q26jwx6vb9nqpw0000gn / T / previews-diagnostics-20200713-200707 / Intermediates / ChildProfile + CoreDataClass.swift, NSFilePath = /用户/ james /库/开发人员/Xcode/DerivedData/Bedtime_Clock-hehxrojiiepyadgitseedusiyozd/Build/Intermediates.noindex/Previews/Project One / Intermediates.noindex /项目 One.build/Debug-iphonesimulator/项目 One.build/DerivedSources/CoreDataGenerated/Model/ChildProfile+CoreDataClass.swift, NSUnderlyingError = 0x7fb78a150620 {Error Domain = NSPOSIXErrorDomain 代码= 17“文件存在”}}

我可以使用相同的预览代码成功预览ChildRow()。那么,这是一个错误还是我做错了什么?

0 个答案:

没有答案