NSTimer:如何将它用于数据库

时间:2012-08-03 10:02:18

标签: iphone ios xcode sqlite

我有一个包含5个记录的数据库,我需要使用计时器逐个显示屏幕上的记录。时间间隔应该是30秒。

- (NSMutableArray*) fnGetName 
{

DBCon *oDBCon = [[DBCon alloc] init];
[oDBCon fnInitializeDB];
NSMutableArray *arrDict = [[NSMutableArray alloc] init];
NSString *sQryName = [NSString stringWithFormat:@"SELECT * FROM tbl_names"];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(oDBCon.database, [sQryName UTF8String], -1, &selectstmt, NULL) == SQLITE_OK) 
{
        while(sqlite3_step(selectstmt) == SQLITE_ROW) {
        oBlossom = [[Blossom alloc]initWithPrimaryKey:primaryKey database:oDBCon.database];
        oBlossom.psName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
        NSLog(@"oBlossom.psName %@", oBlossom.psName);
        oBlossom.psKeyword = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,2)];
        NSLog(@"oBlossom.psKeyword %@", oBlossom.psKeyword);
        [arrDict addObject:oBlossom];

}

1 个答案:

答案 0 :(得分:0)

返回 arrDict 并使用Timer更新UI

注意:完成操作后使计时器失效。否则它不会停止并导致崩溃。

-(void)disaplydisplayRecordsWithTimeInterval{

    NSMutableArray *myCollection = [self fnGetName];

    NSTimer timerWithTimeInterval:30.0 target:self selector:@selector(displayRecords:) userInfo:arrDict repeats:YES];

}

- (void)displayRecords:(NSTimer*)theTimer {

    NSMutableArray *myArrayCollection = (NSMutableArray *)theTimer.userInfo;
    //your code here to displayRecords using myarrDict
}

- (NSMutableArray*) fnGetName {

    DBCon *oDBCon = [[DBCon alloc] init];
    [oDBCon fnInitializeDB];
    NSMutableArray *arrDict = [[NSMutableArray alloc] init];
    NSString *sQryName = [NSString stringWithFormat:@"SELECT * FROM tbl_names"];
    sqlite3_stmt *selectstmt;
    if(sqlite3_prepare_v2(oDBCon.database, [sQryName UTF8String], -1, &selectstmt, NULL) == SQLITE_OK) 
    {
        while(sqlite3_step(selectstmt) == SQLITE_ROW) {
            oBlossom = [[Blossom alloc]initWithPrimaryKey:primaryKey database:oDBCon.database];
            oBlossom.psName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
            NSLog(@"oBlossom.psName %@", oBlossom.psName);
            oBlossom.psKeyword = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,2)];
            NSLog(@"oBlossom.psKeyword %@", oBlossom.psKeyword);
            [arrDict addObject:oBlossom];

        }

        //finilize database here
    }

    //close database here

    return arrDict;
}