从sqlite db

时间:2016-04-29 10:42:45

标签: ios objective-c sqlite uitableview

我在数据库中有超过10000条记录。我想在表视图上加载它。从数据库中获取所有数据需要3到4秒,然后将其加载到表视图中。是否有更有效的方法来增加响应并在uitableview中加载数据? 这是我从数据库中获取所有数据的代码

- (void)getAllData {
    NSString * convertInttoStr = [NSString stringWithFormat:@"%d", rowNumber];
    // Getting the database path.
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"iandroidquran_database 3.sqlite"];

    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];
    NSString *sqlSelectQuery = [NSString stringWithFormat:
                                @"SELECT * FROM qSurahContent WHERE surahID=%@" ,
                                convertInttoStr];


    // Query result
    FMResultSet *resultsWithNameLocation = [database executeQuery:sqlSelectQuery];
    while([resultsWithNameLocation next]) {
        NSString *strID = [NSString stringWithFormat:@"%d",[resultsWithNameLocation intForColumn:@"surahID"]];
        NSString *strName = [NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"surahContentArabic"]];

        NSLog(@"surahID = %@, surahName = %@",strID, strName);
        [surahId addObject:strID];
        [surahContentArabic addObject:strName];


    }
    [self.tblView reloadData];

    [database close];
}

任何解决方案?感谢

1 个答案:

答案 0 :(得分:0)

尝试从主线程中的DB获取数据,如下所示:

class Category extends AppModel {
public $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product',
'joinTable' => 'products_categories',
'foreignKey' => 'category_id',
        'associationForeignKey' => 'product_id',
        'unique' => 'keepExisting',
    ),
);
}

class Product extends AppModel {
public $primaryKey = 'id';
public $hasMany = array(
    'ProductSwatch' => array(
        'className' => 'ProductSwatch',
        'foreignKey' => 'product_id',
         'dependent' => true
    ),
    'ProductDimension' => array(
        'className' => 'ProductDimension',
        'foreignKey' => 'product_id',
         'dependent' => true
    ),
    'ProductCare' => array(
        'className' => 'ProductCare',
        'foreignKey' => 'product_id',
         'dependent' => true
    ),
    'ProductDimension' => array(
        'className' => 'ProductDimension',
        'foreignKey' => 'product_id',
        'dependent' => false,
    ),
    'ProductCare' => array(
        'className' => 'ProductCare',
        'foreignKey' => 'product_id',
        'dependent' => false,
    ),
    'Review' => array(
        'className' => 'Review',
        'foreignKey' => 'product_id',
         'dependent' => true,
    )
);
}

这个主线程具有高优先级,并且执行速度比以往任何时候都快。试试吧。

希望有所帮助......