Tableview从单独队列中的数据库填充

时间:2014-07-30 09:56:42

标签: ios sqlite uitableview grand-central-dispatch

我实现了一个简单的tableview,它从SQLite数据库中获取数据。我有这段代码:

-(void)fetchDataFromDb
{

    [self.activityIndicator startAnimating];
    [self cleanAllArrays];

    __block NSMutableArray* newAllClipboards = [[NSMutableArray alloc] init];
    dispatch_queue_t workingQueue = dispatch_queue_create("workingQueuee", NULL);
    dispatch_async(workingQueue,
                   ^{
                       newAllClipboards = [[NSMutableArray alloc] initWithArray:[self.dbAcess getAllClipBoards]];

                       dispatch_async(dispatch_get_main_queue(),
                                      ^{
                                          self.allClipboards = newAllClipboards;
                                          [self sortClipboardsSentOrDelivered];
                                          [self.tableVIew reloadData];
                                          [self.activityIndicator stopAnimating];
                                      });
                   });
}

问题是添加时单元格是空的。有趣的是,当我在主线程上完成所有操作时,一切都很好:

-(void)fetchDataFromDb
{

    [self.activityIndicator startAnimating];
    dispatch_queue_t workingQueue = dispatch_queue_create("workingQueuee", NULL);
    dispatch_async(workingQueue,
                   ^{
                       dispatch_async(dispatch_get_main_queue(),
                                      ^{
                                          [self cleanAllArrays];
                                          self.allClipboards = [[NSMutableArray alloc] initWithArray:[self.dbAcess getAllClipBoards]];
                                          [self sortClipboardsSentOrDelivered];
                                          [self.tableVIew reloadData];
                                          [self.activityIndicator stopAnimating];

                                      });

                   });
}

我只是不明白这一点。

3 个答案:

答案 0 :(得分:1)

我觉得问题是因为您尝试使用除主线程之外的单独线程来访问ClipboardData中的getAllClipBoards对象。 ClipboardData中包含UI组件。因此,您正在非主线程中访问它们,这会导致问题。

如果您尝试在主线程中调用getAllClipBoards,那么您将看不到任何问题。

答案 1 :(得分:0)

试试这个,

-(void)fetchDataFromDb
{

    [self.activityIndicator startAnimating];
    [self cleanAllArrays];

    __block NSMutableArray* newAllClipboards = [[NSMutableArray alloc] init];
    dispatch_queue_t workingQueue = dispatch_queue_create("workingQueuee", NULL);
    dispatch_async(workingQueue,
                   ^{
                       newAllClipboards = [[NSMutableArray alloc] initWithArray:[self.dbAcess getAllClipBoards]];
                       typeof(newAllClipboards) __weak weakObj = newAllClipboards;
                       dispatch_async(dispatch_get_main_queue(),
                                      ^{
                                          self.allClipboards = weakObj;
                                          [self sortClipboardsSentOrDelivered];
                                          [self.tableVIew reloadData];
                                          [self.activityIndicator stopAnimating];
                                      });
                   });
}

答案 2 :(得分:0)

-(void)fetchDataFromDb
{

    [self.activityIndicator startAnimating];
    [self cleanAllArrays];

    __block NSMutableArray* newAllClipboards = [[NSMutableArray alloc] init];
    dispatch_queue_t workingQueue = dispatch_queue_create("workingQueuee", NULL);
    dispatch_async(workingQueue,
                   ^{
                       newAllClipboards = [[NSMutableArray alloc] initWithArray:           [self.dbAcess getAllClipBoards]];
                        self.allClipboards = newAllClipboards;
                        [self sortClipboardsSentOrDelivered];
                       dispatch_async(dispatch_get_main_queue(),
                                      ^{
                                          [self.tableVIew reloadData];
                                          [self.activityIndicator stopAnimating];
                                      });
                   });
}

尝试这样做。

相关问题