wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
264 stars 109 forks source link

[WIP] Change resource to protocol #154

Open mathebox opened 7 years ago

mathebox commented 7 years ago

Hi @wvteijlingen,

this is my attempt to change Resource to a protocol. Please have a look. The code is still kind of messy and really not ready to be merged. But the tests are passing :tada:

I made the following changes

Next steps / further improvements

Additional notes We should distinguish between Attribute and Relationship of Resource objects. Currently they are returned as Field instances. Therefore the Field protocol contains some methods which are only used when working with relationships.

What's do we need to support CoreData? (This should probably be moved to a separate issue.)

Hopefully I have covered every change I made. What are your thoughts?

markst commented 7 years ago

151

markst commented 7 years ago

Hey @mathebox, thanks for making a start at this. I was trying to pickup where you left off. For starters I wasn't able to implement the protocol without having to conform entirely to Resource protocol. This seems to be due extension Resource not being public where as the Resource protocol is.

markst commented 7 years ago

Also seems to break the ability to sequence a bunch of BrightFuture futures: [self.spine.delete(self.fitting),self.spine.save(self.fitting)].sequence().onSuccess(callback: { (resources) in

mmoonport commented 7 years ago

Hello guys. I have worked on a a couple ORM's and wonder if there is anything I could lend a hand with. We are looking at using this library but the "offline mode" abilities really should be moved to CoreData and thus, here we are.

mmoonport commented 7 years ago

After doing some research into this issue for our own uses. We have come up with an idea of using spine in concert with core data, abstracted behind NSIncrementalStore. This will allow us to use core data natively with the ability to have our "back-end" be a spine-compliant API. AFNetworking once had a AFIncrementalStore that used AFRESTClient. I believe this could be easily ported to be used with spine. The only thing that would make sense to me is allow the incremental store the ability to create a "Resource" Class by inspecting a NSManagedObject for which fields it has and what types they are when it begins the store in loadMetadata. This would prevent the need for both and give a user wanting to persist with core-data a backend connected to their api. I am hoping that @wvteijlingen could guide me in the right direction as to the dynamic creation of a resource.

markst commented 6 years ago

@mmoonport hey. I've forked this PR branch into our own & have written persistence using Realm. It isn't amazing; but it works for our needs. I'm keen to write a more robust persistence layer on-top of Spine; but it seems development & activity has gone stale.