swift 核心数据地震演示在预览中崩溃

wz1wpwve  于 2023-03-16  发布在  Swift
关注(0)|答案(1)|浏览(111)

目前我了解了更多关于核心数据和测试苹果提供的演示代码“地震-加载和显示一个大的数据源”。但现在我遇到了一个问题与画布视图。每次编辑器崩溃时,我使用提供的Quake. preview。
我也尝试过读取crashLogs,但我不知道是什么原因导致这个错误。在ContentView中,当我将提供程序从.preview更改为.shared时,它没有崩溃。
有人知道为什么会发生这种情况吗?谢谢
内容视图预览提供程序:

struct ContentView_Previews: PreviewProvider {
    static let quakesProvider = QuakesProvider.preview
    static var previews: some View {
        ContentView(quakesProvider: quakesProvider)
        
            .environment(\.managedObjectContext,
                          quakesProvider.container.viewContext)
    }
}
/// An earthquake for use with canvas previews.
    static var preview: Quake {
        let quakes = Quake.makePreviews(count: 1)
        return quakes[0]
    }

   
    static func makePreviews(count: Int) -> [Quake] {
        var quakes: [Quake] = []
        let viewContext = QuakesProvider.preview.container.viewContext
        for index in 0..<count {
            let quake = Quake(context: viewContext)
            quake.code = UUID().uuidString
            quake.time = Date().addingTimeInterval(Double(index) * -300)
            quake.magnitude = .random(in: -1.1...10.0)
            quake.place = "15km SSW of Cupertino, CA"
            quakes.append(quake)
        }
        return quakes
    }
/// A quakes provider for use with canvas previews.
    static let preview: QuakesProvider = {
        let provider = QuakesProvider(inMemory: true)
        Quake.makePreviews(count: 10)
        return provider
    }()
goqiplq2

goqiplq21#

之间存在循环依赖关系
static let preview: QuakesProvider呼叫Quake.makePreviews(count: 10)
以及
static func makePreviews(count: Int) -> [Quake]呼叫QuakesProvider.preview
您可以更改为:

static func makePreviews(context: NSManagedObjectContext, count: Int) -> [Quake] {
    var quakes: [Quake] = []
    for index in 0..<count {
        let quake = Quake(context: context)
        quake.code = UUID().uuidString
        quake.time = Date().addingTimeInterval(Double(index) * -300)
        quake.magnitude = .random(in: -1.1...10.0)
        quake.place = "15km SSW of Cupertino, CA"
        quakes.append(quake)
    }
    return quakes
}

/// A quakes provider for use with canvas previews.
static let preview: QuakesProvider = {
    let provider = QuakesProvider(inMemory: true)
    Quake.makePreviews(context: provider.container.viewContext, count: 10)
    return provider
}()

/// An earthquake for use with canvas previews.
static var preview: Quake {
    let provider = QuakesProvider(inMemory: true)
    let quakes = Quake.makePreviews(context: provider.container.viewContext, count: 1)
    return quakes[0]
}

同样在QuakesProvider.swift中,删除初始化器的private访问级别,以便我们可以在Quake扩展中使用它:

init(inMemory: Bool = false) { ... }

快速用户界面预览:

对于List或在Quake实体上具有FetchRequest的任何视图:

struct ContentView_Previews: PreviewProvider {
    // Use the in-memory and pre-populated QuakesProvider
    static let quakesProvider = QuakesProvider.preview
    static var previews: some View {
        ContentView(quakesProvider: quakesProvider)
            .environment(\.managedObjectContext, quakesProvider.container.viewContext)
    }
}

对于QuakeRow中的单个项目:

struct QuakeRow_Previews: PreviewProvider {
    static var previews: some View {
        // Use the in-memory QuakesProvider to create only 1 item, and returns it
        QuakeRow(quake: .preview)
    }
}

相关问题