Create NSManagedObject from AnyClass #218

Open
opened 2025-12-29 18:24:29 +01:00 by adam · 4 comments
Owner

Originally created by @mrichtsfeld on GitHub (Jun 23, 2018).

Hi @JohnEstropia ,

First of all thanks for your great library.

I have a question regarding the newest version. In Swift 3.x versions I was able to create a new NSManagedObject from AnyClass. This does not seem to work anymore with the newest libraries.

Here is a code extract I used which is not working anymore:

tx.create(Into(myCoreDataClass))

Can you please give me a hint on how to create an Object from an arbitrary class in the new version.

Thanks a lot

Originally created by @mrichtsfeld on GitHub (Jun 23, 2018). Hi @JohnEstropia , First of all thanks for your great library. I have a question regarding the newest version. In Swift 3.x versions I was able to create a new NSManagedObject from AnyClass. This does not seem to work anymore with the newest libraries. Here is a code extract I used which is not working anymore: `tx.create(Into(myCoreDataClass))` Can you please give me a hint on how to create an Object from an arbitrary class in the new version. Thanks a lot
Author
Owner

@JohnEstropia commented on GitHub (Jun 23, 2018):

@mrichtsfeld Into and From clauses are now generic types. See https://github.com/JohnEstropia/CoreStore/issues/257#issuecomment-397517052

@JohnEstropia commented on GitHub (Jun 23, 2018): @mrichtsfeld `Into` and `From` clauses are now generic types. See https://github.com/JohnEstropia/CoreStore/issues/257#issuecomment-397517052
Author
Owner

@mrichtsfeld commented on GitHub (Jun 23, 2018):

Thanks for the quick reply @JohnEstropia

I have changed the code now and it compiles.

tx.create(Into<AirplaneBase>(airplaneInfo.coreDataClass))

The airplaneInfo.coreDataClass is a subclass of AirplaneBase

Still it seems as if the proper object is not created. I feed the created class into an object monitor and get a crash at this line of code:

fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!

because the entitiyClass is NSManagedObject and not the expected subclass of AirplaneBase.

Do I misunderstand this?

Thanks a lot for any more help on this issue.

@mrichtsfeld commented on GitHub (Jun 23, 2018): Thanks for the quick reply @JohnEstropia I have changed the code now and it compiles. `tx.create(Into<AirplaneBase>(airplaneInfo.coreDataClass))` The airplaneInfo.coreDataClass is a subclass of AirplaneBase Still it seems as if the proper object is not created. I feed the created class into an object monitor and get a crash at this line of code: `fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!` because the entitiyClass is NSManagedObject and not the expected subclass of AirplaneBase. Do I misunderstand this? Thanks a lot for any more help on this issue.
Author
Owner

@JohnEstropia commented on GitHub (Jun 24, 2018):

@mrichtsfeld What type is returned from coreDataClass?
Normally, you can query using tx.create(Into<AirplaneBase>()), which means that you are fetching an AirplaneBase type and you are casting the result to AirplaneBase.

If you have a case where you want to fetch a type but cast it into another, you can write tx.create(Into<superclass type>(fetch type)). Usually you only want to do this if you are casting to a base class.

If you are not getting the correct type during fetch, then either your airplaneInfo.coreDataClass is returning the wrong class, or your entity is not in the model.

@JohnEstropia commented on GitHub (Jun 24, 2018): @mrichtsfeld What type is returned from `coreDataClass`? Normally, you can query using `tx.create(Into<AirplaneBase>())`, which means that you are fetching an `AirplaneBase` type and you are casting the result to `AirplaneBase`. If you have a case where you want to fetch a type but cast it into another, you can write `tx.create(Into<superclass type>(fetch type))`. Usually you only want to do this if you are casting to a base class. If you are not getting the correct type during fetch, then either your `airplaneInfo.coreDataClass` is returning the wrong class, or your entity is not in the model.
Author
Owner

@mrichtsfeld commented on GitHub (Jun 24, 2018):

@JohnEstropia the returned type is of type AT01which is a subtype for AirplaneBase.

But in this method fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!the entityClassis NSManagedObjectand not AirplaneBase. So the method entityDescriptiondoes not find anything which causes the following crash (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value)

The initiator is a call to tx.monitorObject(airplane) where airplaneis of type AirplaneBase.

Do I need to cast anything else or explicitly define a type?

Thanks again for your help.

@mrichtsfeld commented on GitHub (Jun 24, 2018): @JohnEstropia the returned type is of type `AT01`which is a subtype for `AirplaneBase`. But in this method `fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!`the `entityClass`is `NSManagedObject`and not `AirplaneBase`. So the method `entityDescription`does not find anything which causes the following crash (`Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value`) The initiator is a call to `tx.monitorObject(airplane)` where `airplane`is of type `AirplaneBase`. Do I need to cast anything else or explicitly define a type? Thanks again for your help.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#218