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

Sorry for posting this as issue. #110

Closed sachin-sat closed 7 years ago

sachin-sat commented 7 years ago

Hi, I'm new to swift.

In this project, i just created a subclass for HTTPClient, hit a remote from there and added that client to spine's networkclient. Spine onsuccess closure gives me resources ---> is it correct/process? And there is no data{[]} in the relationship of the model from my REMOTE API(one to many concept - parent have data and child doesn't have. it has only self links - related and resource link in my case) - Is it possible to get child model's data using those remote links in spine.

Thanks in advance

wvteijlingen commented 7 years ago

If Spine returns the resources, you've probably done it correctly. You only need to subclass HTTPClient if you have specific networking requirements. Usually this is not the case.

Yes, you can get the related resources with a Query by using this initialiser: init(resourceType: T.Type, resourceCollection: ResourceCollection)

sachin-sat commented 7 years ago

You are the man, thanks dude. Let me working on them.

cheers.

sachin-sat commented 7 years ago

Hi Wvteijlingen, The word 'Persisting' makes me confuse. Spine manages any DB/file writing ? Shall I store them in my DB? if I store them in the DB means, how can I get related resources with a Query. I mean that "Should I convert my DB model into resourcecollection"?. Please advice me if i'm wrong.

Thanks in advance.

wvteijlingen commented 7 years ago

No, Spine doesn’t save anything locally. With “persisting" I mean POSTing or PUTting the resource to the server. The Resource class implements NSCoding so you could store the resources locally if you’d like (as a cache for example). But that is something you’d have to do yourself.

On 28 Sep 2016, at 06:30, sachin-sat notifications@github.com wrote:

Hi Wvteijlingen, The word 'Persisting' makes me confuse. Spine manages any DB/file writing ? Shall I store them in my DB? if I store them in the DB means, how can I get related resources with a Query. I should convert my DB model into resourcecollection. Please advice me if i'm wrong.

Thanks in advance.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wvteijlingen/Spine/issues/110#issuecomment-250069016, or mute the thread https://github.com/notifications/unsubscribe-auth/ABZJN_0g_QlQ3nVMaBZMW3xM8-q4Evjkks5que1WgaJpZM4KEzyE.

sachin-sat commented 7 years ago

Thanks dude. The reason for asking about storing is that 'as it needs resourcecollection to make query to get related models, storing the resourcecollection in the db/file and converting them into resourcecollection seems not good, right?' But I can get the related resources using init with PATH while making Query. it needs full URL Path(https://www.example.com/xx/yy/zz). Doesn't consider the URL path(the base URL while spine init). Is it proper/seems good?

Please advice me since I'm new to swift.

Thanks in advance

wvteijlingen commented 7 years ago

You can store the entire ResourceCollection if you want. You can also store just the URL and use that to initialise a new Query. Storing just the URL takes up less storage space.

sachin-sat commented 7 years ago

I have a attribute named 'description' in the JSON. As we know, 'description' is pre-defined name to get object's description. So i should use Field name formatters, right?

my code will look like this

public struct AllCapsKeyFormatter: KeyFormatter { public func format(field: Field) -> String { if(field.serializedName == "description") return "description_property" //My object's attribute name else return field.serializedName.uppercaseString } }

is this correct? will it cause any slowness in run time as it applies for every fields?

wvteijlingen commented 7 years ago

You do not need to use a KeyFormatter for this. You can simply define a different serialized named as follows:

override class var fields: [Field] {
  return fieldsFromDictionary([
    "description_property": Attribute().serializeAs("description")
  ])
}

The Swift property will now be called description_property, and it will map to a JSON key named description.

sachin-sat commented 7 years ago

seems great. Thanks buddy.