In a recent post, I demonstrated how to migrate your Realm data when you upgraded your iOS app with a new schema. But, that only handled the data in your local, standalone Realm database. What if you’re using MongoDB Realm Sync to replicate your local Realm data with other instances of your mobile app and with MongoDB Atlas?
I start with the original RChat app. I then extend the iOS app and backend Realm schema to add a new feature that allows chat messages to be tagged as high priority. The next (and perhaps surprisingly more complicated from a Realm perspective) upgrade is to make the author attribute of the existing ChatMessage object non-optional.
to the ContentView(), but that didn’t seem to help either.
I didn’t make any destructive changes so I didn’t go through the more complicated tail end of your post, but I’m hoping to see what I may be doing wrong that is requiring a migration here.
Maybe there is a nuance that might be helpful to add to the post for future readers as well.
When I first worked on this post, I also needed to provide a new schema version. If I recall correctly, I needed to move to realm-cocoa 10.13.0. What version are you using?
If you are on 10.13+ and still need to provide a schema version, I believe that you need to add it to the environment for every view that opens a synced realm (e.g., via @ObservedResults rather than just once for ContnentView.
I was below 10.13 so I updated to current (10.15.1).
The app loads and runs fine, but when I try to write to a Realm, it throws that error. My assumption is that I will need to provide a new schema version.
Can you update your post to indicate why you may or may not need to provide a schema version bump on the client?
Also, I’m using a user realm and a hunt realm, so all over the place I’m passing in different environments depending on what is needed in each view. 68 places in total.
For the syntax, I tried to add schema version like this:
It would be good to understand why you’re being forced to provide a schema version for an additive change. If you’re able to provide a sample before and after project that has this behaviour, then I can try to reproduce and see whether there’s something that needs to be fixed.
@Andrew_Morgan – If it helps, I’ve noticed that the app is also crashing when I dismiss the sheet that has ObservedResults that include one of the models that have a new prop. So dismissing the ObservedResults is causing the crash that says that a migration is needed.
Loading the results is fine. Dismissing them causes the crash.
to all of the ObservedResults for the two types that have added fields.
I’m now searching through the docs and StackOverflow to see what I’m doing wrong, but I can’t figure it out. Everything just says that additive changes will work and the term ‘migration’ is so over crowded with the cloud>mongo shift that I don’t think I’ll be able to find it.
Are you able to create a sharable example of where this is happening? If I can reproduce an issue and cant find a solution, then I can work it with engineering.
I threw this together this morning. Original schema didn’t have a state value in the Address object. Added it and it throws the error when dismissing the details sheet.
I think that I’ve found the issue with the mini-app you shared. When a view invokes a SwiftUI Sheet view, that view doesn’t inherit the environment (vs. when you embed a view within a view).
The fix is to add the Realm config to the Sheet’s environment.
Good to hear that it works! The key is that Sheets don’t inherit the environment from the view that presents them (unlike embedding a view within a view). SwiftUI is great at hiding complexity, but it can sometimes mal mask what’s really going on.