Dynamically create/fetch NSManagedObjects based on the Entity name #215

Closed
opened 2025-12-29 15:26:50 +01:00 by adam · 4 comments
Owner

Originally created by @Guferos on GitHub (May 20, 2018).

I have a case where I need to deserialize JSON into the Core Data objects and I tried to find a way to dynamically create/fetch objects based on their entity name string. The CoreStore API uses generics to create/fetch objects therefore the NSManagedObject subclass must be known. I thought about using native CoreData methods but it is not possible to access the NSManagedObjectContext from the transaction. Is there any way to create objects dynamically using string as the entityName and get the generic NSManagedObject? Something like:

...
 let object = transaction.create(intoEntityName: "User") // returns NSManagedObject
...
Originally created by @Guferos on GitHub (May 20, 2018). I have a case where I need to deserialize JSON into the Core Data objects and I tried to find a way to dynamically create/fetch objects based on their entity name string. The CoreStore API uses generics to create/fetch objects therefore the NSManagedObject subclass must be known. I thought about using native CoreData methods but it is not possible to access the NSManagedObjectContext from the transaction. Is there any way to create objects dynamically using string as the entityName and get the generic NSManagedObject? Something like: ``` ... let object = transaction.create(intoEntityName: "User") // returns NSManagedObject ... ```
adam added the question label 2025-12-29 15:26:50 +01:00
adam closed this issue 2025-12-29 15:26:50 +01:00
Author
Owner

@JohnEstropia commented on GitHub (May 21, 2018):

You can manage the static and dynamic typing of fetches using this special Into initializer:

 let object = transaction.create(Into<NSManagedObject>(User.self)) // returns NSManagedObject

The generic type can be NSManagedObject or any of its subclass, and you need to make sure that the dynamic type passed into the argument is any subclass of that generic type. The fetch result will always use the dynamic type.

@JohnEstropia commented on GitHub (May 21, 2018): You can manage the static and dynamic typing of fetches using this special `Into` initializer: ``` let object = transaction.create(Into<NSManagedObject>(User.self)) // returns NSManagedObject ``` The generic type can be `NSManagedObject` or any of its subclass, and you need to make sure that the dynamic type passed into the argument is any subclass of that generic type. The fetch result will always use the dynamic type.
Author
Owner

@Guferos commented on GitHub (May 21, 2018):

John, thanks for a very quick reply. I somehow missed that extra initializer. Is there any way to do the same when using the importUniqueObject: method or this cannot be achieved due to associatedType constraint?

@Guferos commented on GitHub (May 21, 2018): John, thanks for a very quick reply. I somehow missed that extra initializer. Is there any way to do the same when using the importUniqueObject: method or this cannot be achieved due to associatedType constraint?
Author
Owner

@JohnEstropia commented on GitHub (May 21, 2018):

Hmm, I'll need to check again, but import methods should have the same usage of Into<T> clauses.

@JohnEstropia commented on GitHub (May 21, 2018): Hmm, I'll need to check again, but import methods should have the same usage of `Into<T>` clauses.
Author
Owner

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

I'm closing this issue for now. Let me know if you this wasn't resolved.

@JohnEstropia commented on GitHub (Jun 10, 2018): I'm closing this issue for now. Let me know if you this wasn't resolved.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#215