Iphone NSTimer:如何将其用于数据库
我有一个数据库,其中有5条记录,我需要使用计时器在屏幕上逐个显示记录。时间间隔应为30秒Iphone NSTimer:如何将其用于数据库,iphone,ios,xcode,sqlite,Iphone,Ios,Xcode,Sqlite,我有一个数据库,其中有5条记录,我需要使用计时器在屏幕上逐个显示记录。时间间隔应为30秒 - (NSMutableArray*) fnGetName { DBCon *oDBCon = [[DBCon alloc] init]; [oDBCon fnInitializeDB]; NSMutableArray *arrDict = [[NSMutableArray alloc] init]; NSString *sQryName = [NSString stringWithFormat:@"S
- (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];
}
返回arrDict并使用计时器更新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;
}
希望这个链接能对你有所帮助!并在没有记录后使其失效,否则它将无用地占用内存或可能崩溃