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
266 stars 109 forks source link

Do not make unnecessary modifications to addedResources/removedResources #53

Closed krasnoukhov closed 8 years ago

krasnoukhov commented 8 years ago

This fixes the case when persisting changes given there were previously loaded resources. Otherwise invalid requests will be sent in attempt to create already existing relations or to remove inexistent.

krasnoukhov commented 8 years ago

@wvteijlingen Please let me know if this change is ok with you so I'll update the specs

wvteijlingen commented 8 years ago

Good point! Wouldn't this achieve the same thing but in a bit more readable way?

public func addResource(resource: Resource) {
    resources.append(resource)
    if let index = removedResources.indexOf(resource) {
        removedResources.removeAtIndex(index)
    } else {
        addedResources.append(resource)
    }
}
public func removeResource(resource: Resource) {
    resources = resources.filter { $0 !== resource }
    if let index = addedResources.indexOf(resource) {
        addedResources.removeAtIndex(index)
    } else {
        removedResources.append(resource)
    }
}
krasnoukhov commented 8 years ago

Good point! Commit is updated with better implementation and specs.

wvteijlingen commented 8 years ago

Nice work! I'm not sure but I think we should assert addedResources.isEmpty in testRemoveAdded() and removedResources.isEmpty in testAddRemoved() as well.

krasnoukhov commented 8 years ago

Makes sense, commit updated

wvteijlingen commented 8 years ago

Sweet!