mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-13 23:23:29 +01:00
Create NSManagedObject from AnyClass #218
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
@JohnEstropia commented on GitHub (Jun 23, 2018):
@mrichtsfeld
IntoandFromclauses are now generic types. See https://github.com/JohnEstropia/CoreStore/issues/257#issuecomment-397517052@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.
@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 anAirplaneBasetype and you are casting the result toAirplaneBase.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.coreDataClassis returning the wrong class, or your entity is not in the model.@mrichtsfeld commented on GitHub (Jun 24, 2018):
@JohnEstropia the returned type is of type
AT01which is a subtype forAirplaneBase.But in this method
fetchRequest.entity = context.parentStack!.entityDescription(for: EntityIdentifier(self.entityClass))!theentityClassisNSManagedObjectand notAirplaneBase. So the methodentityDescriptiondoes 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)whereairplaneis of typeAirplaneBase.Do I need to cast anything else or explicitly define a type?
Thanks again for your help.