Realm
MongoDB Developer Center
chevron-right
Developer Topics
chevron-right
Products
chevron-right
Realm
chevron-right

New Realm Cocoa Data Types

Lee MaguirePublished Aug 09, 2021 • Updated May 13, 2022
Realm
facebook icontwitter iconlinkedin icon
random alt
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
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.

MutableSet

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.

Map

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.

AnyRealmValue

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:
Usage:
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

Conclusion

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:

Copy Link
facebook icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial
Realm Data and Partitioning Strategy Behind the WildAid O-FISH Mobile Apps

May 12, 2022
News & Announcements
Realm Cocoa 5.0 - Multithreading Support with Integration for SwiftUI & Combine

May 16, 2022
News & Announcements
How We Migrated Realm JavaScript From NAN to N-API

May 13, 2022
News & Announcements
New Realm Data Types: Dictionaries/Maps, Sets, Mixed, and UUIDs

May 09, 2022
Table of Contents
  • MutableSet