title: Realm List KVO observation
body: <p>The Realm Swift documentation states that most of the properties you use on a model class can be observed using KVO. Using ReactiveCocoa, for every property I have on a model class, I create a comparable <code>rac_</code> prefixed property that sends value changes which I can then use to bind to views in an MVVM style architecture.</p>
  
  <p>An example model class could look like:</p>
  
  <pre><code> class Post: Object {
       dynamic var text = ""
       private(set) lazy var rac_text: AnyProperty&lt;String&gt; = { [unowned self] in
           return AnyProperty(initialValue: self.name, signal: self.rac_valuesForKeyPath("text", observer: self).toSignal().takeUntil(self.willDeallocSignal())
       }()
   }
  </code></pre>
  
  <p>This is incredibly handy becuase 1) it's not mutable from the outside (<code>AnyProperty</code> vs <code>MutableProperty</code> and 2) only lives as long as the model does with <code>.takeUntil(self.willDeallocSignal())</code>. (I also wanted to ask, is the <code>[unowned self]</code> bit necessary here? I'm not sure if <code>self</code> gets captured or not, always been bad at that).</p>
  
  <p>The problem comes in with <code>List</code> properties. Lists can't be marked dynamic which makes sense, their type can't be represented in objective-c. And key value observing works just fine, with one major caveat.</p>
  
  <p>Take the same class with a relationship list property:</p>
  
  <pre><code>class Post: Object {
      let users: List&lt;User&gt; = List&lt;User&gt;()
  }
  </code></pre>
  
  <p>The corresponding reactive observation property should look something along the lines of:</p>
  
  <pre><code>private(set) lazy var rac_users: AnyProperty&lt;List&lt;User&gt;&gt; = {
      return AnyProperty(initialValue: self.users, signal: self.rac_valuesForKeyPath("users", observer: self).toSignal().takeUntil(self.willDeallocSignal()))
  }()
  </code></pre>
  
  <p>However on observation, the signal doesn't emit <code>List</code> objects, it emits <code>RLMArray</code> objects. I've had to jerryrig a signal producer that looks like:</p>
  
  <pre><code>private(set) lazy var rac_posts: AnyProperty&lt;List&lt;Post&gt;&gt; = { [unowned self] in
      return AnyProperty&lt;List&lt;Post&gt;&gt;(initialValue: self.posts, producer: self.rac_valuesForKeyPath("posts", observer: self)
          .toSignalProducer()
          .assumeNoErrors()
          .map { $0 as! RLMArray }
          .map { array in
              var list = List&lt;Post&gt;()
              for i in 0..&lt;array.count {
                  if let element =  array[i] as? Post {
                      list.append(element)
                  }
              }
              return list
      })
  }()
  </code></pre>
  
  <p>Of course, the <code>if let</code> statement always fails because <code>RLMObject</code> can't be cast to <code>Post</code>. So I either need a) a way to convert <code>RLMObject</code>s to <code>Object</code>s or b) a way to kvo on a list property that emits a list. I tested it using traditional KVO and got the same results.</p>