Using Realm database in iOS Swift Apps
Realm is a popular open-source mobile database solution that provides an alternative to traditional SQLite databases in iOS, macOS, tvOS, and watchOS Applications. It offers a simple and efficient way to persist data locally on the device and perform complex queries and transactions.
In this blog, we will learn how to integrate and use the Realm database in iOS app to manage data storage and retrieval.
Step 1: Installing Realm
To start using Realm in your iOS app, you need to install the Realm Swift library. There are multiple ways to install Realm, but the recommended method is using CocoaPods, Carthage, or Swift Package Manager for iOS projects.
To start using Realm in your iOS app, you need to install the Realm Swift library. There are multiple ways to install Realm, but the recommended method is using CocoaPods, Carthage, or Swift Package Manager for iOS projects.
- Open Terminal and go to your project directory.
- If you haven’t already started your project with CocoaPods, run the command: pod init. This will make a 𝙿𝚘𝚍𝚏𝚒𝚕𝚎 for your project.
- Open the Podfile using a text editor and add the following line inside the target block:
𝚙𝚘𝚍 ‘𝚁𝚎𝚊𝚕𝚖𝚂𝚠𝚒𝚏𝚝’
- Save the Podfile and run the command: pod install in Terminal.
- Await for CocoaPods to download and install the RealmSwift library. Once it is finished, close your Xcode project and open the running generated .𝚡𝚌𝚠𝚘𝚛𝚔𝚜𝚙𝚊𝚌𝚎 file.
Step 2: Setting up Realm in iOS Project.
After installing Realm, you need to configure it in your iOS project. Follow these steps to set up Realm in your application development project:
- In Xcode, open your project .𝚡𝚌𝚠𝚘𝚛𝚔𝚜𝚙𝚊𝚌𝚎 𝚏𝚒𝚕𝚎.
- Create a new Swift file (e.g., 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚠𝚒𝚏𝚝) to manage your Realm configuration and interactions.
- signify the RealmSwift library at the top of the file:𝚒𝚖𝚙𝚘𝚛𝚝 𝚁𝚎𝚊𝚕𝚖𝚂𝚠𝚒𝚏𝚝
- Declare a class named RealmWrapper and add the following code:𝚏𝚒𝚗𝚊𝚕 𝚌𝚕𝚊𝚜𝚜 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛 {
𝚜𝚝𝚊𝚝𝚒𝚌 𝚕𝚎𝚝 𝚜𝚑𝚊𝚛𝚎𝚍 = 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛() // 𝚂𝚒𝚗𝚐𝚕𝚎𝚝𝚘𝚗 𝚒𝚗𝚜𝚝𝚊𝚗𝚌𝚎
𝚙𝚛𝚒𝚟𝚊𝚝𝚎 𝚕𝚎𝚝 𝚛𝚎𝚊𝚕𝚖: 𝚁𝚎𝚊𝚕𝚖
𝚙𝚛𝚒𝚟𝚊𝚝𝚎 𝚒𝚗𝚒𝚝() {
// 𝙶𝚎𝚝 𝚝𝚑𝚎 𝚍𝚎𝚏𝚊𝚞𝚕𝚝 𝚁𝚎𝚊𝚕𝚖 𝚌𝚘𝚗𝚏𝚒𝚐 𝚘𝚋𝚓𝚎𝚌𝚝
𝚐𝚞𝚊𝚛𝚍 𝚕𝚎𝚝 𝚛𝚎𝚊𝚕𝚖 = 𝚝𝚛𝚢? 𝚁𝚎𝚊𝚕𝚖() 𝚎𝚕𝚜𝚎 {
𝚏𝚊𝚝𝚊𝚕𝙴𝚛𝚛𝚘𝚛(“𝙵𝚊𝚒𝚕𝚎𝚍 𝚝𝚘 𝚒𝚗𝚒𝚝𝚒𝚊𝚕𝚒𝚣𝚎𝚍 𝚁𝚎𝚊𝚕𝚖”)
}
𝚜𝚎𝚕𝚏.𝚛𝚎𝚊𝚕𝚖 = 𝚛𝚎𝚊𝚕𝚖
}
}
Step 3: Creating a Realm Object
In Realm, data is organized into objects, like tables in a traditional database. Each Realm object represents a row in the database table and makes the programming in swift easy.
Follow these steps to build a Realm object in your iOS app:
- Create a new Swift file (e.g., 𝙿𝚎𝚛𝚜𝚘𝚗.𝚜𝚠𝚒𝚏𝚝) to define your Realm object.
- signify the RealmSwift data library at the uppermost of the file:
𝙿𝚎𝚛𝚜𝚘𝚗.𝚜𝚠𝚒𝚏𝚝
- Declare a new class and inherit from the Object class provided by Realm:
𝚏𝚒𝚗𝚊𝚕 𝚌𝚕𝚊𝚜𝚜 𝙿𝚎𝚛𝚜𝚘𝚗: 𝙾𝚋𝚓𝚎𝚌𝚝 {
@𝙿𝚎𝚛𝚜𝚒𝚜𝚝𝚎𝚍(𝚙𝚛𝚒𝚖𝚊𝚛𝚢𝙺𝚎𝚢: 𝚝𝚛𝚞𝚎) 𝚟𝚊𝚛 𝚒𝚍: 𝙾𝚋𝚓𝚎𝚌𝚝𝙸𝚍 // 𝙿𝚛𝚒𝚖𝚊𝚛𝚢 𝚔𝚎𝚢
@𝙿𝚎𝚛𝚜𝚒𝚜𝚝𝚎𝚍 𝚟𝚊𝚛 𝚗𝚊𝚖𝚎: 𝚂𝚝𝚛𝚒𝚗𝚐 = “”
@𝙿𝚎𝚛𝚜𝚒𝚜𝚝𝚎𝚍 𝚟𝚊𝚛 𝚊𝚐𝚎: 𝙸𝚗𝚝 = 0
} - Customizing the properties and their types according to your app’s requirements. The @𝙿𝚎𝚛𝚜𝚒𝚜𝚝𝚎𝚍 attribute relates to a property for persistence in the Realm database.
Step 4: Executing CRUD Operations
Now that you have set up Realm and defined a Realm object, you can execute CRUD (Create, Read, Update, Delete) operations on your data. Follow these steps to execute basic CRUD operations:
- To add a new object to the Realm database, use the below code:
𝚕𝚎𝚝 𝚘𝚋𝚓𝙿𝚎𝚛𝚜𝚘𝚗 = 𝙿𝚎𝚛𝚜𝚘𝚗()
𝚘𝚋𝚓𝙿𝚎𝚛𝚜𝚘𝚗.𝚗𝚊𝚖𝚎 = “𝙹𝚘𝚢 𝙶𝚘𝚗𝚜𝚊𝚕𝚟𝚎𝚜”
𝚘𝚋𝚓𝙿𝚎𝚛𝚜𝚘𝚗.𝚊𝚐𝚎 = 𝟹0
𝚝𝚛𝚢? 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖.𝚠𝚛𝚒𝚝𝚎 {
𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖.𝚊𝚍𝚍(𝚘𝚋𝚓𝙿𝚎𝚛𝚜𝚘𝚗)
}
2. For fetching all objects of a specific type, use the below code:
𝚕𝚎𝚝 𝚙𝚎𝚛𝚜𝚘𝚗𝚜 = 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖.𝚘𝚋𝚓𝚎𝚌𝚝𝚜(𝙿𝚎𝚛𝚜𝚘𝚗.𝚜𝚎𝚕𝚏)
𝚏𝚘𝚛 𝚙𝚎𝚛𝚜𝚘𝚗 𝚒𝚗 𝚙𝚎𝚛𝚜𝚘𝚗𝚜 {
𝚙𝚛𝚒𝚗𝚝(“𝙿𝚎𝚛𝚜𝚘𝚗 𝙽𝚊𝚖𝚎: \(𝚙𝚎𝚛𝚜𝚘𝚗.𝚗𝚊𝚖𝚎)”)
𝚙𝚛𝚒𝚗𝚝(“𝙿𝚎𝚛𝚜𝚘𝚗 𝙰𝚐𝚎: \(𝚙𝚎𝚛𝚜𝚘𝚗.𝚊𝚐𝚎 ?? 0)”)
}
3. For fetching an object by its 𝚒𝚍, use the below code:
𝚏𝚞𝚗𝚌 𝚏𝚎𝚝𝚌𝚑𝙿𝚎𝚛𝚜𝚘𝚗𝙱𝚢𝙸𝚍(𝚒𝚍: 𝙾𝚋𝚓𝚎𝚌𝚝𝙸𝚍) -> 𝙿𝚎𝚛𝚜𝚘𝚗? {
𝚛𝚎𝚝𝚞𝚛𝚗 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖
.𝚘𝚋𝚓𝚎𝚌𝚝(𝚘𝚏𝚃𝚢𝚙𝚎: 𝙿𝚎𝚛𝚜𝚘𝚗.𝚜𝚎𝚕𝚏, 𝚏𝚘𝚛𝙿𝚛𝚒𝚖𝚊𝚛𝚢𝙺𝚎𝚢: 𝚒𝚍)
}
- For fetching objects by name, use the below code:
𝚏𝚞𝚗𝚌 𝚏𝚎𝚝𝚌𝚑𝙿𝚎𝚛𝚜𝚘𝚗𝚜𝙱𝚢𝙽𝚊𝚖𝚎(𝚗𝚊𝚖𝚎: 𝚂𝚝𝚛𝚒𝚗𝚐) -> 𝚁𝚎𝚜𝚞𝚕𝚝𝚜<𝙿𝚎𝚛𝚜𝚘𝚗>? {
𝚕𝚎𝚝 𝚙𝚛𝚎𝚍𝚒𝚌𝚊𝚝𝚎 = 𝙽𝚂𝙿𝚛𝚎𝚍𝚒𝚌𝚊𝚝𝚎(𝚏𝚘𝚛𝚖𝚊𝚝: “𝚗𝚊𝚖𝚎 == %@”, 𝚗𝚊𝚖𝚎
𝚛𝚎𝚝𝚞𝚛𝚗 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖
.𝚘𝚋𝚓𝚎𝚌𝚝𝚜(𝙿𝚎𝚛𝚜𝚘𝚗.𝚜𝚎𝚕𝚏).𝚏𝚒𝚕𝚝𝚎𝚛(𝚙𝚛𝚎𝚍𝚒𝚌𝚊𝚝𝚎)
}
- For updating an existing object, modify its properties and save the changes with below code:
𝚒𝚏 𝚕𝚎𝚝 𝚘𝚋𝚓𝙿𝚎𝚛𝚜𝚘𝚗 = 𝚙𝚎𝚛𝚜𝚘𝚗𝚜.𝚏𝚒𝚛𝚜𝚝 {
𝚝𝚛𝚢? 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖.𝚠𝚛𝚒𝚝𝚎 {
𝚘𝚋𝚓𝙿𝚎𝚛𝚜𝚘𝚗.𝚗𝚊𝚖𝚎 = “𝙹𝚘𝚑𝚗 𝚐𝚘𝚗𝚜𝚊𝚕𝚟𝚎𝚜”
}
}
6. For deleting an object from the Realm database, use the below code:
𝚒𝚏 𝚕𝚎𝚝 𝚘𝚋𝚓𝙿𝚎𝚛𝚜𝚘𝚗 = 𝚙𝚎𝚛𝚜𝚘𝚗𝚜.𝚏𝚒𝚛𝚜𝚝 {
𝚝𝚛𝚢? 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖.𝚠𝚛𝚒𝚝𝚎 {
𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖.𝚍𝚎𝚕𝚎𝚝𝚎(𝚘𝚋𝚓𝙿𝚎𝚛𝚜𝚘𝚗)
}
}
Step 5: Advanced Realm Features
Realm offers additional features to handle more complex scenarios. Here are a few examples:
- Relationships: You can create relationships between Realm objects using properties such as 𝙻𝚒𝚗𝚔𝚒𝚗𝚐𝙾𝚋𝚓𝚎𝚌𝚝𝚜 or 𝚁𝚎𝚊𝚕𝚖𝙾𝚙𝚝𝚒𝚘𝚗𝚊𝚕. introduce to the Realm documentation for detailed examples.
- Queries: Realm provides a powerful query API to fetch objects based on specific criteria. For example:
𝚕𝚎𝚝 𝚙𝚎𝚛𝚜𝚘𝚗𝚜 = 𝚁𝚎𝚊𝚕𝚖𝚆𝚛𝚊𝚙𝚙𝚎𝚛.𝚜𝚑𝚊𝚛𝚎𝚍.𝚛𝚎𝚊𝚕𝚖.𝚘𝚋𝚓𝚎𝚌𝚝𝚜(𝙿𝚎𝚛𝚜𝚘𝚗.𝚜𝚎𝚕𝚏).𝚏𝚒𝚕𝚝𝚎𝚛(“𝚊𝚐𝚎 > 𝟷𝟾”)
- Notifications: You can observe changes in Realm objects using notifications. This allows your app to stay updated with real-time changes created by other parts of the app or remote data sources. Follow the Realm documentation for more information.
Conclusion
In this blog, we learn the basics of using Realm in iOS apps. We learned how to install Realm, set up our project, create Realm objects, and Altering with CRUD operations. We also briefly touched upon advanced features like relationships, queries, and notifications.
Realm provides a robust and efficient solution for data persistence and swift advantage more useful for iOS application development, offering a wide range of features to simplify the database management system (DBMS). Feel free to approach the Realm documentation for more in-depth usage and examples.





