Interested in speaking at MongoDB World 2022? Click here to become a speaker.
HomeLearnArticleNew Realm Cocoa Data Types

New Realm Cocoa Data Types

Updated: Aug 09, 2021 |

Published: Aug 09, 2021

  • Mobile
  • Realm
  • Swift
  • ...

By Lee Maguire

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.

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

1class Movie: Object, ObjectKeyIdentifiable {
2 @Persisted(primaryKey: true) var _id: ObjectId
3 @Persisted var _partitionKey: String
4 // we will want to keep the order of the cast, so we will use a `List`
5 @Persisted var cast: List<String>
6 @Persisted var countries: MutableSet<String>
7 @Persisted var genres: MutableSet<String>
8 @Persisted var languages: MutableSet<String>
9 @Persisted var writers: MutableSet<String>
10}

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:

1let danishMovies = realm.objects(Movie.self).filter("'Danish' IN languages")

#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

1@Persisted phoneNumbers: Map<String, String>
2
3phoneNumbers["Charlie"] = "+353 86 123456789"
4let charliesNumber = phoneNumbers["Charlie"] // "+353 86 123456789"

Map also supports aggregate functions so you can easily calculate data:

1@Persisted testScores: Map<String, Int>
2
3testScores["Julio"] = 95
4testScores["Maria"] = 95
5testScores["John"] = 70
6
7let averageScore = testScores.avg()

As well as filtering with NSPredicate:

1@Persisted dogMap: Map<String, Dog>
2
3let spaniels = dogMap.filter(NSPredicate("breed = 'Spaniel'")) // Returns `Results<Dog>`

You can observe a Map just like the other collection types:

1let token = map.observe(on: queue) { change in
2 switch change {
3 case .initial(let map):
4 ...
5 case let .update(map, deletions: deletions, insertions: insertions, modifications: modifications):
6 // `deletions`, `insertions` and `modifications` contain the modified keys in the Map
7 ...
8 case .error(let error):
9 ...
10 }
11}

Combine is also supported for observation:

1cancellable = map.changesetPublisher
2 .sink { change in
3 ...
4 }

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

1class Settings: Object {
2 @Persisted(primaryKey: true) var _id: ObjectId
3 @Persisted var _partitionKey: String?
4 @Persisted var misc: Map<String, AnyRealmValue>
5}

Usage:

1misc["lastScreen"] = .string("home")
2misc["lastOpened"] = .date(.now)
3
4// To unwrap the values
5
6if case let .string(lastScreen) = misc["lastScreen"] {
7 print(lastScreen) // "home"
8}

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.

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

Rate this article
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.