I am trying to update table view from Core data like this:
dispatch_queue_t request_queue = dispatch_queue_create("com.myqueue.qu", NULL);
dispatch_async(request_queue, ^{
NSManagedObjectContext *parent = [[CoreDataManager instance] managedObjectContext];
NSManagedObjectContext *searchContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:parent.concurrencyType];
[searchContext setPersistentStoreCoordinator:[[CoreDataManager instance] persistentStoreCoordinator]];
NSArray * array = ...// receive array from CoreData, just select, there is no changes
if (array.count > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
block(array);
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
block(nil);
});
}
});
dispatch_release(request_queue);
In block I do:
_tableViewData = [array mutableCopy];
[self.tableView reloadData];
But in:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
My obj:
MyObj *obj = _tableViewData[indexPath.row];
With nil fields. So obj is empty.
Edit:
I tried to search without creating 'searchContext', using parentContext and tableView updated ok, Is it correct way?
dispatch_queue_t request_queue = dispatch_queue_create("com.myqueue.qu", NULL);
dispatch_async(request_queue, ^{
NSManagedObjectContext *parent = [[CoreDataManager instance] managedObjectContext];
NSArray * array = ...// receive array from CoreData, using parent as search context
...