I'm trying to do simple fetch request of my TrainingSessionHistory
(coredata object) in one of my SwiftUI View but got Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
struct TrainingHistoryView: View {
var trainingSession : TrainingSession
init(trainingSession : TrainingSession) {
self.trainingSession = trainingSession
let fetchRequest = FetchRequest(fetchRequest: TrainingSessionHistory.getBy(nb: 2, myTrainingSession: self.trainingSession))
print(fetchRequest.wrappedValue) // -> crash this EXC_BAD_INSTRUCTION
}
TrainingSessionHistoryExtension : creation of fetch request
static func getBy(nb : Int, myTrainingSession : TrainingSession) -> NSFetchRequest<TrainingSessionHistory>{
let request:NSFetchRequest<TrainingSessionHistory> = TrainingSessionHistory.fetchRequest() as! NSFetchRequest<TrainingSessionHistory>
//request.predicate = NSPredicate(format: "trainingSession == %@", myTrainingSession)
request.fetchLimit = nb
let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
request.sortDescriptors = [sortDescriptor]
return request
}
I don't understand what is happen bad in the request :
Edit : Fixing
Replacing by
let req = TrainingSessionHistory.getBy(nb: 2, myTrainingSession: self.trainingSession)
self.alternativeContext.performAndWait {
do {
let result = try req.execute()
print(result)
}catch {
print(error)
}
}
where alternativeContext
define in delegate look like this :
lazy var alternativeContext: NSManagedObjectContext = {
let context = persistentContainer.newBackgroundContext()
return context
}()
All work but still don't understand why "FetchRequest.wrappedValue()" method wasn't working.