beretis
beretis

Reputation: 909

Tableview populating from database in separate queue

I'm implementing a simple tableview, which gets data from a SQLite database. I have this code:

-(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];
                                      });
                   });
}

The problem is that the cell is empty when added. The interesting thing is that when I do it all on the main thread, everything is just fine:

-(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];

                                      });

                   });
}

I just don't understand this.

Upvotes: 1

Views: 60

Answers (3)

skyler
skyler

Reputation: 742

-(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];
                                      });
                   });
}

Try doing this way.

Upvotes: 0

Naga Mallesh Maddali
Naga Mallesh Maddali

Reputation: 1005

I feel issue is because you are trying to access objects of ClipboardData in getAllClipBoards, using separate thread other than main thread. ClipboardData has UI components in it. So, you are accessing them in a non-main thread, which is causing issue.

If you try to call getAllClipBoards in a main thread then you wont see any issue.

Upvotes: 1

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this,

-(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];
                                      });
                   });
}

Upvotes: 0

Related Questions