Realm
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

New Realm Data Types: Dictionaries/Maps, Sets, Mixed, and UUIDs

Ian WardPublished Jan 13, 2022 • Updated May 09, 2022
Realm
Copy Link
facebook icontwitter iconlinkedin icon
random alt
Rate this announcement
star-empty
star-empty
star-empty
star-empty
star-empty

TL;DR

Starting with Realm Javascript 10.5, Realm Cocoa 10.8, Realm .NET 10.2, and Realm Java 10.6, developers will be able persist and query new language specific data types in the Realm Database. These include Dictionaries/Maps, Sets, a Mixed type, and UUIDs.

Introduction

We're excited to announce that the Realm SDK team has shipped four new data types for the Realm Mobile Database. This work – prioritized in response to community requests – continues to make using the Realm SDKs an intuitive, idiomatic experience for developers. It eliminates even more boilerplate code from your codebase, and brings the data layer closer to your app's source code.
These new types make it simple to model flexible data in Realm, and easier to work across Realm and MongoDB Atlas. Mobile developers who are building with Realm and MongoDB Realm Sync can leverage the flexibility of MongoDB's data structure in their offline-first mobile applications.
Read on to learn more about each of the four new data types we've released, and see examples of when and how to use them in your data modeling:

Dictionaries/Maps

Dictionaries/maps allow developers to store data in arbitrary key-value pairs. They're used when a developer wants to add flexibility to data models that may evolve over time, or handle unstructured data from a remote endpoint. They also enable a mobile developer to store unique key-value pairs without needing to check the data for uniqueness before insertion.
Both Dictionaries and Maps can be useful when working with REST APIs, where extra data may be returned that's not defined in a mobile app's schema. Mobile developers who need to future-proof their schema against rapidly changing feature requirements and future product iterations will also find it useful to work with these new data types in Realm.
Consider a gaming app that has multiple games within it, and a single Player class. The developer building the app knows that future releases will need to enable new functionality, like a view of player statistics and a leaderboard. But the Player can serve in different roles for each available game. This makes defining a strict structure for player statistics difficult.
With Dictionary/Map data types, the developer can place a gameplayStats field on the Player class as a dictionary. Using this dictionary, it's simple to display a screen that shows the player's most common roles, the games they've competed in, and any relevant statistics that the developer wants to include on the leaderboard. After the leaderboard has been released and iterated on, the developer can look to migrate their Dictionary to a more formally structured class as part of a formal feature.
Kotlin
Swift
JavaScript
.NET

Mixed

Realm's Mixed type allows any Realm primitive type to be stored in the database, helping developers when strict type-safety isn't appropriate. Developers may find this useful when dealing with data they don't have total control over – like receiving data and values from a third-party API. Mixed data types are also useful when dealing with legacy states that were stored with the incorrect types. Converting the type could break other APIs and create considerable work. With Mixed types, developers can avoid this difficulty and save hours of time.
We believe Mixed data types will be especially valuable for users who want to sync data between Realm and MongoDB Atlas. MongoDB's document-based data model allows a single field to support many types across documents. For users syncing data between Realm and Atlas, the new Mixed type allows developers to persist data of any valid Realm primitive type, or any Realm Object class reference. Developers don't risk crashing their app because a field value violated type-safety rules in Realm.
Kotlin
Swift
JavaScript
.NET

Sets

Sets allow developers to store an unordered array of unique values. This new data type in Realm opens up powerful querying and mutation capabilities with only a few lines of code.
With sets, you can compare data and quickly find matches. Sets in Realm have built-in methods for filtering and writing to a set that are unique to the type. Unique methods on the Set type include, isSubset(), contains(), intersects(), formIntersection, and formUnion(). Aggregation functions like min(), max(), avg(), and sum() can be used to find averages, sums, and similar.
Sets in Realm have the potential to eliminate hundreds of lines of gluecode. Consider an app that suggests expert speakers from different areas of study, who can address a variety of specific topics. The developer creates two classes for this use case: Expert and Topic. Each of these classes has a Set field of strings which defines the disciplines the user is an expert in, and the fields that the topic covers.
Sets will make the predicted queries easy for the developer to implement. An app user who is planning a Speaker Panel could see all experts who have knowledge of both "Autonomous Vehicles" and "City Planning." The application could also run a query that looks for experts in one or more of these disciples by using the built-in intersect method, and the user can use results to assemble a speaker panel.
Developers who are using MongoDB Realm Sync to keep data up-to-date between Realm and MongoDB Atlas are able to keep the semantics of a Set in place even when synchronizing data.
You can depend on the enforced uniqueness among the values of a Set. There's no need to check the array for a value match before performing an insertion, which is a common implementation pattern that any user of SQLite will be familiar with. The operations performed on Realm Set data types will be synced and translated to documents using the $addToSet group of operations on MongoDB, preserving uniqueness in arrays.
Kotlin
Swift
JavaScript
.NET

UUIDs

The Realm SDKs also now support the ability to generate and persist Universally Unique Identifiers (UUIDs) natively. UUIDs are ubiquitous in app development as the most common type used for primary keys. As a 128-bit value, they have become the default for distributed storage of data in mobile to cloud application architectures - making collisions unheard of.
Previously, Realm developers would generate a UUID and then cast it as a string to store in Realm. But we saw an opportunity to eliminate repetitive code, and with the release of UUID data types, Realm comes one step closer to boilerplate-free code.
Like with the other new data types, the release of UUIDs also brings Realm's data types to parity with MongoDB. Now mobile application developers will be able to set UUIDs on both ends of their distributed datastore, and can rely on Realm Sync to perform the replication.
Kotlin
Swift
JavaScript
.NET

Conclusion

From the beginning, Realm's engineering team has believed that the best line of code is the one a developer doesn't need to write. With the release of these unique types for mobile developers, we're eliminating the workarounds – the boilerplate code and negative impact on CPU and memory – that are commonly required with certain data structures. And we're doing it in a way that's idiomatic to the platform you're building on.
By making it simple to query, store, and sync your data, all in the format you need, we hope we've made it easier for you to focus on building your next great app.
Stay tuned by following @realm on Twitter.
Want to Ask a Question? Visit our Forums.
Want to be notified about upcoming Realm events, like talks on SwiftUI Best Practices or our new Kotlin Multiplatform SDK? Visit our Global Community Page.

Copy Link
facebook icontwitter iconlinkedin icon
Rate this announcement
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial
Migrating Your iOS App's Realm Schema in Production

Sep 01, 2022
Tutorial
Developing a Side-Scrolling Platformer Game with Unity and MongoDB Realm

Sep 23, 2022
Tutorial
Introduction to the Realm SDK for Android

Sep 23, 2022
Article
Making SwiftUI Previews Work For You

May 13, 2022
Table of Contents
  • TL;DR