Assertion failed _viewID>0 terminating app

Hello.

I am having some issues getting couch base lite to work.

I am using this setup in my controllers and classes:

// creates the manager object
- (BOOL) createTheManager {
    
    // create a shared instance of CBLManager
    _manager = [CBLManager sharedInstance];
    if (!_manager) {
        NSLog (@"Cannot create shared instance of CBLManager");
        return NO;
    }
    
    NSLog (@"Manager created");
    
    return YES;

}

- (BOOL) isDatabaseCreated : (NSString *) name {

// create a database
if (![self createTheDatabase : name]) return NO;

return YES;

}

// creates the database
- (BOOL) createTheDatabase : (NSString *) name {

NSError *error;

// create a name for the database and make sure the name is legal
NSString *dbname = name;
if (![CBLManager isValidDatabaseName: dbname]) {
    NSLog (@"Bad database name");
    return NO;
}

// create a new database
_database = [_manager databaseNamed: dbname error: &error];
    
    if (!_database) {
        NSLog (@"database error . Error message: %@", error.localizedDescription);
        return NO;
    }

// log the database location
NSString *databaseLocation = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingString: @"/Library/Application Support/CouchbaseLite"];
NSLog(@"Database %@ created at %@", dbname, [NSString stringWithFormat:@"%@/%@%@", databaseLocation, dbname, @".cblite"]);

return YES;

}

This is how I am using the code once I make an instance of the database:

- (void)insert:(NSString *)name isCurrent:(BOOL)isCurrent
{
NSError *error;

CBLQuery *query = [[_database viewNamed:@"test"] createQuery];
[query setStartKey: name];
CBLQueryEnumerator *result = [query run:nil];
NSNumber *total = [[result rowAtIndex:0] value];
if(total == 0)
{
    // add item
    NSMutableDictionary *item = [NSMutableDictionary new];
    item[@"name"] = name;
    item[@"isCurrent"] = [NSNumber numberWithBool:isCurrent];

    // Create an empty document
    CBLDocument *doc = [_database documentWithID: name];
    // Save the ID of the new document
    NSString *docID = doc.documentID;
    
    // Write the document to the database
    CBLRevision *newRevision = [doc putProperties:course error:
                                &error];
    if (newRevision) {
        NSLog(@"Document created and written to database, ID = %@", docID);
    }
    else
    {
        // retrieve the document from the database
        CBLDocument *retrievedDoc = [_database documentWithID: @"Burgers"];
        // display the retrieved document
        NSLog(@"The retrieved document contains: %@", retrievedDoc.properties);
      }
  }
}

I need to know what I am doing wrong here and I want to have two databases open at the same time “test” and “test2”, how would I do that so I wouldnt get a concurrent issue?

Are you creating a view before running your query?

It would help to say where in your code the assertion failure is happening.

Ok. maybe this is my issue why it isnt inserting. I dont know how to make a view before running the query can you share how I would code that in or what exactly the view does as i thought the view was the database name?

Sure, take a look at the guide here: http://developer.couchbase.com/documentation/mobile/1.2/develop/guides/couchbase-lite/native-api/view/index.html

Thanks I was just reading about this… but I still don’t understand how I would change my code to make it query what I need, maybe you can give a suggestion?

Not exactly sure what you need from the code. It looks like you might be be querying all documents. For that you don’t need a view. You can read about that here: http://developer.couchbase.com/documentation/mobile/1.2/develop/guides/couchbase-lite/native-api/query/index.html

ok awesome, I have been making some good progress now with this fix, but I am facing some concurrency issues as I am trying to access the database in other methods. How do I access the database to do other work so its on the same thread, etc?

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: '***** THREAD-SAFETY VIOLATION: This database is being used on a thread it wasn’t created on! Please see the concurrency guidelines in the Couchbase Lite documentation. *****

See the concurrency guidelines in the Couchbase Lite documentation.