title: Realm append data to a type List<t>
body: <p>I'm trying to go through data and save it in my model, however whatever i do i keep getting the following error: <code>Can't mutate a persisted array outside of a write transaction.</code>. What am i doing wrong? i'm appending each match to the league however it does not seem to work?</p>
  
  <p><strong>realm model</strong></p>
  
  <pre><code>class League: Object {
      dynamic var id: Int = 0
      dynamic var name: String? = ""
      var matches = List&lt;Match&gt;()
  
      override class func primaryKey() -&gt; String {
          return "id"
      }
  }
  
  class Match: Object {
      dynamic var matchId: Int = 0
      dynamic var date: NSDate?
      dynamic var homeName: String? = ""
      dynamic var awayName: String? = ""
      dynamic var awayScore: Int = 0
      dynamic var homeScore: Int = 0
      dynamic var leagueName: String? = ""
      dynamic var homeLogo: NSData?
      dynamic var awayLogo: NSData?
  }
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>    for (_, item) in result {
          if let leagueId = item["league"].int,
              let leagueName = item["name"].string,
              let allMatches = item["matches"].array {
                  let league = League()
                  league.name = leagueName
                  league.id = leagueId
  
                  for match in allMatches {
                      if let matchId = match["matchId"].int,
                          let tournament = match["tournament"].string,
                          let homeTeam = match["homeName"].string,
                          let awayTeam = match["awayName"].string,
                          let homeScore = match["homeScore"].int,
                          let awayScore = match["awayScore"].int,
                          let homeLogo = match["homeLogo"].string,
                          let awayLogo = match["awayLogo"].string,
                          let date = match["date"].string {
                              if let awayLogoUrl = NSURL(string: awayLogo),
                                  let homeLogoUrl = NSURL(string: homeLogo) {
                                      if let awayLogoData = NSData(contentsOfURL: awayLogoUrl),
                                          let homeLogoData = NSData(contentsOfURL: homeLogoUrl) {
                                              let matchObject = Match()
                                              matchObject.matchId = matchId
                                              matchObject.leagueName = tournament
                                              matchObject.homeName = homeTeam
                                              matchObject.awayName = awayTeam
                                              matchObject.homeScore = homeScore
                                              matchObject.awayScore = awayScore
                                              matchObject.homeLogo = homeLogoData
                                              matchObject.awayLogo = awayLogoData
  
                                              let formatter = NSDateFormatter()
                                              formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
                                              formatter.timeZone = NSTimeZone(abbreviation: "CET")
                                              matchObject.date = formatter.dateFromString(date)!
  
                                              league.matches.append(matchObject)
                                      }
                              }
                      }
  
                      print(league)
  
                      try! realm.write {
                          realm.add(league, update: true)
                      }
                  }
          }
      }
  }
  </code></pre>