Realm: A mobile database solution that offers an alternative to SQLite and Core Data. It’s designed for speed and ease of use.
Browse / Realm Database Cheatsheet
Realm Database Cheatsheet
A concise guide to using Realm, covering schema definition, CRUD operations, queries, and relationships.
Core Concepts & Setup
Realm Fundamentals
|
|
|
Key Features:
|
|
Data Model: Realm uses a schema to define the structure of your data. Models are defined as classes. |
|
Installation (Swift): Add |
|
Importing Realm:
|
Configuration
|
Default Realm |
The default Realm is suitable for most basic use cases. It stores data in the app’s default location. |
|
Custom Realm Configuration |
Use |
|
In-Memory Realm |
Useful for testing. Data is not persisted to disk.
|
Error Handling
|
Realm throws exceptions for various errors. Wrap Realm operations in |
|
Common Errors:
|
|
Example:
|
Defining Realm Models
Basic Model Definition
|
Realm models are defined as classes that inherit from |
|
Properties must be declared with the |
|
Example:
|
Supported Data Types
|
|
Integer numbers. |
|
|
Floating-point numbers. |
|
|
Textual data. |
|
|
Boolean values (true/false). |
|
|
Date and time values. |
|
|
Binary data. |
Optional Properties
|
Properties can be declared as optional using |
|
Optional properties can store |
|
Example:
|
Ignored Properties
|
Properties marked with |
|
Useful for temporary or calculated values. |
|
Example:
|
CRUD Operations
Creating Objects
|
Create instances of your Realm model classes and add them to the Realm. |
|
Example:
|
Reading Objects
|
Use Realm queries to retrieve objects. |
|
Example:
|
Updating Objects
|
Update objects within a write transaction. |
|
Example:
|
Deleting Objects
|
Delete objects within a write transaction. |
|
Example:
|
Querying Realm Data
Basic Queries
|
Realm uses a query language similar to NSPredicate. |
|
Use |
|
Example:
|
Common Query Operators
|
|
Equals. |
|
|
Not equals. |
|
|
Greater than. |
|
|
Less than. |
|
|
Greater than or equal to. |
|
|
Less than or equal to. |
|
|
String starts with. |
|
|
String ends with. |
|
|
String contains. |
|
|
String matches a wildcard pattern. |
Compound Predicates
|
Combine predicates using |
|
Example:
|
Sorting Results
|
Use |
|
Example:
|