How to organize realm data


#1

I’m investigating using realm for a project where we have 10K-20K devices split among 1K-2K teams and need to sync data to each device. The content, which can range from 100K-1M+ records, is about 80%-90% common to all teams, with 10%-20% specific to each team.

I’ve started reading up on realm but haven’t yet figured out the best approach to architecting this in such a way that each device gets just the data it needs for the team to which it belongs. Any tips on where to look first? Should I be looking at query-based sync? Object-level permissions? Separate realm per team (with massive data duplication)?


#2

@davidj The team’s users have their data? Or the team’s data are shared with all the team’s users?


#3

There are several options but it’s a pretty broad question and a lot of the answer will depend on how you use your data.

The first thing is to explore are Realm Collections and in particular, Results; results are the results of a query (filter). The results are live and objects that meet or fall outside of the filter criteria are automatically updated within those results.

Suppose you have two teams of wine tasters. One team tastes Red wine and the other team tastes white wine. Obviously the first team will want to know only about reds, the other whites.

One of the team one members clicks a ‘Show Me Wines To Taste’ button, for the available wines. Once they’ve selected and tasted the wine, written a review and completed that wine (which sets a ‘tasted’ property to true), they click the ‘Show Me Wines To Taste’ button again. This time the wine they just tasted will not be in the list. The reason? because there was a Query e.g.filter on the data.

self.results = realm.objects(Wines.self).filter("color = 'Red' and tasted = false")

In this case we are storing the results var as a class variable so the query does not need to be re-run each time. The results object automatically contains the wines that match the criteria of the query.

Suppose though, we want to automatically have our list of available wines updated when a tasting has completed with a UI update as well. i.e. the wine list should refresh automatically in the UI without having to click the ‘Show Me Wines To Taste’.

Then explore Notifications. Notifications involve Observers that observe query results and notify the app (via an event) when a change occurs; objects Added, Modified or Removed. That event contains info about what changed so you can then then prompt your app to update the UI with the available wine list.

Observers can be as granular as needed; anywhere from observing all wines, to each team only observing red or white wines, to observing only wines from Paso Robles, to observing red wines from single vineyard down to a specific red wine.

This is all at the 10,000 foot level and the links are the Swift docs but should set you off in the right direction.


#4

Each member of a given team gets the same data. Each team has 80%-90% data in common with all the other teams, the remaining 10%-20% specific to that team or a subset of the teams.


#5

Thanks for the pointers. I’ll take a look at Realm Collections and Notifications.