开发者社区> 问答> 正文

FetchedResults崩溃:调度队列:com.apple.main-thread(0)

我遵循本教程学习动态过滤器和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 {
//    }
//}

歌手+CoreDataClass.斯威夫特:

import Foundation
import CoreData

@objc(Singer)
public class Singer: NSManagedObject {

}

歌手+CoreDataProperties.Swive:

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"
    }

} 核心数据有一个实体:具有两个字符串属性的歌手:FirstName和LastName。模块:当前产品模块。手册/无。

当我尝试预览内容视图时,预览崩溃。当我打开崩溃日志文件并转到DebugNavigator时,我可以看到以下错误:

分派队列:com.apple.main-线程(0)

在线:

FilteredList.swift:

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

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

class AppDelegate: UIResponder, UIApplicationDelegate {

会有什么问题呢?WrappedValue?代码有什么问题?

展开
收起
游客5akardh5cojhg 2019-12-24 12:58:06 1354 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
How to Build a Successful Data 立即下载
Why you should care about data layout in the file system 立即下载
Spark SQL:Past Present &Future 立即下载