简体   繁体   中英

reloadRowsAtIndexPaths with sqlite (FMDB)

i'm using the sqlite db with FMDB wrapper to populate my UITableView, i make thread in background that fetch from a server and update the data in the sqlite, every time this happens i reload all UITableView with a notification, and happens that there are 20 reload data consecutively and when this happens it's impossible to scroll the uitableview because it's blocked to relaod consecutively, so my question is if there is a way to reload better the uitableview that lets the user to scroll the uitableview, i'm thinking about Reloadrowsatindexpaths, but if i reload only one cell, i need however to refecth from the sqlite the content because to fetch the content of uitableview i do this:

 - (void)loadTableViewInfo
{
[self.source removeAllObjects];

FMDatabase *db = [FMDatabase databaseWithPath:path];

if ([db open]) {
    FMResultSet *sectionQuery = [db executeQuery:@"SELECT name,id,age FROM user"];

    while([sectionQuery next])
    {
      [self.source addObject:[sectionQuery resultDictionary]];
    }
 }
 }

the soruce is the data soruce of the array, anyone can tell me how i can do?

You're probably blocking on the main thread, preventing the UITableView from being able to respond to the user's interactions.

A quick fix would be to spawn a background thread to execute the query and place the data in self.source :

- (void)loadTableViewInfo {
    [self performSelectorInBackground:@selector(doBackgroundQuery) withObject:nil];
}

- (void)doBackgroundQuery {
    [self.source removeAllObjects];

    FMDatabase *db = [FMDatabase databaseWithPath:path];

    if ([db open]) {
        FMResultSet *sectionQuery = [db executeQuery:@"SELECT name,id,age FROM user"];

        while([sectionQuery next])
        {
            [self.source addObject:[sectionQuery resultDictionary]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
    }
}

The long-term solution would be to use NSOperationQueue . Let me know if you have any questions about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM