How to Use Realm Database in iOS App 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.

  1. Open Terminal and go to your project directory.
  2. If you haven’t already started your project with CocoaPods, run the command: pod init. This will make a π™Ώπš˜πšπšπš’πš•πšŽΒ for your project.
  3. Open the Podfile using a text editor and add the following line inside the target block:

πš™πš˜πš β€˜πšπšŽπšŠπš•πš–πš‚πš πš’πšπš’

  1. Save the Podfile and run the command: pod install in Terminal.
  2. 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:

  1. In Xcode, open your project .πš‘πšŒπš πš˜πš›πš”πšœπš™πšŠπšŒπšŽ πšπš’πš•πšŽ.
  2. Create a new Swift file (e.g., πšπšŽπšŠπš•πš–πš†πš›πšŠπš™πš™πšŽπš›.πšœπš πš’πšπš) to manage your Realm configuration and interactions.
  3. signify the RealmSwift library at the top of the file:πš’πš–πš™πš˜πš›πš πšπšŽπšŠπš•πš–πš‚πš πš’πšπš
  4. 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:

  1. Create a new Swift file (e.g., π™ΏπšŽπš›πšœπš˜πš—.πšœπš πš’πšπš) to define your Realm object.
  2. signify the RealmSwift data library at the uppermost of the file:

π™ΏπšŽπš›πšœπš˜πš—.πšœπš πš’πšπš

  1. Declare a new class and inherit from the Object class provided by Realm:
    πšπš’πš—πšŠπš• πšŒπš•πšŠπšœπšœ π™ΏπšŽπš›πšœπš˜πš—: π™Ύπš‹πš“πšŽπšŒπš {
    @π™ΏπšŽπš›πšœπš’πšœπšπšŽπš(πš™πš›πš’πš–πšŠπš›πš’π™ΊπšŽπš’: πšπš›πšžπšŽ) πšŸπšŠπš› πš’πš: π™Ύπš‹πš“πšŽπšŒπšπ™Έπš // π™Ώπš›πš’πš–πšŠπš›πš’ πš”πšŽπš’
    @π™ΏπšŽπš›πšœπš’πšœπšπšŽπš πšŸπšŠπš› πš—πšŠπš–πšŽ: πš‚πšπš›πš’πš—πš = “”
    @π™ΏπšŽπš›πšœπš’πšœπšπšŽπš πšŸπšŠπš› 𝚊𝚐𝚎: π™Έπš—πš = 0
    }
  2. 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:

  1. 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:

πšπšžπš—πšŒ πšπšŽπšπšŒπš‘π™ΏπšŽπš›πšœπš˜πš—π™±πš’π™Έπš(πš’πš: π™Ύπš‹πš“πšŽπšŒπšπ™Έπš) -> π™ΏπšŽπš›πšœπš˜πš—? {
πš›πšŽπšπšžπš›πš— πšπšŽπšŠπš•πš–πš†πš›πšŠπš™πš™πšŽπš›.πšœπš‘πšŠπš›πšŽπš.πš›πšŽπšŠπš•πš–
.πš˜πš‹πš“πšŽπšŒπš(πš˜πšπšƒπš’πš™πšŽ: π™ΏπšŽπš›πšœπš˜πš—.πšœπšŽπš•πš, πšπš˜πš›π™Ώπš›πš’πš–πšŠπš›πš’π™ΊπšŽπš’: πš’πš)
}

  1. For fetching objects by name, use the below code:

πšπšžπš—πšŒ πšπšŽπšπšŒπš‘π™ΏπšŽπš›πšœπš˜πš—πšœπ™±πš’π™½πšŠπš–πšŽ(πš—πšŠπš–πšŽ: πš‚πšπš›πš’πš—πš) -> πšπšŽπšœπšžπš•πšπšœ<π™ΏπšŽπš›πšœπš˜πš—>? {
πš•πšŽπš πš™πš›πšŽπšπš’πšŒπšŠπšπšŽ = π™½πš‚π™Ώπš›πšŽπšπš’πšŒπšŠπšπšŽ(πšπš˜πš›πš–πšŠπš: “πš—πšŠπš–πšŽ == %@”, πš—πšŠπš–πšŽ
πš›πšŽπšπšžπš›πš— πšπšŽπšŠπš•πš–πš†πš›πšŠπš™πš™πšŽπš›.πšœπš‘πšŠπš›πšŽπš.πš›πšŽπšŠπš•πš–
.πš˜πš‹πš“πšŽπšŒπšπšœ(π™ΏπšŽπš›πšœπš˜πš—.πšœπšŽπš•πš).πšπš’πš•πšπšŽπš›(πš™πš›πšŽπšπš’πšŒπšŠπšπšŽ)
}

  1. 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:

  1. Relationships: You can create relationships between Realm objects using properties such as π™»πš’πš—πš”πš’πš—πšπ™Ύπš‹πš“πšŽπšŒπšπšœ or πšπšŽπšŠπš•πš–π™Ύπš™πšπš’πš˜πš—πšŠπš•. introduce to the Realm documentation for detailed examples.
  2. Queries: Realm provides a powerful query API to fetch objects based on specific criteria. For example:

πš•πšŽπš πš™πšŽπš›πšœπš˜πš—πšœ = πšπšŽπšŠπš•πš–πš†πš›πšŠπš™πš™πšŽπš›.πšœπš‘πšŠπš›πšŽπš.πš›πšŽπšŠπš•πš–.πš˜πš‹πš“πšŽπšŒπšπšœ(π™ΏπšŽπš›πšœπš˜πš—.πšœπšŽπš•πš).πšπš’πš•πšπšŽπš›(β€œπšŠπšπšŽ > 𝟷𝟾”)

  1. 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.

how can we help you?

Contact us at the Consulting WP office nearest to you or submit a business inquiry online.

Get technology solution for your business need