Crash using CoreData

Any suggestion that could point me in the right direction? This keeps crashing and doesn't seem to write data to the Coredata entity. Thank you

import SwiftUI
import CoreData

struct DataView: View {

@State var myDetail = String("")
@ObservedObject private var locationManager = LocationManager()
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: UserLocation.entity(), sortDescriptors:[NSSortDescriptor(key: "id", ascending: true)]) var userLocations: FetchedResults<UserLocation>
var body: some View {
    NavigationView {
            VStack {
                VStack {
                    TextField("Short Description where you are", text: $myDetail)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .foregroundColor(Color.blue)
                        .background(Color.blue)
                        .padding(10)
                        .border(Color.green, width: 5)
                        
                    Button("Add Location") {
                        
                        let myDate = self.getTodaysDate()
                        let myTime = self.getTodaysTime()
                        let myLatitude = self.getLatitude()
                        let myLongitude = self.getLongitude()
                        let myDetail = self.myDetail
                        
                        let userLocation = UserLocation(context: self.moc)
                        
                        userLocation.id = UUID()
                        userLocation.date = myDate
                        userLocation.time = myTime
                        userLocation.latitude = myLatitude
                        userLocation.longitude = myLongitude
                        userLocation.detail = myDetail
                        
                        try? self.moc.save()
                    }
                }
1 Like

What is the stack trace of the crash?

MyApp`main:
0x1003ccd54 <+0>: sub sp, sp, #0x40 ; =0x40
0x1003ccd58 <+4>: stp x29, x30, [sp, #0x30]
0x1003ccd5c <+8>: add x29, sp, #0x30 ; =0x30
0x1003ccd60 <+12>: mov x2, #0x0
0x1003ccd64 <+16>: stur w0, [x29, #-0x4]
0x1003ccd68 <+20>: mov x0, x2
0x1003ccd6c <+24>: stur x1, [x29, #-0x10]
0x1003ccd70 <+28>: bl 0x1003ccb1c ; type metadata accessor for MyTracingApp.AppDelegate at
0x1003ccd74 <+32>: str x1, [sp, #0x18]
0x1003ccd78 <+36>: bl 0x1003e2c84 ; symbol stub for: NSStringFromClass
0x1003ccd7c <+40>: mov x29, x29
0x1003ccd80 <+44>: bl 0x1003e2d38 ; symbol stub for: objc_retainAutoreleasedReturnValue
0x1003ccd84 <+48>: ldur x1, [x29, #-0x10]
0x1003ccd88 <+52>: ldur w8, [x29, #-0x4]
0x1003ccd8c <+56>: str x0, [sp, #0x10]
0x1003ccd90 <+60>: mov x0, x8
0x1003ccd94 <+64>: mov x9, #0x0
0x1003ccd98 <+68>: mov x2, x9
0x1003ccd9c <+72>: ldr x3, [sp, #0x10]
0x1003ccda0 <+76>: bl 0x1003e2c90 ; symbol stub for: UIApplicationMain
-> 0x1003ccda4 <+80>: ldr x1, [sp, #0x10]
0x1003ccda8 <+84>: str w0, [sp, #0xc]
0x1003ccdac <+88>: mov x0, x1
0x1003ccdb0 <+92>: bl 0x1003e2d20 ; symbol stub for: objc_release
0x1003ccdb4 <+96>: mov w8, #0x0
0x1003ccdb8 <+100>: mov x0, x8
0x1003ccdbc <+104>: ldp x29, x30, [sp, #0x30]
0x1003ccdc0 <+108>: add sp, sp, #0x40 ; =0x40
0x1003ccdc4 <+112>: ret

Thread 1: Exception: "-[__NSConcreteUUID compare:]: unrecognized selector sent to instance 0x283f395e0"

It looks like you’re trying to compare two UUIDs, not for equality but for ordering. That makes no sense. I’m not sure how you got into this state but creating an NSSortDescriptor with an id key seems suspicious. For example, this code will crash in a similar fashion:

let a = [UUID(), UUID(), UUID()]
let nsA = a as NSArray
let sortedNSA = nsA.sortedArray(using: [NSSortDescriptor(key: "self", ascending: true)])
print(sortedNSA)

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Perfect that solved my problem, thank you very much.

Instead of:
@FetchRequest(entity: UserLocation.entity(), sortDescriptors:[NSSortDescriptor(key: "id", ascending: true)]) var userLocations: FetchedResults

I am using:

@FetchRequest(entity: UserLocation.entity(), sortDescriptors:) var userLocations: FetchedResults