View with all the documents of a certain type

I’m a full beginner with everything map/reduce, and I’m trying to do something really simple in my iOS app in Swift.
I would like to create a view with all the documents whose “type” property has a certain value (in this case “pixync”)

So In my AppDelegate’s didFinishLaunchingWithOptions, I have the following code:

func setupCouchbase() {
    let manager = CBLManager.sharedInstance()
    if manager == nil {
        println("Cannot create Manager instance")
    } else {
        var error:NSError? = nil
        let database = manager.databaseNamed("pixync", error: &error)
        if database == nil {
            println("Cannot create or get database pixync because \(error?.debugDescription)")
        } else {
            var pixyncsView = database.viewNamed("pixyncs")
            pixyncsView.setMapBlock({ (doc, emit) -> Void in
                if doc["type"] as? String == "pixync" {
                    emit(doc["title"], doc)
                }
            }, version: "1")
        }
    }
}

Now when I set a breakpoint on the first line of the map block, add some documents and then query the view with the following code, my breakpoint is never hit, and the query doesn’t have any result:

if let database = CBLManager.sharedInstance().databaseNamed("pixync", error: &error) {
            if let query = database.viewNamed("pixyncs").createQuery() {
                var enumerator = query.run(&error)
                for var i:UInt = 0; i < UInt(enumerator.count); i++ {
                    println(enumerator.rowAtIndex(i))
                }
            }
        }

Obviously, I’m doing something wrong here, but again, I’m completely new to map/reduce so I’m lost.

After doing some debugging, there might be something wrong in my code to create the documents too because even with an all-documents query, there doesn’t seem to be anything in the database, even though the code says the document was successfully created. I’m really lost.

if let database = CBLManager.sharedInstance().databaseNamed("pixync", error: &error) {
        let pixync = [
            "type": "pixync",
            "title": self.titleCell!.field.text,
            "date": self.date!,
            "device": UIDevice.currentDevice().identifierForVendor.UUIDString
        ]
        var pixyncDoc = database.createDocument()
        if pixyncDoc.putProperties(pixync, error: &error) != nil {
            println("Failed to save document because \(error?.debugDescription)")
        } else {
            println("Document written to database pixync with Id = \(pixyncDoc.documentID)")
        }
    }

I figured it out. In fact I was mistaken in thinking that my document was correctly saved because I reversed my if condition on putProperties. As a matter of fact, my document was not correctly saved because I try to save an NSDate as is in the database.

Glad you figured it out!