Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-522

Access list property in Realm migration

      Goals

      I've added a primary key to an object that didn't have one before, which is based on some of the object's property, one of which has a List<String> data type. However, I am not able to correctly access the list property, hence I can't write the migration for the new primary key.

      Expected Results

      Being able to parse the elements in the List property.

      Actual Results

      Not being able to parse the list.

      Code Sample

      My database object looks as follows:

      @objcMembers
      public class TestObject: Object {
          dynamic private(set) var id: String = ""
      
          let _foobar: List<String> = .init()
      
          private(set) public var foobar: [SomeEnum] {
              set {
                  _foobar.removeAll()
                  _foobar.append(objectsIn: newValue.map { $0.rawValue })
              }
      
              get {
                  return _foobar.compactMap { SomeEnum(rawValue: $0) }
              }
          }
      
          public override class func primaryKey() -> String? {
              return "id"
          }
      
          convenience init(foo: [SomeEnum]) {
              self.init()
      
              let foobar = foo.reduce("", { $0 + "-" + $1.rawValue })
      
              self.id = foobar
              self.foobar = foo
          }
      }
      

      In my migration block I'm trying to access _foobar, but can't seem to get it right:

      static func migratePrimaryKey(_ migration: RealmSwift.Migration) {
          migration.enumerateObjects(ofType: TestObject.className()) { old, _ in
              let field: String = "_foobar"
      
              let test1 = old?[field] as? List<String>
              let test2 = old?[field] as? [String]
              let test3 = old?[field] as? List<DynamicObject>
              let test4 = old?[field]
          }
      }
      

      Both test and test2 are nil. test3 throws some exceptions:

      Could not cast value of type 'NSTaggedPointerString' (0x7fff87a8a270) to 'RealmSwift.DynamicObject' (0x10f065fe8).
      

      The output of test4 shows the following:

      (lldb) po test4
      ▿ Optional<Any>
        - some : List<string> <0x600001fb5f80> (
          [0] someContent
      )
      

      There seems to be some technical restriction which prevents me from accessing the actual list properties of my migration object?

      Version of Realm and Tooling

      Realm framework version:

      Realm Object Server version: 3.19.0

      Xcode version: 11.2.

      iOS/OSX version: 13.2

      Dependency manager + version: Carthage 0.34.0

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: