1

I'm updating Realm an iOS Swift app from 5.5.1 to 10.12.0 using Cocoa Pods.

This update have a Breaking Change public typealias User = RLMUser which conflicts with my own public final class User: Object. Refactoring to another name makes my app to crashes as assert(object.realm != nil) when reading this user class.

Is it because I've renamed my class and the realm database is expecting the old name from it's database? Or should I do some sort of merge when app start?

Or should I make my own branch changing the 'typealias User = RLMUser` in the Pod?

Peter
  • 75
  • 1
  • 7
  • "Refactoring to another name makes my app to crashes" <--- sounds like you just need a [migration](https://stackoverflow.com/questions/47914361/best-way-to-rename-a-class-in-realm-swift). – Sweeper Aug 26 '21 at 11:02
  • I would recommend you to have a facade, a protocol, which you interact with instead of the concrete Realm object. In this way, your code would be very safe to refactor without the need to deal with the underlying Realm object. – Sean Goudarzi Aug 26 '21 at 12:37
  • There's a pretty good answer by @Sweeper but more information is probably needed. It appears this is a Sync'd app? If that's the case, a migration will not be available. Also, the overall file structure is different when using Sync/10.x vs 5.x. Can you clarify if this is Sync'd or not? Also, you could create a new user class MyUserClass and craft some code to copy the data from the old class to the new one, so it's a 'manual' migration. There are a view other options as well. – Jay Aug 26 '21 at 15:56
  • Yes, I'm going to figure out how to do the migration or first test the override suggestion by @Sweeper . I'm not using SyncUser so a migration or an override of class name should work. – Peter Aug 26 '21 at 19:18

1 Answers1

1

Is it because I've renamed my class and the realm database is expecting the old name from it's database?

Yes. You can either fix this by doing a migration as described here, or you can override _realmObjectName:

class RenamedUser: Object {
    ...

    override class func _realmObjectName() -> String? {
        "User"
    }
}

This way, to Realm, RenamedUser is still called User, but to Swift, it's called RenamedUser.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thanks Sweeper. Yes I think that your suggestions will work. I will approve your solution as soon as I have implemented it in the code :) – Peter Aug 26 '21 at 19:14