Why my newly created database holds reference to the views created with previous deleted database?

we are facing a new exception after upgrading to iOS couchbase lite 1.3.1,especially after deleting the database and creating again.After long time of analysis, understood that issue is due to CBL_Shared property of CBLManager retains some old reference. More details below,

1.Before deleting the database,created four different views using different instances of databases.
CBLManager *cblManager = [[CBLManager sharedInstance]copy]; CBLDatabase *couchDB = [cblManager databaseNamed:databaseName error:&error];
2. After deleting the database,checked the CBL_Shared property of the database’s manager, it was nil.
3. Created new database.Check the shared property of the newly created database’s manager(before creating any new views), it showed the same four views as a value, for key “mapversiontable”.

Also understood from the source code that, when a new view is created, it expects the viewID value as “-1”, since it holds the old reference, returns the value which is greater than “0” and the app is crashing because of that

Deleting the database should wipe out all the caches and old data right? How it is happening then?

i am able to create views, if i kill the instance of my application and launch again on device :frowning: Not able to understand the behaviour

I’m not clear on what’s happening. (In particular, there is no public API named CBL_Shared.) Can you show some code clarifying what you’re doing?

Sorry Jens. Let me explain the above steps with code snippets and screenshots.
1.Created a database and created some views using that database as below,

Database creation

CBLManager *cblManager = [[CBLManager sharedInstance]copy];
CBLDatabase *couchDB = [cblManager databaseNamed:databaseName error:&error];

Creating View (sample)

  • (NSMutableArray *)getEmpDtlsFromDataBase:(CBLDatabase *)db {
    NSMutableArray *arrEmpDtls = [[NSMutableArray alloc] init];
    CBLView *viewEmp = [db viewNamed:VW_GET_EMPLOYEE];
    [viewEmp setMapBlock:MAPBLOCK({
    id docType = [doc objectForKey:kDocumentType];
    if ([docType isEqualToString:kEmployee])
    emit(doc, nil);
    }) reduceBlock:nil version:@“1.0”];

    CBLQuery *query = [[db viewNamed:VW_GET_EMPLOYEE] createQuery];
    query.descending = NO;
    NSError *error;
    CBLQueryEnumerator *queryEnumerator = [query run:&error];
    for (int i = 0; i < queryEnumerator.count; i++) {
    CBLQueryRow *row = [queryEnumerator rowAtIndex:i];
    CBLDocument *doc = row.document;
    Employee *empl = [Employee modelForDocument:doc];
    if (empl != nil) {
    [arrEmpDtls addObject:empl];
    }
    }
    return arrEmpDtls;
    }

Like the view created above, we have created other views also to fetch the required data

see the attachment(Edited_1

) for the value of CBL_Shared property in the debug area, after creating views

2.After deleting the database,checked the CBL_Shared property of the database’s manager using breakpoints, it was nil.

Deleting the database and immediately creating the database as below,

-(void)doPurgeDatabase{
NSError *error;
BOOL isDeletionSuccess = NO;
CBLManager *manager =[[CBLManager sharedInstance]copy];
NSString *filePath =[CBLManager defaultDirectory];
CBLDatabase *database=[manager databaseNamed:kCBLDataBaseName error:&error];
if (database) {
[database.manager close];
manager = nil;
isDeletionSuccess=[database deleteDatabase:&error];
database = nil;
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager]removeItemAtPath:filePath error:&error];
if (error) {
CCLog(@“printing error while removing file:%@”,error.localizedDescription);
}
}
if (isDeletionSuccess) {
BOOL status = [DBHandler createDataBase:kCBLDataBaseName];
if (!status) {
CCLog(@“error while creating database”);
}
}else{
CCLog(@“error while deleting database%@”, error.localizedDescription);
}
}
}

Value of the CBL_Shared property after deleting the database (check Edited_2 for reference),

3.Value of the CBL_Shared Property immediately after creating a new database (after deletion),

Though i have just created the database, already created views are shown in the newly created database CBL_Shared property.We tried clearing cache or any temp documents (after database deletion) if any exists, but could not find anything. Pls help us out on this issue.

manager = nil;        
isDeletionSuccess=[database deleteDatabase:&error];

I think that’s your problem. Closing the manager implicitly closes all its databases. Deleting a database after closing it is technically illegal; it’s probably a no-op.

Delete the database first, then close the manager.

Jens , if i does not close the manager before deleting the database, the following warning is coming,

WARNING: <CBL_Shared: 0x79f35900>: Still waiting to -forgetDatabaseNamed: “couchdb” {at -[CBL_Shared forgetDatabaseNamed:]:139}

-(void)doPurgeDatabase{

NSError *error;
CBLDatabase *database=[[[CBLManager sharedInstance]copy]databaseNamed:kCBLDataBaseName error:&error];
if(database){
    bool isDeletionSuccess = [database deleteDatabase:&error];
    if (isDeletionSuccess) {
        [database.manager close];
        database=nil;
        [self createDatabase];
        
    }else{
        CCLog(@"error while deleting database:%@",error.localizedDescription);
    }
}

}

-(void)createDatabase{
BOOL status = [DBHandler createDataBase:kCBLDataBaseName];
if (!status) {
CCLog(@“error while creating database”);
}
}

Why do you make a copy of the shared CBLManager just for that one call?

You should have one CBLManager instance, hold that in a long-lived variable, and use it for everything on that thread. (If you use multiple threads, do the same with each thread.)

PS: To quote blocks of code, put a line of three back-quotes before and another after the code.