loganwright / Genome

A simple, type safe, failure driven mapping library for serializing JSON to models in Swift 3.0 (Supports Linux)
MIT License
762 stars 58 forks source link

Core Data initialization #86

Open lumo2707 opened 7 years ago

lumo2707 commented 7 years ago

Can you provide us how to use Genome for core data? Which protocol/class we have to use?

According to your Readme: NSMappableManagedObject is not avaiable.

couchdeveloper commented 7 years ago

I did some experiments with the Core Data extension - but it appears to me that it doesn't work as expected - at least with Swift 3.0 and Swift 3.1.

I came up with a working solution in this fork. It uses a different approach: instead of a base class, it uses a protocol and an extension with default implementations. That way, we do not need to modify the sources for the managed objects which are generated by Xcode. This also fixed an issue in a default implementation of an extension function, which would not properly have the dynamic type Self - but rather always only the type to which it is constrained (NSManagedObject) - leading to not get the proper name for the Entity. Also, there seems to be a bug in the code which does not create a managed object - or I'm missing something, or there is a missing extension which is not included in the sources.

So, I quite radically changed the file CoreData.swift. Once that worked, it turned out that there's an issue with passing the context parameter properly to "children". This required a fix in the underlying dependency Node based on version 1.0, for which I created this fork. (Note: Now, it seems, Node v2 will fix this - which is alpha or even beta).

Once that has been fixed, the Genome project needed to be made compatible for Carthage again. I also found a fix for this, but requires Swift 3.1. (This has something to do with how and where SwiftPM stores its build products, and the project uses a pre-build script where the path to the build folder is used).

Now, the Genome project can be used from Carthage as usual (but requires Swift 3.1) and the serialisation of managed objects variables within managed objects to and from JSON now feels much more easier, too.

Before making a PR to Genome, I would like to test it with my team-mates in our projects. Maybe, Node 2 will be available soon and then things may change again dramatically.