gpt4 book ai didi

ios - FetchedResults 崩溃 : Dispatch queue: com. apple.main-thread (0)

转载 作者:行者123 更新时间:2023-11-29 05:13:18 25 4
gpt4 key购买 nike

我按照本教程学习了动态过滤器和 CoreData: https://www.hackingwithswift.com/quick-start/ios-swiftui/dynamically-filtering-fetchrequest-with-swiftui

我有以下代码。 ContentView.swift:

import SwiftUI

struct ContentView: View {

@Environment(\.managedObjectContext) var moc
@State var lastNameFilter = "A"

var body: some View {

VStack {
FilteredList(filter: lastNameFilter)

Button("Add Examples") {
let taylor = Singer(context: self.moc)
taylor.firstName = "Taylor"
taylor.lastName = "Swift"

let ed = Singer(context: self.moc)
ed.firstName = "Ed"
ed.lastName = "Sheeran"

let adele = Singer(context: self.moc)
adele.firstName = "Adele"
adele.lastName = "Adkins"

try? self.moc.save()
}

Button("Show A") {
self.lastNameFilter = "A"
}

Button("Show S") {
self.lastNameFilter = "S"
}
}

}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

FilteredList.swift:

import CoreData
import SwiftUI

struct FilteredList: View {

var fetchRequest: FetchRequest<Singer>
var singers: FetchedResults<Singer> { fetchRequest.wrappedValue }

var body: some View {
List(singers, id: \.self) { singer in
Text("\(singer.wrappedFirstName) \(singer.wrappedLastName)")
}
}

init(filter: String) {
fetchRequest = FetchRequest<Singer>(entity: Singer.entity(), sortDescriptors: [], predicate: NSPredicate(format: "lastName BEGINSWITH %@", filter))
}

}

//struct FilteredList_Previews: PreviewProvider {
// static var previews: some View {
// }
//}

Singer+CoreDataClass.swift:

import Foundation
import CoreData

@objc(Singer)
public class Singer: NSManagedObject {

}

Singer+CoreDataProperties.swift:

import Foundation
import CoreData


extension Singer {

@nonobjc public class func fetchRequest() -> NSFetchRequest<Singer> {
return NSFetchRequest<Singer>(entityName: "Singer")
}

@NSManaged public var firstName: String?
@NSManaged public var lastName: String?

var wrappedFirstName : String {
firstName ?? "Unknown"
}

var wrappedLastName : String {
lastName ?? "Unknown"
}

}

核心数据有 1 个实体:Singer,具有 2 个字符串属性:firstName 和 lastName。模块:当前产品模块。 Codegen:手动/无。

当我尝试预览 ContentView 时,预览崩溃。当我打开崩溃日志文件并转到调试导航器时,我可以看到以下错误:

Dispatch queue: com.apple.main-thread (0)

上线:

FilteredList.swift:

var singers: FetchedResults<Singer> { fetchRequest.wrappedValue }

List(singers, id: \.self) { singer in

AppDelegate.swift:

class AppDelegate: UIResponder, UIApplicationDelegate {

可能是什么问题?包裹值(value)?代码有什么问题?

编辑:以下是我的 Dropbox 上崩溃日志文件的链接: crash-logs

最佳答案

对我有帮助的是将 struct ContentView_Previews(...) 更改为:

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return ContentView().environment(\.managedObjectContext, context)
}
}

我还将 struct FilteredList_Previews(...) 更改为:

struct FilteredList_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return FilteredList(filter: "A").environment(\.managedObjectContext, context)
}
}

感谢@Gergely,因为他在这里帮助我解决了类似的问题:https://stackoverflow.com/a/59629730/12315994

关于ios - FetchedResults 崩溃 : Dispatch queue: com. apple.main-thread (0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59463550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com