How to update an existing view and update index ? (ios / swift)

已查看 96 次
跳至第一个未读帖子

Pelle Krøgholt

未读,
2015年5月12日 04:01:522015/5/12
收件人 mobile-c...@googlegroups.com


Hi,

I have a couple of design documents that holds some views for example a document-types doc with theses views 

                        ...
                        if (typeof doc.type !== 'undefined') {
                            if (doc.type === 'foo') {
                                emit(doc.type, {
                                    title: doc.title,
                                    attribute: doc.attribute
                                });
                            }
                            else if (doc.type === 'bar') {
                                emit(doc.type, {
                                    title: doc.title,
                          ....



then I have a utility function that returns a compiled view (the current working solution in Java ~ CouchUtils.java) that can be queried.

In swift I have a small starter problem i roughly do

       let rev = database.existingDocumentWithID("_design/document-types")
       let views = rev.propertyForKey("views")

Now when I try to look up the properties of `views` then I keep getting:

    MyIOSAPP[26894:207053] -[__NSCFDictionary propertyForKey:]: unrecognized selector sent to instance 0x7fcc99e9a7e0

I also get this error when trying out:

    println(views.propertyList())
    println(views.propertyName())
    println(views.propertiesToSaveForDeletion())

 
Any hints appreciated !


Thx

Pelle

CouchUtils.java

Jens Alfke

未读,
2015年5月12日 11:48:242015/5/12
收件人 mobile-c...@googlegroups.com

On May 12, 2015, at 1:01 AM, Pelle Krøgholt <pellek...@gmail.com> wrote:

Now when I try to look up the properties of `views` then I keep getting:

    MyIOSAPP[26894:207053] -[__NSCFDictionary propertyForKey:]: unrecognized selector sent to instance 0x7fcc99e9a7e0

Well, that error means exactly what it says: ‘views’ is an NSDictionary, and you’re trying to call -propertyForKey: on it, which isn’t an NSDictionary method. It sounds like you’re expecting that ‘views’ is a CBLDocument, but that doesn’t make sense — documents don’t contain documents.

Couchbase Lite documents are JSON objects. CBLDocument.properties returns that JSON object parsed into Cocoa objects of class NSDictionary, NSArray, NSString, NSNumber, or NSNull. Same goes for -propertyForKey:.

A hint for future debugging: when you’re curious what the value or class of a variable is at runtime, set a breakpoint there, then look at the value in the debugger. If the variable list doesn’t tell you, you can print an object using the ‘po’ command. If you want to know what its exact class is, try ‘po [views class]’.

—Jens

Pelle Krøgholt

未读,
2015年5月16日 15:08:482015/5/16
收件人 mobile-c...@googlegroups.com


On Tuesday, May 12, 2015 at 5:48:24 PM UTC+2, Jens Alfke wrote:
...


A hint for future debugging: when you’re curious what the value or class of a variable is at runtime, set a breakpoint there, then look at the value in the debugger. If the variable list doesn’t tell you, you can print an object using the ‘po’ command. If you want to know what its exact class is, try ‘po [views class]’.

OK thanks a lot for the nice explanation. I was in the debugger doing print etc. but clearly didn't get the `po` concept. I can now create a CBLView from a document ~ my use case is that have stored some views in a document.

So now I have a view and in ios/swift I can do view.deleteIndex(). In contrast to do delete index I had like to do view.updateIndex() which I can do in Java. But its not listed in the doc/api http://developer.couchbase.com/mobile/develop/references/couchbase-lite/couchbase-lite/view/view/index.html

So how to reindex a view in ios/swift?


 

Jens Alfke

未读,
2015年5月16日 17:54:532015/5/16
收件人 mobile-c...@googlegroups.com

On May 16, 2015, at 12:08 PM, Pelle Krøgholt <pellek...@gmail.com> wrote:

So now I have a view and in ios/swift I can do view.deleteIndex(). In contrast to do delete index I had like to do view.updateIndex() which I can do in Java. But its not listed in the doc/api

For some reason this method was accidentally not made public in iOS. I’ve just filed an issue. I’ll see if there’s time to get it into 1.1 (there should be, as it’s a trivial change to mark the method public.)

You never actually have to call this. A view’s index is updated if necessary before you query it. But the updateIndex method allows you to shift the time at which the re-indexing happens, for instance if you want the work to be done immediately after updating a bunch of documents instead of when the user views the data.

As a workaround you can issue a query with the smallest possible result set, e.g. setting limit=1. That will update the index, and the additional query time will be minimal. You can then just ignore the result. (Even better, you can run the query asynchronously so the updating happens in the background.)

—Jens

Jens Alfke

未读,
2015年5月16日 20:07:502015/5/16
收件人 mobile-c...@googlegroups.com

On May 16, 2015, at 2:54 PM, Jens Alfke <je...@couchbase.com> wrote:

For some reason this method was accidentally not made public in iOS. I’ve just filed an issue. I’ll see if there’s time to get it into 1.1 (there should be, as it’s a trivial change to mark the method public.)

Oops, I was wrong: View.updateIndex is not an official part of the public API. It just happens to be public in the Java implementation.

—Jens

Pelle Krøgholt

未读,
2015年5月18日 04:23:212015/5/18
收件人 mobile-c...@googlegroups.com
OK ~ I went for a simple limited query. Once again thanks for the explanations and confirming the `official` ios/android api ~ which brings me to the last question(TM):

whats the equivalent to(in Java):

view.setCollation(com.couchbase.lite.View.TDViewCollation.TDViewCollationRaw)

in ios ?

//

pelle

Jens Alfke

未读,
2015年5月18日 12:38:252015/5/18
收件人 mobile-c...@googlegroups.com

On May 18, 2015, at 1:23 AM, Pelle Krøgholt <pellek...@gmail.com> wrote:

whats the equivalent to(in Java):
view.setCollation(com.couchbase.lite.View.TDViewCollation.TDViewCollationRaw)
in ios ?

Again, View.collation isn’t in our public API (as you can see here.) The iOS codebase partially implements it but the accessors aren’t made public. We could add it without much trouble. Is this an important feature for you?

(Looking at the iOS implementation, I suspect non-default collation will have slow performance because the keys are still indexed by SQL in the default order. But I think with some of the indexing improvements in 1.1, we could fix that.)

—Jens

Pelle Krøgholt

未读,
2015年5月28日 07:34:002015/5/28
收件人 mobile-c...@googlegroups.com


On Monday, May 18, 2015 at 6:38:25 PM UTC+2, Jens Alfke wrote:

On May 18, 2015, at 1:23 AM, Pelle Krøgholt <pellek...@gmail.com> wrote:

whats the equivalent to(in Java):
view.setCollation(com.couchbase.lite.View.TDViewCollation.TDViewCollationRaw)
in ios ?

Again, View.collation isn’t in our public API (as you can see here.) The iOS codebase partially implements it but the accessors aren’t made public. We could add it without much trouble. Is this an important feature for you?

currently its not an important thing - it turned out the views we build works fine.

 
(Looking at the iOS implementation, I suspect non-default collation will have slow performance because the keys are still indexed by SQL in the default order. But I think with some of the indexing improvements in 1.1, we could fix that.)

ok thx for the explanation and looking into it.
回复全部
回复作者
转发
0 个新帖子