Uncaught Exception - KeyPath Not Found #296

Closed
opened 2025-12-29 15:28:20 +01:00 by adam · 3 comments
Owner

Originally created by @damianstrain on GitHub (Dec 8, 2019).

Hi John,

I am deleting an Entity by a specified key using the following code (CoreStore v6.3.2):

CoreStore.perform(
        asynchronous: {(transaction) -> Int in
            return try transaction.deleteAll(From<Project>(), Where<Project>(key, isEqualTo: value))
        },
        success: {(count) -> Void in
            print("Num deleted", count)
        },
        failure: {(error) -> Void in
           print("Error", error)
        }
)

An uncaught exception gets raised when an unknown Entity key is passed, specifically in NSManagedObjectContext+Querying.swift, line 568.

Screenshot 2019-12-07 at 23 33 26

I would have expected a CoreStoreError to be thrown? so that I may handle it in my failure closure. Unfortunately all I get is a SIGABRT and it crashes.

On a side note, I only came across this due to me mistyping the key 😃

Originally created by @damianstrain on GitHub (Dec 8, 2019). Hi John, I am deleting an Entity by a specified `key` using the following code (CoreStore v6.3.2): ``` CoreStore.perform( asynchronous: {(transaction) -> Int in return try transaction.deleteAll(From<Project>(), Where<Project>(key, isEqualTo: value)) }, success: {(count) -> Void in print("Num deleted", count) }, failure: {(error) -> Void in print("Error", error) } ) ``` An uncaught exception gets raised when an unknown Entity `key` is passed, specifically in `NSManagedObjectContext+Querying.swift`, line 568. <img width="983" alt="Screenshot 2019-12-07 at 23 33 26" src="https://user-images.githubusercontent.com/1780727/70381890-1c391a00-194a-11ea-9695-036463b52854.png"> I would have expected a `CoreStoreError` to be thrown? so that I may handle it in my `failure` closure. Unfortunately all I get is a `SIGABRT` and it crashes. On a side note, I only came across this due to me mistyping the `key` 😃
adam closed this issue 2025-12-29 15:28:20 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Dec 11, 2019):

@damianstrain What is key? The recommended syntax for this is using KeyPaths as those are type-safe:

try transaction.deleteAll(
    From<Project>().where(\.key ==value)
)
@JohnEstropia commented on GitHub (Dec 11, 2019): @damianstrain What is `key`? The recommended syntax for this is using KeyPaths as those are type-safe: ```swift try transaction.deleteAll( From<Project>().where(\.key ==value) ) ```
Author
Owner

@damianstrain commented on GitHub (Dec 11, 2019):

@JohnEstropia key in this case is uuid, and yes you are 100% correct, doing where(\.key ==value) is more type safe and I have since changed it to use KeyPaths.

But Im still curious though, shouldn't a CoreStoreError get thrown if the KeyPath is incorrect?

@damianstrain commented on GitHub (Dec 11, 2019): @JohnEstropia `key` in this case is `uuid`, and yes you are 100% correct, doing `where(\.key ==value)` is more type safe and I have since changed it to use KeyPaths. But Im still curious though, shouldn't a `CoreStoreError` get thrown if the KeyPath is incorrect?
Author
Owner

@JohnEstropia commented on GitHub (Jan 7, 2020):

The CoreStoreErrors are meant to be used for tracking misconfigurations in the stores or in the models. KeyPath mistakes are meant to be caught during compile time, and the type-safe syntax should be reliable enough to catch those. The type-erased dynamic utilities (like Where(_:isEqualTo:)) are meant for more complex uses, but leaves correctness as the caller's responsibility.

@JohnEstropia commented on GitHub (Jan 7, 2020): The `CoreStoreError`s are meant to be used for tracking misconfigurations in the stores or in the models. KeyPath mistakes are meant to be caught during compile time, and the type-safe syntax should be reliable enough to catch those. The type-erased dynamic utilities (like `Where(_:isEqualTo:)`) are meant for more complex uses, but leaves correctness as the caller's responsibility.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#296