Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Removes emptyObject requirement #249

Merged
merged 2 commits into from
Oct 3, 2021
Merged

feat: Removes emptyObject requirement #249

merged 2 commits into from
Oct 3, 2021

Conversation

cbaker6
Copy link
Contributor

@cbaker6 cbaker6 commented Oct 3, 2021

New Pull Request Checklist

Issue Description

#243 required all ParseObjects to have an init() due to the addition of emptyObject. This reverts that requirement and instead provides emptyObject as a recommendation to the developer.

Developers should feel empowered to add properties like emptyObject which require new instances of ParseObject’s on their own depending on their respective use cases. In addition, developers can also add extensions and custom methods. It is not the intent for the Swift SDK to attempt to do everything on behalf of the developer as the SDK’s design is enable and empower by maximizing the Swift and Parse has to offer.

Related issue: #247

Approach

Allow developer to add emptyObject computed property to ParseObject‘s on their own. emptyObject is useful if you only want to send modified or particular values to the server instead of the whole object:

    /*:
     It's recommended the developer adds the emptyObject computed property or similar.
     Gets an empty version of the respective object. This can be used when you only need to update a
     a subset of the fields of an object as oppose to updating every field of an object.
     - note: Using an empty object and updating a subset of the fields reduces the amount of data sent between
     client and server when using `save` and `saveAll` to update objects.
    */
    var emptyObject: Self {
        var object = Self() // Or any init of your object you want. Note, ideally you want an empty init so you don’t send unnecessary keys.
        object.objectId = objectId
        object.createdAt = createdAt
        return object
    }

Which then allows you to do:

//: Define initial GameScores.
let score = GameScore(score: 10)

/*: Save asynchronously (preferred way) - Performs work on background
    queue and returns to specified callbackQueue.
    If no callbackQueue is specified it returns to main queue.
*/
score.save { result in
    switch result {
    case .success(let savedScore):

        /*: To modify, need to make it a var as the value type
            was initialized as immutable. Using `emptyObject`
            allows you to only send the updated keys to the
            parse server as opposed to the whole object.
        */
        var changedScore = savedScore.emptyObject
        changedScore.score = 200
        changedScore.save { result in
            switch result {
            case .success:
                /*: Only the modified keys were sent to produce a successful save.
                */

            case .failure(let error):
                assertionFailure("Error saving: \(error)")
            }
        }
    case .failure(let error):
        assertionFailure("Error saving: \(error)")
    }
}

See #243 for more details…

TODOs before merging

  • Add entry to changelog
  • Update to Jazzy docs 0.14.0

@parse-github-assistant
Copy link

parse-github-assistant bot commented Oct 3, 2021

Thanks for opening this pull request!

  • 🎉 We are excited about your hands-on contribution!

@cbaker6 cbaker6 merged commit 51dfbe1 into main Oct 3, 2021
@cbaker6 cbaker6 deleted the removeEmptyReq branch October 3, 2021 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant