ios - 将对象存储到 RealmSwift 中的列表后的 EXC_BAD_ACCESS

标签 ios swift list swift3 realm

我正在将数据存储到 Realm 中,其中我在游戏对象中有一个目标列表:

目标.swift

class Goal : Object {
    dynamic var scoreTime = Date()
    dynamic var scoreBy:Player?
    dynamic var scoreForTeam:GameTeam?
    dynamic var homeScoreAfter:Int = 0
    dynamic var awayScoreAfter:Int = 0
    let passers = List<Player>()
}

游戏.swift

class Game : Object {
    dynamic var startDate:Date? = nil
    dynamic var endDate:Date? = nil
    dynamic var homeTeam:GameTeam?
    dynamic var awayTeam:GameTeam?
    let goals = List<Goal>()
}

我在Game的goals-property中添加了一个goal,但是goals-list似乎在保存之后就失效了。这是代码:

let toCreate = Game()

toCreate.homeTeam = GameTeam()
toCreate.awayTeam = GameTeam()

toCreate.startDate = Date()

let goal = Goal(scoreBy: allPlayers[0].player, scoreForTeam: toCreate.homeTeam!, passedBy: [], scoreTime: Date(), homeScoreAfter: 1, awayScoreAfter: 0)
toCreate.goals.append(goal)

let realm = try! Realm()

try! realm.write {
    realm.add(toCreate)
}

但是在 realm.write 调用之后我遇到了这个错误:

(lldb) po toCreate.goals

expression produced error: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x8).
The process has been returned to the state before expression evaluation.

而且我不能再使用目标列表了。它在 realm.write 完成之前打印正常。我在这里缺少什么微不足道的东西......?对象的其他部分运行良好。

编辑:所以不知何故问题出在属性 Goal.passers - 从对象中删除它可以解决问题。这是否与对象层次结构中对玩家的大量引用有关?请看,Game.homeTeam.playersGoal.scoreByGoal.passers 都引用了玩家对象。

最佳答案

需要回答我自己的问题。我的问题出在 Goal 对象的自定义初始化代码中,它看起来像这样:

    init(scoreBy:Player, scoreForTeam:GameTeam, passedBy:[Player]?, scoreTime:Date, homeScoreAfter:Int, awayScoreAfter:Int) {
        super.init()
        ... 
    }

这段代码不会产生这样一个正常工作的 Realm 对象。在没有自己的 init 方法的情况下创建 Goal 内联对象解决了这个问题。


编辑:虽然行为本身很奇怪 - 该构造函数应该工作,还是这是 Realm 中的错误?

关于ios - 将对象存储到 RealmSwift 中的列表后的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43966915/

相关文章:

ios - 无法使用 NSJSONSerialization 将数字转换为 JSON 值

ios - swift 将 CLLocation 转换为 CLLocationCoordinate2D

python - 使用列表位置中包含的项目进行计算(遗传算法适合度)

android - Flutter ListView 不滚动到最后添加的项目,而是滚动到最后一个之前的项目

objective-c - 我的 .xcdatamodel 中的空心箭头是什么?

swift - 无法获取 NSTextView 的前景色

Android 列表格式

python - 如何以python方式以任意顺序迭代两个列表

ios 如何在从 UITableView 中的 rss 提要获取数据时显示事件指示器?

ios - 如何从 presentModalViewController 函数创建一个更小的 UIView?