title: Couchbase Lite filtered pull replication
body: <p>I've got android and web apps. Android app uses Couchbase Lite, web app uses Couchbase. I'm using Couchbase Sync Gateway to enable data replication between those two databases.</p>
  
  <p>When user logs in to mobile app for the first time I'd like to pull replicate the document with data on this specific user (based on username). I read that filtered replication is supported by Couchbase Lite <a href="http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/replication/index.html" rel="nofollow">(1)</a> but I'm quite confused which method should I use.</p>
  
  <p>What I want to do is a bit similar to <a href="https://github.com/couchbase/couchbase-lite-android/issues/200" rel="nofollow">this</a>, so I guess my filter function would be like:</p>
  
  <pre><code>function (doc, req) { 
      if(doc.DocType &amp;&amp; doc.DocType == "User" &amp;&amp; doc.IsActive &amp;&amp; doc.Login == req.query.text) 
          { return true } 
      else 
          {return false} 
  }
  </code></pre>
  
  <p>but I've got no idea where should I create this function (in my web app code? I found only <code>DesignDocument.create()</code> but it inserts only View functions).</p>
  
  <p>I read that with SyncGateway I should use channels, but as far I understand  channels base on static property of a document and I need to use username as a dynamic parameter.</p>
  
  <p>How can I manage to pull replicate only this specific document?</p>
  
  <p>edit.</p>
  
  <p>So previously my SyncGateway config sync property was like:</p>
  
  <pre><code>"sync":`
              function (doc) {
              channel (doc.channels);
          }`
  </code></pre>
  
  <p>and it worked normally without filers. I turned it into:</p>
  
  <pre><code>"sync":`
              function (doc, oldDoc) {                 
  
                  if(doc.DocType=="User" &amp;&amp; doc.IsActive){
                      channel(doc.Login);
                  }
                  else {
                      channel (doc.channels);
                  }
          }`
  </code></pre>
  
  <p>and my Java method is:</p>
  
  <pre><code>public void startFirstLoginReplication(String login, String password) throws CouchbaseLiteException{
  
      (...)
      pull = DatabaseManager.getDatabaseInstance().createPullReplication(this.createSyncURL(false));
  
      List&lt;String&gt; channels = new ArrayList&lt;String&gt;();
      channels.add(login);
      pull.setChannels(channels);
  
      pull.setContinuous(false);
  
      pull.start();
  
      progressDialog = showLoadingSpinner();
  
      pull.addChangeListener(this);   
  }
  </code></pre>
  
  <p>With normal replication, when it was updated method <code>changed()</code> was called. With filter it is not, nothing is pulled and progressDialog is stuck.</p>
  
  <p>In SyncGateway console I get:</p>
  
  <pre><code>2016-06-27T23:45:46.081+02:00 HTTP:  #003: GET     /kris_mobile_db/_local/01ef0a1ec2301f9f9f3bb2fae9d352d26fd4a9a7
  2016-06-27T23:45:46.094+02:00 HTTP: #003:     --&gt; 404 missing  (14.5 ms)
  2016-06-27T23:45:46.300+02:00 HTTP:  #004: POST /kris_mobile_db/_changes
  2016-06-27T23:45:46.301+02:00 Changes: MultiChangesFeed({Mo}, {Since:0 Limit:0 Conflicts:true IncludeDocs:false Wait:false Continuous:false  Terminator:0xc08245cba0 HeartbeatMs:300000 TimeoutMs:300000}) ...
  2016-06-27T23:45:46.301+02:00 Changes+: MultiChangesFeed: channels expand to channels.TimedSet{"Mo":0x1} ...
  2016-06-27T23:45:46.301+02:00 Changes+: MultiChangesFeed sending &amp;{Seq:1 ID:_user/GUEST Deleted:false Removed:{} Doc:map[] Changes:[] Err:&lt;nil&gt; branched:false}
  2016-06-27T23:45:46.302+02:00 Changes: MultiChangesFeed done
  </code></pre>
  
  <p>What am I missing?</p>