mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
Dynamically create/fetch NSManagedObjects based on the Entity name #215
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 @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:
@JohnEstropia commented on GitHub (May 21, 2018):
You can manage the static and dynamic typing of fetches using this special
Intoinitializer:The generic type can be
NSManagedObjector 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.@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?
@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 (Jun 10, 2018):
I'm closing this issue for now. Let me know if you this wasn't resolved.