BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

New Realm Cocoa Data Types

Lee Maguire4 min read • Published Aug 09, 2021 • Updated Mar 06, 2023
Facebook Icontwitter iconlinkedin icon
Rate this article
In this blog post we will discover the new data types that Realm has to offer.
Over the past year we have worked hard to bring three new datatypes to the Realm SDK: MutableSet, Map, and AnyRealmValue.
Build better mobile apps with Atlas Device Sync: Atlas Device Sync is a fully-managed mobile backend-as-a-service. Leverage out-of-the-box infrastructure, data synchronization capabilities, built-in network handling, and much more to quickly launch enterprise-grade mobile apps. Get started now by build: Deploy Sample for Free!


MutableSet allows you to store a collection of unique values in an unordered fashion. This is different to List which allows you to store duplicates and persist the order of items.
MutableSet has some methods that many will find useful for data manipulation and storage:
  • Intersect
    • Gets the common items between two MutableSets.
  • Union
    • Combines elements from two MutableSets, removing any duplicates.
  • Subtract
    • Removes elements from one MutableSet that are present in another given MutableSet.
  • isSubset
    • Checks to see if the elements in a MutableSet are children of a given super MutableSet.
So why would you use a MutableSet over a List?
  • You require a distinct collection of elements.
  • You do not rely on the order of items.
  • You need to perform mathematical operations such as Intersect, Union, and Subtract.
  • You need to test for membership in other Set collections using isSubset or intersects.

Practical example

Using our Movie object, we want to store and sync certain properties that will never contain duplicates and we don't care about ordering. Let's take a look below:
Straight away you can see the use case, we never want to have duplicate elements in the countries, genres, languages, and writers collections, nor do we care about their stored order. MutableSet does support sorting so you do have the ability to rearrange the order at runtime, but you can't persist the order.
You query a MutableSet the same way you would with List:

Under the hood

MutableSet is based on the NSSet type found in Foundation. From the highest level we mirror the NSMutableSet / Set API on RLMSet / MutableSet.
When a property is unmanaged the underlying storage type is deferred to NSMutableSet.


Our new Map data type is a Key-Value store collection type. It is similar to Foundation's Dictionary and shares the same call semantics. You use a Map when you are unsure of a schema and need to store data in a structureless fashion. NOTE: You should not use Map over an Object where a schema is known.

Practical example

Map also supports aggregate functions so you can easily calculate data:
As well as filtering with NSPredicate:
You can observe a Map just like the other collection types:
Combine is also supported for observation:

Under the hood

Map is based on the NSDictionary type found in Foundation. From the highest level, we mirror the NSMutableDictionary / Dictionary API on RLMDictionary / Map.
When a property is unmanaged the underlying storage type is deferred to NSMutableDictionary.


Last but not least, a datatype we are very excited about, AnyRealmValue. No this is not another collection type but one that allows you to store various different types of data under one property. Think of it like Any or AnyObject in Swift or a union in C.
To better understand how to use AnyRealmValue, let's see some practical examples.
Let's say we have a Settings class which uses a Map for storing the user preferences, because the types of references we want to store are changing all the time, we are certain that this is schemaless for now:
Here we can store different variants of the value, so depending on the need of your application, you may find it useful to be able to switch between different types.

Under the hood

We don't use any Foundation types for storing AnyRealmValue. Instead the AnyRealmValue enum is converted to the ObjectiveC representation of the stored type. This is any type that conforms to RLMValue. You can see how that works here.


I hope you found this insightful and have some great ideas with what to do with these data types! All of these new data types are fully compatible with MongoDB Realm Sync too, and are available in Objective-C as well as Swift. We will follow up with another post and presentation on data modelling with Realm soon.
Links to documentation:

Facebook Icontwitter iconlinkedin icon
Rate this article

Document our Realm-Powered Swift Frameworks using DocC

May 13, 2022 | 6 min read

Creating a Multiplayer Drawing Game with Phaser and MongoDB

Apr 02, 2024 | 15 min read

Create a Custom Data Enabled API in MongoDB Atlas in 10 Minutes or Less

Sep 23, 2022 | 8 min read

Migrating Your iOS App's Synced Realm Schema in Production

Mar 06, 2023 | 9 min read
Table of Contents
  • MutableSet