繁体   English   中英

核心数据获取请求错误

[英]Core Data Fetch Request Bug

我有一个使用Core Data构建的应用程序。 我使用Fetched Results Controller对所有Biking结构进行了初始加载。 然后我尝试使用获取请求发送检查特定Biking Struct的请求,但是我收到错误:谓词行上的Thread 1: EXC_BAD_ACCESS 我只是需要这个请求来检查是否保存了具有特定ID的结构。 我对这一切都是新手,请原谅我的无知! 我究竟做错了什么?

private let appDelegate = UIApplication.shared.delegate as! AppDelegate
private let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
private var fetchedRC: NSFetchedResultsController<Biking>!

private func refresh() {
    do {
        let request = Biking.fetchRequest() as NSFetchRequest<Biking>
        if query.isEmpty {
            request.predicate = NSPredicate(format: "bikingOwner == %@", note)

            let sort = NSSortDescriptor(key: #keyPath(Biking.buttonText), ascending: true)
            request.sortDescriptors = [sort]
            do {
                fetchedRC = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
                fetchedRC.delegate = self
                try fetchedRC.performFetch()
            } catch let error as NSError {
                print("Could not fetch. \(error), \(error.userInfo)")
            }
        }
    }
}

var bikingQuery = [Biking]()

var query = "test"

let fetchRequest = Biking.fetchRequest() as NSFetchRequest<Biking>
// ERROR Below
fetchRequest.predicate = NSPredicate(format: "buttonID == %@", query)
do {
    bikingQuery = try context.fetch(fetchRequest)
} catch {
    fatalError("Failed to fetch: \(error)")
}

if bikingQuery.count >= 0 {
    print("Found!")
}

替换谓词行

 let query = "test"
 fetchRequest.predicate = NSPredicate(format: "buttonID == %@", query)`

let query:Double = 2.0 // Any Value you want to search

fetchRequest.predicate = NSPredicate(format: "buttonID == %f", query)

有关详细信息,请参阅此链接

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1

希望它有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM