Use dateToArray() in Objective-C CBLView's setMapBlock: emit?

Hello,

in javascript, map function has builtin function dateToArray():

function(doc,meta){
emit( dateToArray(doc.created_at), …)
}…

Is it possible that I can use dateToArray function in Objective-C? and How?

Here is my code:

CBLView* view = [database viewNamed: @“cpfollow_view”];

[view setMapBlock: ^(NSDictionary* doc, void (^emit)(id key, id value)){
    

    if (doc[@"domain"] &&  doc[@"created_at"] && [doc[@"type"] isEqualToString:kDocType]){

//HOW to Add dateToArray() to doc[@“created_at”] ?
NSArray* key = @[doc[@“domain”], doc[@“created_at”] ];

        emit(key, doc); 
    }
    
} version: @"2"]; // bump version any time you CHANGE the MAPBLOCK body!

Thank You!!

I don’t know exactly what dateToArray does, but your Objective-C map function has access to the Foundation framework’s date handling classes, like NSDate, NSCalendar and NSDateFormatter, which you can use to do anything you want with dates.

(But watch out: date string parsing and formatting are surprisingly expensive, on any platform. If you can avoid them, your view will index faster. For instance, date strings already sort in chronological order, so you don’t need to break them up into components to get sorting.)

Hi Jens,

Thanks for your prompt reply. I understood and I will use couchbase server view REST API instead (Couchbase SDKs) ( Couchbase SDKs)to group dates into different levels.

Thanks!
Tim

You can’t use Couchbase Server APIs on a Couchbase Lite database.

You can’t even use them to talk to the server from a client, because the Couchbase Server will be behind a firewall and not reachable from outside. (Or if it isn’t, you have a serious security problem.)

dateToArray() was championed by @jchrisa as a feature for Couchbase Server. Since that compound key query is really useful for Couchbase (lite or server) aggregations, should we consider adding something that takes NSDate and gives a similar array?

1 Like