Introducing Realm: Building Modern Swift Apps with Realm Database

Get started quickly with using Realm in your Swift apps with this free preview chapter from our new book: Realm: Building Modern Swift Apps with Realm Database! By Marin Todorov.

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Deleting Items

Last but not least, you’re going to let the user delete items from their list.

This is quite similar to adding and modifying items: you’re going to add a new method on ToDoItem and then add the relevant code in the view controller to react to user events.

Thanks to the two UITableViewDelegate methods already included in the starter code of ToDoListController, the table view already reacts to left-swipes and displays a red Delete button:

This provides a good starting point for this chapter’s last task. Let’s get down to business!

Open Entities/ToDoItem.swift and add one last method to the extension, below toggleCompleted():

func delete() {
  guard let realm = realm else { return }
  try! realm.write {
    realm.delete(self)
  }
}

Just like before, you get a reference to the object’s Realm and then start a write transaction to perform your updates.

Note: As you’ll learn later on, if you try modifying a persisted object without starting a write transaction, your code will throw an exception. You can only modify managed objects inside a Realm write transaction.

Since the class is a Realm object, you can simply call realm.delete(self) to delete the current object from the Realm.

Finally, you need to add a few more lines in your view controller to call your new delete() method. Back in Scenes/ToDoListController.swift add below toggleItem(_:):

func deleteItem(_ item: ToDoItem) {
  item.delete()
}

Then, scroll down to tableView(_:commit:forRowAt:) and append at the bottom:

deleteItem(item)

This will call your new deleteItem(_) method, which in turn invokes the delete() method on the to-do item.

Run the app one last time, swipe left on a to-do item, and tap Delete:

Just like the previous features you added, the deletion of the object from the Realm is reflected in the table, accompanied by a pleasant animation.

With that last piece of code, your simple CRUD application is complete. You’ve learned a bit about fetching objects from a Realm file, adding and modifying existing objects, and how to react to data changes and keeping your read and write code separate.

In fact, you already possess the knowledge to create simple Realm apps! However, since working with Realm has so many advantages, you’ll want to expand your knowledge as soon as possible. Worry not, we’ve got you covered. The rest of this book provides everything you’ll need to learn about Realm in detail.

Challenges

Challenge 1: Enhance Your To-Do app With More Features

To warm up for the next chapter, work through a few small tasks to polish your to-do application.

Start by modifying the existing code so it only allows the deletion of completed tasks. Way to simulate ticking items off the list!

Finally, add a feature which allows the user to tap a cell and be presented with an alert where they can edit the current to-do item’s text. Once they close the alert, the text change will be persisted, and the UI should be updated accordingly.

This chapter didn’t go into much detail in regards to the various available APIs, so don’t worry too much if you can’t figure out how to complete this challenge . You can open the challenge folder of this chapter and peek at the completed solution code. At this point, it’s not expected you can figure out everything on your own.

These challenges might not be very complex, but they’ll get you writing some simple Realm code to warm up for the grand tour of Realm’s object features in the next chapter.

Where to Go From Here?

If you enjoyed what you learned in this tutorial, why not check out the complete Realm: Building Modern Swift Apps with Realm Database book?

Realm finds the sweet spot between the simplicity of storing data as JSON on disk and using heavy, slow ORMs like Core Data or similar that are built on top of SQLite. The Realm Database aims to be fast, performant and provide the commodities that mobile developers need such as working with objects, type-safety, and native notifications.

In this book, you’ll do the following:

  • Learn how easy it is to set up your first Realm database.
  • See how to persist and read data under the CRUD model.
  • Discover how to work with Realm configurations.
  • Design smart and responsive migrations for your Realms.
  • Create a Realm Cloud instance and sync your data in real time, across all devices, anywhere.

Realm Database has been under active development for several years. It powers apps by some of the biggest names in the App Store, including Adidas, Amazon, Nike, Starbucks, BBC, GoPro, Virgin, Cisco, Groupon and many more who have chosen to develop their mobile apps with Realm.

Realm Platform is a relatively new commercial product which allows developers to automatically synchronize data not only across Apple devices but also between any combination of Android, iPhone, Windows, or macOS apps. Realm Platform allows you to run the server software on your own infrastructure and keep your data in-house which more often suits large enterprises. Alternatively, you can use Realm Cloud which runs a Platform for you and you start syncing data very quickly and only pay for what you use.

To celebrate the launch of the book, it’s currently on sale as part of our Advanced Swift Spring Bundle for a massive 40% off. But don’t wait too long, as this deal is only on until Friday, April 27.

If you have any questions or comments on this tutorial, feel free to join the discussion below!