- February 24, 2024
- Posted by: Harshil Vyas
- Category: Mobile Development
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.