CBL iOS unable to create multiple views

mobile

#1

Hi,

I am witnessing some strange behavior with the local views on CBL for iOS and I am unable to create multiple views.

In my initDataService function, I have this:

...
self.database = try CBLManager.sharedInstance().databaseNamed(username);
self.createViewOne();
...

createViewOne function:

private func createViewOne() -> Bool {
    guard database != nil else { return false }

    let view1: CBLView = self.database.viewNamed("viewOne");
    view1.setMapBlock({(doc, emit) in
        print("inside my first view");
        if ("TYPE_A" == doc["type"] as! String) {
            emit(doc["date"] as! String, doc);
        }
    }, version: "1");

    return true;
}

This works just fine. However, if I add a second view, like this:

 ...
self.database = try CBLManager.sharedInstance().databaseNamed(username);
self.createViewOne();
self.createViewTwo();
...

and createViewTwo function looks like this:

private func createViewTwo() -> Bool {
    guard database != nil else { return false }

    let view2: CBLView = self.database.viewNamed("viewTwo");
    view2.setMapBlock({(doc, emit) in
        print("inside my second view");
        if ("TYPE_B" == doc["type"] as! String) {
            emit(doc["date"] as! String, doc);
        }
    }, version: "1");

    return true;
}

When I run the app on a simulator, the second view is never created. The print statement is never run, even though I’ve verified with Couchbase Lite Viewer that the docs have in fact synced with the phone, and also, the view one works fine.

Also, if I get rid of creating a second view, but change the name of the first view, so that createViewOne function looks like this:

private func createViewOne() -> Bool {
    guard database != nil else { return false }

    let view1: CBLView = self.database.viewNamed("viewOneB");  //added a 'B' to make view name different
    view1.setMapBlock({(doc, emit) in
        print("inside my first view");
        if ("TYPE_A" == doc["type"] as! String) {
            emit(doc["date"] as! String, doc);
        }
    }, version: "1");

    return true;
}

and I run it on the simulator, the app crashes with the following error:

2016-09-02 03:22:57.704 MyApp[99924:2908368] *** ASSERTION FAILED: _viewID > 0
2016-09-02 03:22:57.704 MyApp[99924:2908368] *** Assertion failure in -[CBL_SQLiteViewStorage mapTableName](), /Users/jenkins/jenkins/workspace/couchbase-lite-ios-builds/couchbase-lite-ios/Source/CBL_SQLiteViewStorage.m:226
2016-09-02 03:22:57.708 MyApp[99924:2908368] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '_viewID > 0'

In between each test on the simulator, I am deleting the app from the device. I’ve tried resetting the content & settings on the simulator also.

I’ll note that I am using a custom proxy between the CBL on the iOS device and the Sync Gateway. I’m also using v1.3 of iOS CBL.

This error is new as I’ve successfully created multiple views before upgrading to 1.3. Any help is appreciated.

Thanks in advance.

EDIT*** I will put together steps to reproduce from scratch once at office.


#2

Well, a view isn’t indexed until it’s first queried, so if you don’t query view 1 you won’t see it’s log message.

Besides that, I don’t have any ideas … we have a lot of unit tests that do stuff like this, so I’m pretty sure things aren’t as broken as they look here!

Please file an issue on Github, and if you can include a runnable program to reproduce this, that would be awesome.


#3

I’ve created an issue here. There’s a link in the issue to a small program meant to illustrate the issue. However, when creating a new project, I no longer got the crash and instead it’s just like the views don’t index. I’ve also added in a call to create a query.