EXC_BAD_INSTRUCTION

public static func fetch(in context: NSManagedObjectContext, configurationBlock: (NSFetchRequest<Self>) -> () = { _ in }) -> [Self] { let request = NSFetchRequest<Self>(entityName: Self.entityName) configurationBlock(request)

    return try! context.fetch(request) 
}

context.fetch(request), 'fetch' function has error. Thread 24: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Take a look at the documentation for debugging techniques that has guidance on debugging common reasons for a crash. If you get stuck, let us know what you tried, and attach the fully symbolicated Apple crash report in your reply. Posting a Crash Report explains how to do so.

— Ed Ford,  DTS Engineer

This error does not occur on every call。

Let the lldb of Xcode that input 'po context.fetch(request)'. and I get the messages:

error: <EXPR>:8:15: error: cannot convert value of type 'NSFetchRequest<ResultType>' to expected argument type 'NSFetchRequest<any NSFetchRequestResult>' context.fetch(request) ^ <EXPR>:8:15: note: arguments to generic parameter 'ResultType' ('ResultType' and 'any NSFetchRequestResult') are expected to be equal

It would be more helpful to start with the crash report to start with, since it sounds like you're trying to debug the reason for the EXC_BAD_INSTRUCTION crash. Since you're at a breakpoint in LLDB, you can run the detach command, which will allow the crash to complete, rather than be intercepted by LLDB. That way, the crash report will be stored, and you can retrieve it from disk to share here. All of the info you need to collect the report and then post it here is in Posting a crash report.

— Ed Ford,  DTS Engineer

when I use detach command and I get this:

Ed and I discussed the issue behinds the scenes, and so I am chiming in to comment the following error from the Core Data perspective. Regarding the following error:

error: <EXPR>:8:15: error: cannot convert value of type 'NSFetchRequest<ResultType>' to expected argument type 'NSFetchRequest<any NSFetchRequestResult>' context.fetch(request) ^ <EXPR>:8:15: note: arguments to generic parameter 'ResultType' ('ResultType' and 'any NSFetchRequestResult') are expected to be equal

This seems to indicate that you were passing an NSFetchRequest object tied to one entity as an argument to a closure (configurationBlock?) and the closure expects (or infers) a fetch request tied to the other entity.

Since the potential type mismatch happens in runtime, it's hard to reason the why by simply reading the piece of code. Do you have a minimal sample project that reproduce the issue? You mentioned that the error "does not occur on every call," but since you've figured out the line triggering the crash and use lldb to trigger the crash report, I'd think that you have a reasonable way to reproduce the issue.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for discussed the issue.

I will try to describe this problem in more detail.

The Self is custom class 'Managed', It's a subclass of NSManagedObject.

and I made some changes to delete ‘configurationBlock(request)’.

so the new code:

public static func fetch(in context: NSManagedObjectContext, 
   configurationBlock:  (NSFetchRequest<Self>) -> () = { _ in } )  -> [Self] {
    let request = NSFetchRequest<Self>(entityName: Self.entityName)
    return try! context.fetch(request)  
}

it become a simplest CoreData query code.

I have called this function dozens of times and the problem with it occurs again and I still use the lldb of Xcode that input 'po context.fetch(request)'.

error: <EXPR>:8:15: error: cannot convert value of type 'NSFetchRequest<ResultType>' to expected argument type 'NSFetchRequest<any NSFetchRequestResult>' context.fetch(request) ^

<EXPR>:8:15: note: arguments to generic parameter 'ResultType' ('ResultType' and 'any NSFetchRequestResult') are expected to be equal context.fetch(request) ^

I call this method many times in the same way that it successful. I confuse why did it fail.

I seem to have found the wrong way to trigger, in my original code I used the Promise framework and I put the 'fetch' method into the asynchronous closure of the Promise, and when I trigger function something, The asynchronous closure of the Promise will be called several times (the closure will call back after it completes the query). Of course, with many successes, sometimes an error will be reported. I don't understand what went wrong.

If you can't find a solution, please provide some ideas to help me find the problem. My project is too complicated, and a simple Demo doesn't seem to trigger this problem.

EXC_BAD_INSTRUCTION
 
 
Q