Sitemap

Manage Data Storage in iOS Apps with Realm

3 min readApr 25, 2024

--

In the realm of iOS app development, data management is a fundamental aspect that developers constantly grapple with. Whether it’s storing user preferences, managing user-generated content, or handling complex data structures, having a robust and efficient storage solution is crucial. Enter Realm, a powerful and flexible alternative to CoreData. In this article, we’ll explore how to harness the capabilities of Realm to implement a storage manager following the CRUD (Create, Read, Update, Delete) approach in Swift.

Press enter or click to view image in full size

Setting Up Realm

Before we delve into the nitty-gritty of CRUD operations with Realm, let’s ensure we have it set up in our project. Realm can be integrated into your project manually or through package managers like CocoaPods, Carthage, or Swift Package Manager (SPM). Once integrated, we’re ready to define our data model and initialize Realm.

Defining the Data Model

In our example, let’s consider a simple data model representing items in a list. We’ll create an Item class that subclasses Object, Realm's base class for data models.

import RealmSwift

class Item: Object {
@Persisted var id = UUID().uuidString
@Persisted var name = ""
@Persisted var description = ""
}

Here, we define an Item class with properties representing the item's ID, name, and description. Using @Persisted property wrappers ensures that these properties are managed by Realm.

Get Igor Pak’s stories in your inbox

Join Medium for free to get updates from this writer.

Implementing the Storage Manager

With our data model in place, let’s create a StorageManager class responsible for CRUD operations using Realm.

import RealmSwift

class StorageManager {
// MARK: - CRUD Operations

// Create
func addItem(name: String, description: String) {
let realm = try! Realm()
let newItem = Item()
newItem.name = name
newItem.description = description
try! realm.write {
realm.add(newItem)
}
}

// Read
func getAllItems() -> Results<Item> {
let realm = try! Realm()
return realm.objects(Item.self)
}

// Update
func updateItem(item: Item, newName: String, newDescription: String) {
let realm = try! Realm()
try! realm.write {
item.name = newName
item.description = newDescription
}
}

// Delete
func deleteItem(item: Item) {
let realm = try! Realm()
try! realm.write {
realm.delete(item)
}
}
}

In this StorageManager class:

  • We provide methods for adding, retrieving, updating, and deleting items.
  • The addItem method creates a new Item object, sets its properties, and saves it to the Realm database.
  • The getAllItems method retrieves all Item objects from the Realm database.
  • The updateItem method updates the properties of an existing Item object.
  • The deleteItem method deletes a specific Item object from the Realm database.

Conclusion

In this article, we’ve explored how to leverage Realm to implement a robust storage manager following the CRUD approach in iOS app development. Realm offers a seamless and efficient way to manage data, allowing developers to focus on building great user experiences without worrying about the intricacies of data storage. By integrating Realm into your projects, you can elevate your app’s performance and scalability while maintaining a clean and maintainable codebase. So, why wait? Dive into the world of Realm and unlock the full potential of your iOS apps!

--

--

Igor Pak
Igor Pak

Written by Igor Pak

iOS Developer | Fulbright & Muskie Alumnus | Advocate for Tech Accessibility & Inclusivity