Deleting a Shopping List
To delete a Shopping List is similar to adding. We need to define a new delete method on the Shopping List class. In that method, make a DELETE request to the /shopping_lists/:id endpoint where :id is the ID of our Shopping List model. We already have to delete implemented in our Table View Controller but we need to invoke this new delete method from our Table View Controller, and, on a successful DELETE request to our API server, we need to update the Table View by reloading it. To add the delete functionality, we need to follow these steps:
- Open the
ShoppingList.swiftfile from our iOS project and add the followingdeletemethod to the class. In this method, we make the request by specifying theDELETEHTTP method and also pass theidof the Shopping List we want to delete in the URL. On completion, we call theonCompletionclosure function passed as part of thedeletemethod invocation:
func delete(onCompletion: @escaping () -> Void) {
request(url: "/https/www.packtpub.com/shopping_lists...