Crash with implicitly unwrapped optional on a simple setup #46

Closed
opened 2025-12-29 15:22:56 +01:00 by adam · 1 comment
Owner

Originally created by @rshev on GitHub (Apr 13, 2016).

I'm getting fatal error: unexpectedly found nil while unwrapping an Optional value on fetchAll in the following simple setup:

        do {
            try CoreStore.addSQLiteStoreAndWait()
            let users = CoreStore.fetchAll(From(User))
            print(users)
        }
        catch (let error) {
            print(error)
        }

Crash occurs in entityNameForClass func, and entity User clearly exists in the model.
What am I doing wrong?

PS. Maybe you shouldn't implicitly unwrap optional there at all? What if someone feeds wrong class?

Originally created by @rshev on GitHub (Apr 13, 2016). I'm getting `fatal error: unexpectedly found nil while unwrapping an Optional value` on `fetchAll` in the following simple setup: ``` do { try CoreStore.addSQLiteStoreAndWait() let users = CoreStore.fetchAll(From(User)) print(users) } catch (let error) { print(error) } ``` Crash occurs in `entityNameForClass` func, and entity `User` clearly exists in the model. What am I doing wrong? PS. Maybe you shouldn't implicitly unwrap optional there at all? What if someone feeds wrong class?
adam added the wontfix label 2025-12-29 15:22:56 +01:00
adam closed this issue 2025-12-29 15:22:57 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Apr 14, 2016):

@rshev Did you check if the User class is set correctly in your xcdatamodeld file? Note that the Module name matters.

You can inspect how your entities look like after calling addSQLiteStoreAndWait():

try CoreStore.addSQLiteStoreAndWait()
print(CoreStore.entityTypesByName)

PS. Maybe you shouldn't implicitly unwrap optional there at all? What if someone feeds wrong class?

Nobody should be able to perform operations on a wrong class (i.e. you shouldn't be fetching/creating a class that's not in your model), so catching this mistake early is best.

See the README file's entry on Creating Objects (yours is a fetch, but the same principles apply):

If the entity does not belong to any store, an assert will be triggered. This is a programmer error and should never occur in production code.
@JohnEstropia commented on GitHub (Apr 14, 2016): @rshev Did you check if the `User` class is set correctly in your xcdatamodeld file? Note that the `Module` name matters. You can inspect how your entities look like after calling `addSQLiteStoreAndWait()`: ``` swift try CoreStore.addSQLiteStoreAndWait() print(CoreStore.entityTypesByName) ``` > PS. Maybe you shouldn't implicitly unwrap optional there at all? What if someone feeds wrong class? Nobody should be able to perform operations on a wrong class (i.e. you shouldn't be fetching/creating a class that's not in your model), so catching this mistake early is best. See the README file's entry on [Creating Objects](https://github.com/JohnEstropia/CoreStore#creating-objects) (yours is a fetch, but the same principles apply): ``` If the entity does not belong to any store, an assert will be triggered. This is a programmer error and should never occur in production code. ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#46