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

In place record update #443

Merged
merged 13 commits into from
Nov 22, 2018
Merged

In place record update #443

merged 13 commits into from
Nov 22, 2018

Conversation

groue
Copy link
Owner

@groue groue commented Nov 21, 2018

This pull request introduces a new way to perform database updates: updateChanges(_:with:).

// Mutates the record according to the provided closure, and then, if the
// record has any difference from its previous version, executes an
// UPDATE statement so that those differences and only those difference are
// saved in the database.
if var player = try Player.fetchOne(db, key: 42) {
    try player.updateChanges(db) {
        $0.score += 10
        $0.hasAward = true
    }
}

updateChanges(_:with:) returns whether the record was changed or not:

let modified = try player.updateChanges(db) {
    $0.firstName = "Arthur"
    $0.lastName = "Smith"
}
if modified {
    print("Player was modified, and updated in the database")
} else {
    print("Player was not modified")
}

@groue groue merged commit a2af4fb into development Nov 22, 2018
@groue groue added this to the GRDB 3.6.0 milestone Nov 22, 2018
@groue groue deleted the feature/updateChanges branch December 8, 2018 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant