That was surprisingly easy! We have yet another trigger in our database, this time for making sure that newly created reminders lists are inserted in the right spot. We hope this is giving all of our viewers some inspiration for figuring out where little bits of logic can be extracted out of your app code and put into the database layer, where it can truly be enforced 100% of the time.
Let’s play around with another trigger, and this one will be a little different. Let’s say that we only want users to be able to associate at most 5 tags with a reminder. This kind of validation logic is easy enough to implement in SwiftUI. When the user taps the “Save” button we can simply check how many tags are assigned, and if it’s more than 5 we refuse to actually save the data and display an alert.
But that is a weak enforcement of this rule. Nothing is stopping other parts of the code base from sneaking in some extra tags, breaking the rule that at most 5 tags should be associated with a reminder. A better way to enforce this would be to install a trigger that makes sure one is never allowed to assign more than 5 tags to a single reminder.
And it’s surprising just how easy it is to implement this with triggers. But, first, we don’t even currently have a way for users to associate tags with reminders in our app. We never built out that part of the UI. So let’s do that, and then write a trigger.