V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
netabare
V2EX  ›  iOS

往 SwiftData 的 context 里面塞东西的时候该怎么正确处理 relationship 的关系?

  •  
  •   netabare · 140 天前 · 820 次点击
    这是一个创建于 140 天前的主题,其中的信息可能已经有所发展或是发生改变。

    报错的位置在 Model 里面字段属性的 codegen 部分,具体来说大概是这样的位置:

    {
        @storageRestrictions(accesses: _$backingData, initializes: _child)
            init(initialValue) {
                        _$backingData.setValue(forKey: \._child, to: initialValue)
                        _jobDescription = _SwiftDataNoType()
            }
        ...
    }
    

    报错内容是「 illegal attempt to establish a relationship between objects in different contexts 」。

    如果按照字面上的意思,也已经按照这里( https://stackoverflow.com/questions/77485748/json-to-swiftdata-decode-of-json-ends-in-error-at-observationregistrar )的说法在所有 Model 的 init 函数里加上了 context 那段代码了。

    之前也遇到类似的情况,不过不在 init 而是在 getValue 里面。报错EXEC_BREAKPOINT Code=1或者Illegal attempt to establish a relationship,查了 stackoverflow 或者 Developer 社区的说法是可以通过给每个 Model 提供无参构造器,给每个 relationship 字段提供默认值来解决问题。也就是例如

    let child = Child()
    modelContext.insert(child)
    
    let parent = Parent()
    modelContext.insert(parent)
    
    parent.child = child
    

    这样先存入 context 再把 relationship 接上来的办法来解决。

    然后在实现 JSON 导入导出的时候又遇到类似的问题。不过 JSONDecoder 是通过调用 Model 里面的构造器来创建对象的吧。也就是这个

    required init(from decoder: Decoder) throws {
       guard let context = ... else {
          ...
       }
       let container = try decoder.container(keyedBy: CodingKeys.self)
       
       self.child = try? container.decode(Child.self, forKey: .child)
       ...
    }
    

    换句话说就是得先把 Child 构造出来然后赋值给self.child字段,而这就会触发Illegal attempt to establish a relationship 'child' between objects in different contexts

    但是因为 JSON 的 Decode 过程,没法通过先构造一个无参数的 Parent 再把 child 加到 parent 上面的方式来绕过。

    这种情况下该怎么解决 relationship 的问题呢?

    尝试了加@Relationship或者不加或者加不同的注解,也试过在 relationship 两边的 class 分别加上对面的引用或者只加一边,但每次都会在同样的位置报错。

    另外也在模拟器里面发现有时候虽然报错但是程序退出重启之后会多出来那么一两个原 JSON 里面的数据,也就是说存在解析成功了的情况,所以感觉也很迷惑。

    1 条回复    2023-12-10 21:29:53 +08:00
    hogus
        1
    hogus  
       139 天前 via iPhone
    不需要 insert child
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   904 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 116ms · UTC 22:10 · PVG 06:10 · LAX 15:10 · JFK 18:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.