Reputation: 509
I am storing data into Realm, where I have a List of goals inside a game object:
Goal.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>()
}
Game.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>()
}
I am adding a goal into Game's goals-property, but the goals-list seems to be invalidated after the saving happens. Here is the code:
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)
}
However after realm.write
call I am facing this error:
(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.
And I cannot consume the list of goals any more. It prints fine before realm.write is completed. Is there something trivial I am missing here...? The other parts of the objects play out just fine.
EDIT: so somehow the problem is the property Goal.passers
- removing it from the object fixes the issue. Can this be related into having quite many references to player in the object hierarchy? See, Game.homeTeam.players
, Goal.scoreBy
and Goal.passers
have the references to player objects.
Upvotes: 0
Views: 616
Reputation: 509
Gonna need to answer my own question. My issue was in the custom init code of the Goal
-object, as it looked like this:
init(scoreBy:Player, scoreForTeam:GameTeam, passedBy:[Player]?, scoreTime:Date, homeScoreAfter:Int, awayScoreAfter:Int) {
super.init()
...
}
This code does not produce such an realm object that works properly. Creating the Goal
-object inline without own init-method solved the issue.
EDIT: The behaviour itself is weird though - should that constructor work, or is this a bug in Realm?
Upvotes: 1