Swift 5 (Xcode Version 12.3): Program stopping at nonexistent breakpoint

I have some code that fetches entity model data in core-data, but when I built or debug in a simulator or phone the result will have

Thread 39: EXC_BREAKPOINT (code=1, subcode=0x1afa582ec)

and it is stopped at line 6 in the code bellow

Code Block
let context = self.managedObjectContext
 let fetchRequest: NSFetchRequest<SomeEntityModel> = SomeEntityModel.fetchRequest()
   
fetchRequest.predicate = NSPredicate(format: "id=%@", "001")
 
let fetchResult = try context.fetch(fetchRequest)


but, if I stop the debug, and run the app, not in the Xcode, the feature that develops those codes didn't stop, and it is running fine.

Does anyone have a solution to why this happens?


Accepted Reply

okay, this problem has been solved, I was noticed that I was using -com.apple.CoreData.ConcurrencyDebug 1 for debugging my core data thread process, and there is an issue where my NSManagedObjectContext was defined at the main thread.

the process that my core data processes occurred at background thread, so the exc_breakpoint appears because there is a violation in using dispatch in my core data process. I have understood this by reading about core data multithreading rules.

how I solve this problem by using performAndWait for my NSManagedObjectContext if I want still processing in the background thread, but if I change the process to the main thread it doesn't need to use performAndWait.

Code Block
...
DispatchQueue.global(qos: .background).async {
...
//fetch the previous data before replace or save the new one
let context = self.managedObjectContext
    context.performAndWait {
let fetchRequest: NSFetchRequest<SomeEntityModel> = SomeEntityModel.fetchRequest()
    fetchRequest.predicate = NSPredicate(format: "id=%@", "001")
  let fetchResult = try context.fetch(fetchRequest)
...
}
...
}
...
...
...
lazy var managedObjectContext: NSManagedObjectContext = {
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
     
    return managedObjectContext
  }()

Replies

EXC_BERAKPOINT is a type of Mach exception that is usually the signature of a crash. Please see Understanding the Exception Types in a Crash Report for what this exception means and some common reasons. How are you handling the errors form that try?
oh after I read the link that you have been attached, I noticed that I used dispatch for running this code, I am using dispatch for the background thread, but if I remove the dispatch method, the app running well.

do-catch that handles the error from try won't be print the error.


I think this problem happens because of my DispatchQueue.global(qos: .background).async. I use this to store data minutes in my music app when users pause or next to the next music. below is the code of how I use this qos background.

Code Block
...
DispatchQueue.global(qos: .background).async {
...
//fetch the previous data before replace or save the new one
let context = self.managedObjectContext
let fetchRequest: NSFetchRequest<SomeEntityModel> = SomeEntityModel.fetchRequest()   
fetchRequest.predicate = NSPredicate(format: "id=%@", "001") 
let fetchResult = try context.fetch(fetchRequest)
...
}
...


okay, this problem has been solved, I was noticed that I was using -com.apple.CoreData.ConcurrencyDebug 1 for debugging my core data thread process, and there is an issue where my NSManagedObjectContext was defined at the main thread.

the process that my core data processes occurred at background thread, so the exc_breakpoint appears because there is a violation in using dispatch in my core data process. I have understood this by reading about core data multithreading rules.

how I solve this problem by using performAndWait for my NSManagedObjectContext if I want still processing in the background thread, but if I change the process to the main thread it doesn't need to use performAndWait.

Code Block
...
DispatchQueue.global(qos: .background).async {
...
//fetch the previous data before replace or save the new one
let context = self.managedObjectContext
    context.performAndWait {
let fetchRequest: NSFetchRequest<SomeEntityModel> = SomeEntityModel.fetchRequest()
    fetchRequest.predicate = NSPredicate(format: "id=%@", "001")
  let fetchResult = try context.fetch(fetchRequest)
...
}
...
}
...
...
...
lazy var managedObjectContext: NSManagedObjectContext = {
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
     
    return managedObjectContext
  }()