Meteoric
Meteoric

Reputation: 146

How to use query in couchbase lite

I'm working on couchbase lite wpf project. I want to get appointments by date. So I created a view with below code.

appointments.AddRange(App.StorageRepository.Query<AppointmentModel>(map, limit, offset, TodayWhere));



bool TodayWhere(IDictionary<string, object> accessDoc, IDictionary<string, object> parameters)
            {
                var tableName = parameters["table"].ToString();
                if (!accessDoc["_id"].ToString().Contains(tableName))
                {
                    return false;
                }
                var startDay = Convert.ToDateTime(accessDoc["startDate"].ToString());
                var today = Convert.ToDateTime(parameters["today"].ToString());
                if (startDay.Date != today.Date)
                {
                    return false;
                }
                return true;
            }

public List<T> Query<T>(IDictionary<string, object> map, int limit, int offset, Func<IDictionary<string, object>, IDictionary<string, object>, bool> monitoring)
        {
            List<T> list = new List<T>();
            var viewName = map["view"].ToString();
            var view = Manager.SharedInstance.GetDatabase(DatabaseName).GetView(viewName);
            view.SetMap((doc, emit) =>
                {
                    if (!monitoring(doc, map))
                    {
                        return;
                    }
                    emit(doc, null);
                }, "1.0");

            var query = view.CreateQuery();
            query.Limit = limit;
            query.Descending = true;
            var queryResult =
                query
                .Run()
                .ToList();
            foreach (var item in queryResult)
            {
                var doc = GetObject<T>(item.Document.UserProperties);
                list.Add(doc);
            }
            return list;
        }

So this code is working, but there is a problem. For example. I created the view in 2016.05.06. The view returns appointments of the day correctly. But It returns same data in today too. I'm not sure what is wrong. I'm confused about View and query. I can't use the view like SQL view table?

Upvotes: 0

Views: 353

Answers (1)

Hod
Hod

Reputation: 2276

Views build a static index that is persistent. The map function for a View must be a pure function, meaning it can't depend on anything but the document passed in. If your map function relies on anything (like the current date), you'll get inconsistent results. It looks like this may be what's happening in your case.

Rules for Couchbase Lite View map functions:

  • It must be a "pure" function: That means any time it's called with the same input, it must produce exactly the same output. In other words, it can't use any external state, just its input JSON.
  • It can't have side effects: It shouldn't change any external state, because it's unpredictable when it's called or how often it's called or in what order documents are passed to it.
  • It must be thread-safe: It may be called on a background thread belonging to the indexer, or even in parallel on several threads at once.

See the documentation on Couchbase Lite Views.

Upvotes: 1

Related Questions