How to Set an NSInteger __block variable to comboboxFilter.indexOfSelectedItem inside of dispatch_async

36 Views Asked by At

I have been trying to set an NSInteger block variable to the value of a combobox's selected index inside a thread. The variable is always zero outside of the dispatch_async even though it has been set inside the dispatch_async.

When I NSLog the value of the variable "comboboxFilterSelectedIndex" from within the dispatch_async it holds the correct index value, however it looses the value when returning the DoSearch thread.

Needless to say I am not proficient at objective c and could sure use some help.

Thank you

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    doSearch(directoryURL, words, self->tableContents, self.tableViewOutlet, self.checkboxLookInPathOutlet, self.checkboxContainsAllWordsOutlet, self.checkboxContainsWholeWordsOutlet, self.comboboxFilterOutlet, self.labelHitCountOutlet, self.imageViewThumbnailOutlet, self.labelFilesCountOutlet, self.textViewMetadataOutlet, self.labelElapsedTimeOutlet);
    //dispatch_async(dispatch_get_main_queue(), ^(){


    // Next 2 must be used in main thread only
    dispatch_async(dispatch_get_main_queue(), ^{
        self.buttonSearchOutlet.enabled = true;
        self.buttonSearchOutlet.title = @"Search";
    });
    return;

    //});
});


void doSearch(NSURL *searchPathURL, NSArray *searchWords, 
    NSMutableArray *tableContents, NSTableView *tableView, NSButton 
    *checkboxLookInPath, NSButton *checkboxContainsAllWords, NSButton 
   *checkboxContainsWholeWords, NSComboBox *comboboxFilter, 
    NSTextField *labelHitCount, NSImageView * imageViewThumbnail, 
    NSTextField *labelFilesCount, NSTextView *textViewMetadata, 
    NSTextField *labelElapsedTime){

    For ...{

        //__block NSInteger comboboxFilterSelectedIndex = All;
        __block NSInteger comboboxFilterSelectedIndex = 0;
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"------------>%lu", comboboxFilter.indexOfSelectedItem); // Value is correct
            comboboxFilterSelectedIndex = comboboxFilter.indexOfSelectedItem;
            NSLog(@"------------>%lu", comboboxFilterSelectedIndex); // Value is correct
        });

        NSLog(@"------------>%lu", comboboxFilterSelectedIndex); //Value is Always = 0

        ...
    }
}
0

There are 0 best solutions below