Creating Complex Views

43 views
Skip to first unread message

kde...@gmail.com

unread,
May 21, 2015, 2:03:49 PM5/21/15
to mobile-c...@googlegroups.com
I have a situation where i am trying to use "psuedo" joins where the doc types are ticket and schedule where I want to emit both types of documents.
The schedule document has eventid, year, minute, second and the ticket document has the schedule id.

In the if statement for a schedule doc, I will emit([doc[eventid], doc[year], doc[month], doc[day], doc[scheduleid], name)
In the if statement for a ticket doc, I want to lookup the eventid, year, month, day from the scheduleid in the ticket document. then emit([schedule.event, schedule.year, schedule.month, schedule.day, doc["schedule"], NULL).

I will reduce to get ticket count per event.  My question is to handle how to look up a schedule document from within mapblock.  I have got "thread exception" error in my first go at this, but before I continue to develop solution I wanted to make sure this is the best solution.  Obviously, I can put event, year, month, day in ticket documents but this is redundant information and may lead to errors if I have to change dates of schedule, but I wanted to know if it was a good practice to look up information on another document inside the mapblock on background thread. I realize that the examples are not written in perfect objective c syntax.

I hope I am making myself clear. Please respond.

Jens Alfke

unread,
May 21, 2015, 2:26:30 PM5/21/15
to mobile-c...@googlegroups.com

On May 21, 2015, at 10:28 AM, kde...@gmail.com wrote:

My question is to handle how to look up a schedule document from within mapblock.

You can’t, or rather, it’s illegal. In map/reduce, the map function has to be a “pure” function with no dependencies on any state other than its inputs, i.e. the document properties. Otherwise the index can become invalid.

In your case, if that schedule document changes in a way that would change the data emitted for the ticket document, the ticket document will not be re-indexed (only docs that change are re-indexed) so the index data will be out of date.

—Jens

kde...@gmail.com

unread,
May 21, 2015, 3:57:32 PM5/21/15
to mobile-c...@googlegroups.com
Jens,

Thanks for the quick response.

Can you offer an alternative to count tickets associated with schedules, and order them by date? When ticket does not have date information? Should I store the date information into ticket documents?

Jens Alfke

unread,
May 21, 2015, 4:28:59 PM5/21/15
to mobile-c...@googlegroups.com

On May 21, 2015, at 12:57 PM, kde...@gmail.com wrote:

Can you offer an alternative to count tickets associated with schedules, and order them by date? When ticket does not have date information? Should I store the date information into ticket documents?

Create a view that indexes the tickets with scheduleID as the primary key, then group at level=1 to get the count of tickets for each schedule.
Then create a view that indexes schedules with date as the key.
You’ll need to combine the two together to get ticket count by date. For example, query the first view and create a dictionary that maps schedule ID to count, then query the second view and for each schedule look up the count using the dictionary.

—Jens

kde...@gmail.com

unread,
May 21, 2015, 5:03:09 PM5/21/15
to mobile-c...@googlegroups.com
Thanks. I did not think using two views was an option because I am using CBLTablesource, using one view as the datasource and the other to create a dictionary makes sense.

Jens Alfke

unread,
May 21, 2015, 5:13:17 PM5/21/15
to mobile-c...@googlegroups.com

On May 21, 2015, at 2:03 PM, kde...@gmail.com wrote:

Thanks. I did not think using two views was an option because I am using CBLTablesource, using one view as the datasource and the other to create a dictionary makes sense.

That class is just a utility to help with the most common way of binding a table to a view. It’s not complex, and we encourage developers to copy and extend the source code if they need to go beyond what it can do.

—Jens

kde...@gmail.com

unread,
May 23, 2015, 11:23:21 PM5/23/15
to mobile-c...@googlegroups.com
Do you recomend using CBLUITablesource? It seems to work well with live queries. I have another situation where I want to use a regular UITableview in order to use sections, and my question is about CBLLiveQuery. How do you use CBLLiveQuery with a regular UITableview. I have not had any luck making it work. I have made CBLQuery work correctly by enumerating the query and storing objects in an array to use for UiTableview data source. But I do not think my data will refresh when something changes like CBLLiveQuery. Any thoughts to share about this topic. Basically does live queries only work with CBLTableSource.
Thanks again.

Jens Alfke

unread,
May 24, 2015, 12:58:40 AM5/24/15
to mobile-c...@googlegroups.com

On May 23, 2015, at 8:23 PM, kde...@gmail.com wrote:

How do you use CBLLiveQuery with a regular UITableview. I have not had any luck making it work.

CBLUITableSource isn’t special; it’s just a simple implementation of binding a live query to a table view. If you’re having trouble building your own, feel free to copy its source code and extend or adapt it.

—Jens
Reply all
Reply to author
Forward
0 new messages