我正在尝试根据FMDB数据库创建一个收藏夹表。按下另一个屏幕上的按钮会在数据库中注册更改,但必须重新加载应用程序才能使收藏页面更新最新的添加。有没有更好的地方这样做,以便收藏页面自动反映数据库更改?下面是我用来在收藏夹视图控制器中调用DB的代码。
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // Get the documents directory
docsDir = dirPaths[0];
NSString *path = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"passages.db"]];
FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];
FMResultSet *results = [database executeQuery:@"SELECT * FROM passages WHERE favorite=1"];
NSMutableArray *favorites = [[NSMutableArray alloc] init];
while ([results next]){
NSMutableDictionary *pass = [[NSMutableDictionary alloc]init];
[pass setObject:[results stringForColumn: @"thing1"] forKey:@"thing1"];
[pass setObject:[results stringForColumn: @"thing2"] forKey:@"thing2"];
[pass setObject:[results stringForColumn: @"thing3"] forKey:@"thing3"];
[favorites addObject:pass];
}
[results close];
[database close];