dimanche 24 novembre 2019

How to dynamically parse multiple json files and store data permanently in Core Data

I have stored several files containing data in json format and I stored them in the bundle of my application. Each of these files contains the data related to a particular entity of my Core Data model, represented in a dictionary array format. I have called each of these files as the name of the entity related to the data it contains and I have made each entity conform to the Codable protocol. I am wondering if it is possible, by looping through the entitites (obtained thanks to the property entities of the NSManagedObjectModel class), search for the file relative to each entity, parse the data and save them permanently in the NSManagedObjectContext within which I am doing this operation. I can perform this operation correctly by calling the method:

try JSONDecoder().decode(<MyEntity>.self, from: data)

but I wish I could do it even without indicating the "MyEntity" class but obtaining it dynamically.

Thanks for your help





Aucun commentaire:

Enregistrer un commentaire