swift - Realm swift update List 写入每个运行时

标签 swift xcode realm

我的对象

   class RealmConversation: Object {

        @objc dynamic var id = ""
        var show = List<Show_id>()
        var show_ids: [String] {
            get {
                return show.map { $0.show_id }
            }
            set {
                show.removeAll()
                show.append(objectsIn: newValue.map {Show_id(value: [$0])})
            }
        }
        override class func ignoredProperties() -> [String] {
            return ["show_ids"]
        }
        override static func primaryKey() -> String? {
            return "id"
        }
    }

class Show_id:Object {
    @objc dynamic var show_id = ""
}

附加 View Controller

 newSpecimen.show_ids = ["id1", "id2", "id3"]
    realm.add(newSpecimen, update: true)   
    self.realmconversation = newSpecimen

结果是运行时总是增加 x2 show_ids 如何更新字符串列表项

运行时1
enter image description here
运行时2
enter image description here

最佳答案

If you don’t want to save a field in your model to its Realm, override Object.ignoredProperties(). Realm won’t interfere with the regular operation of these properties; they’ll be backed by ivars, and you can freely override their setters and getters. Realm documentation

为此,在添加新对象之前,您应该检查 show_ids 是否包含您要添加的 id。

关于swift - Realm swift update List 写入每个运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48088865/

相关文章:

Android Realm 迁移 : Adding new Realm list column

java - Realm 无效类型

ios - 你能打印一些东西并根据最后的 println 做一个函数吗?

swift - 带有 CGGradient 的 PaintCode StyleKit Swift 3.0 错误

xcode - 如何安装 dashcode(Apple 开发人员套件的一部分)?

xcode - 在 Xcode 安装的 git 中禁用 osxkeychain 凭证助手

swift - 如何在单击按钮时在 ImageView 中显示不同的图像

swift - Swift 中没有信号量的并发

ios - UIImage 在模拟器上工作,但在设备上 swift 崩溃

android - 使用 React Native Realm 通过多个 List 对象进行查询