English 中文(简体)
• 如何在现实快速发展与一对一的关系转变
原标题:How to migrate one-to-one relationship to one-to-many relationship in Realm Swift

Currently, I have two custom model classes. Between them previously I set up a one-to-one relationship. But now due to new requirements change, I need to convert the relationship into a one-to-many.

For example, this is how I previously structure my model classes

class Location: Object {
  @objc dynamic private var id: String = NSUUID().uuidString
  @objc dynamic var typeString: String = 

}

class Job :Object {
@objc dynamic var id: String = NSUUID().uuidString
let location: Location?
}

新结构

class Job :Object {
@objc dynamic var id: String = NSUUID().uuidString
let locations = RealmSwift.List<Location>()
}

因此,我基本上想在职业类别中节省多个地点物体。 这一新结构对我来说是不起作用的。 但现在我想将旧数据移入这一新机构。 我试图利用以下法典实现这一目标,但没有任何成功。

migration.deleteData(forType: Location.className)
migration.enumerate(ofType:Job.className) { oldObject, newObject in
    let location = migration.create(Location, value: oldObject!["location"]!)
    (newObject!["locations"] as! List<MigrationObject>).append(location)
}
问题回答

简言之,你有一对一的关系,现在希望把这种关系转变为一种关系(通过一份名单),把现有地点作为名单上的第一个对象。

A getcha is you can t have two objects with the same primary key so during the migration process, the old locations have to be removed and will be replaced with a new location with the same properties (and primary key) and then add it to the Job s locations List property.

Here s the code:

let config = Realm.Configuration( schemaVersion: vers, migrationBlock: { migration, oldSchemaVersion in
     if (oldSchemaVersion < vers) {
        migration.deleteData(forType: Location.className()) //delete all locations
        migration.enumerateObjects(ofType: Job.className()) { oldItem, newItem in
           let existingLoc = oldItem!["location"]
           let updatedLoc = migration.create(Location.className(), value: existingLoc)
           let locList = newItem!["locations"] as! List<MigrationObject>
           locList.append(updatedLoc)
        }
     }
 })

I m not sure if you still need the answer or maybe someone else might want the solution.

主要想法是上检索最后设计的定位物体,并将其贴在清单中,而不是使用MigrationObject

migration.enumerateObjects(ofType: Job.className()) { oldItem, newItem in
       let existingLoc = oldItem!["location"]
       let updatedLoc = migration.create(Location.className(), value: existingLoc)
       let savedLoc = updatedLoc.realm?.object(ofType: Location.self, forPrimaryKey: existingLoc.id)
       locList.append(savedLoc)
    }

页: 1 请指出: 这是我的解决方案,我没有找到任何有关文件。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签