swiftrealmrealm-database

"Table name too long" crash with realm-cocoa (Realm DB)


I'm getting a "Table name too long" error after moving some code into a framework. After going through the stack trace, breaking to get the table names on schema creation, then manually trying to create the longer table names. I've determined the name that's problematic. The table name that's problematic is a linking table, is there a way in Realm to force the name of that table using className() or something else for a linking table?

Screenshot of the Realm error being thrown. enter image description here


Solution

  • After further investigation it turns out the way to resolve this was to create a subclass of the type in my app. Due to the how my code was structured, Realm was creating the table name using the class name, package name and the generic type handed in. This made the name way too long. When you subclass the type with it's explicit generic type in the actual app, Realm no longer needs to worry about package names or the generic name. Below should help illustrate the problem and solution.

    class PackageA.One<I>: RealmSwift.Object {
       var List<I> = List<I>()
    }
    
    class App.Two: RealmSwift.Object {
    
    }
    
    let realmObjectsToRegister = [Package.One<App.Two>.self]
    

    The above lead to Realm creating a Table name of "TtGC11PackageA9OneC9App18Two" except the real names in my app made this over 57 characters (the max table name length).

    By doing the following, i shortened the name and fixed the problem

    class PackageA.One<I>: RealmSwift.Object {
       var List<I> = List<I>()
    }
    
    class App.Two: RealmSwift.Object {
    
    }
    
    class App.AppOne: PackageA.One<App.Two> {}
    
    let realmObjectsToRegister = [App.AppOne.self]
    

    The solution then lead to realm naming the table "AppOne" instead and fixed the long name issue.