Question

for some reason i have a dispatch_async thread, and it crashes unless i have a NSLog() method executed in front of it. the block runs a method that retrieves a username from a database.

crash:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    user_web_communicator *usrWeb = [[user_web_communicator alloc]init];
    NSString *author = [usrWeb getUsernameFromID:author_string];
    [_author_label setText:[NSString stringWithFormat:@"Author: %@",author]];
});

working:

NSLog(@"Fetching author for id: %@",author_string);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    user_web_communicator *usrWeb = [[user_web_communicator alloc]init];
    NSString *author = [usrWeb getUsernameFromID:author_string];
    [_author_label setText:[NSString stringWithFormat:@"Author: %@",author]];
});

error

2013-08-19 13:56:06.149 Poll Me[4995:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '{

Rows: AsyncImageView:0x827aa00.minX == 10 + 1*0x827b8d0.marker + -1*0x8281210.marker + 0.5*0x8281260.marker AsyncImageView:0x827aa00.minY == 27.5 + -1*0x827b910.marker + -1*0x82812a0.marker + 0.5*0x82812e0.marker + 0.5*0x82823f0.marker + -0.5*AsyncImageView:0x827aa00.Height Regular_Cell:0x827a400.Height == 56 + 1*0x82823f0.marker Regular_Cell:0x827a400.Width == 320 + 1*0x8281ee0.marker Regular_Cell:0x827a400.minX == 0 + 1*0x8281740.marker + -0.5*0x8281ee0.marker Regular_Cell:0x827a400.minY == 46 + 1*0x8281b30.marker + -0.5*0x82823f0.marker UILabel:0x827a8e0.Width == 0 + 1*0x827b730.marker + 1*0x827b790.marker + -1*0x827b7d0.marker + 1*UILabel:0x827adc0.Width UILabel:0x827a8e0.minX == 18 + 1*0x827b7d0.marker + 1*0x827b8d0.marker + -1*0x8281210.marker + 0.5*0x8281260.marker + 1*AsyncImageView:0x827aa00.Width UILabel:0x827a8e0.minY == 19.5 + -1*0x827b810.marker + 1*0x827b890.marker + -1*0x827b910.marker + -1*0x82812a0.marker + 0.5*0x82812e0.marker + 0.5*0x82823f0.marker + 0.5*AsyncImageView:0x827aa00.Height + -1*UILabel:0x827a8e0.Height + -1*UILabel:0x827b140.Height UILabel:0x827adc0.minX == 18 + 1*0x827b730.marker + 1*0x827b8d0.marker + -1*0x8281210.marker + 0.5*0x8281260.marker + 1*AsyncImageView:0x827aa00.Width UILabel:0x827adc0.minY == 20.5 + 1*0x827b6f0.marker + -1*0x82812a0.marker + 0.5*0x82812e0.marker UILabel:0x827b140.minX == 18 + 1*0x827b850.marker + 1*0x827b8d0.marker + -1*0x8281210.marker + 0.5*0x8281260.marker + 1*AsyncImageView:0x827aa00.Width UILabel:0x827b140.minY == 27.5 + 1*0x827b890.marker + -1*0x827b910.marker + -1*0x82812a0.marker + 0.5*0x82812e0.marker + 0.5*0x82823f0.marker + 0.5*AsyncImageView:0x827aa00.Height + -1*UILabel:0x827b140.Height UITableViewCellContentView:0x827a560.Height == 55 + 1*0x82812e0.marker UITableViewCellContentView:0x827a560.Width == 300 + 1*0x8281260.marker UITableViewCellContentView:0x827a560.minX == 0 + 1*0x8281210.marker + -0.5*0x8281260.marker UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker objective == <> + <750:-1>*0x8280fc0.negError + <250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker

Constraints: Marker:0x8281210.marker Marker:0x8281260.marker Marker:0x82812a0.marker (Integralization adjustment:0.5) Marker:0x82812e0.marker Marker:0x8281740.marker Marker:0x8281b30.marker Marker:0x8280fc0.posErrorMarker Marker:0x8281030.posErrorMarker Marker:0x827b6f0.marker Marker:0x827b730.marker Marker:0x827b790.marker Marker:0x827b7d0.marker Marker:0x827b810.marker Marker:0x827b850.marker Marker:0x827b890.marker Marker:0x827b8d0.marker Marker:0x827b910.marker Marker:0x8281ee0.marker Marker:0x82823f0.marker }: internal error. Cannot find an outgoing row head for incoming head 0x8280fc0.negError, which should never happen.' * First throw call stack: (0x195d012 0x166ae7e 0x195cdeb 0xefef89 0xf01fcf 0xf025c7 0xf0d58f 0xf0d6d4 0x7d860a 0x7e02af 0x7e03be 0x2e7601 0x49484e 0x354ced 0x2e940c 0x354a7b 0x359919 0x3599cf 0x3421bb 0x352b4b 0x2ef2dd 0x167e6b0 0x17dfc0 0x17233c 0x172150 0xf00bc 0xf1227 0xf18e2 0x1925afe 0x1925a3d 0x19037c2 0x1902f44 0x1902e1b 0x29be7e3 0x29be668 0x29effc 0x1e5ed 0x1d75) 2013-08-19 13:56:06.149 Poll Me[4995:4f03] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '{ Rows: AsyncImageView:0x827aa00.minX == 10 + 1*0x827b8d0.marker + -1*0x8281210.marker + 0.5*0x8281260.marker AsyncImageView:0x827aa00.minY == 27.5 + -1*0x827b910.marker + -1*0x82812a0.marker + 0.5*0x82812e0.marker + 0.5*0x82823f0.marker + -0.5*AsyncImageView:0x827aa00.Height Regular_Cell:0x827a400.Height == 56 + 1*0x82823f0.marker Regular_Cell:0x827a400.Width == 320 + 1*0x8281ee0.marker Regular_Cell:0x827a400.minX == 0 + 1*0x8281740.marker + -0.5*0x8281ee0.marker Regular_Cell:0x827a400.minY == 46 + 1*0x8281b30.marker + -0.5*0x82823f0.marker UILabel:0x827a8e0.Width == 0 + 1*0x827b730.marker + 1*0x827b790.marker + -1*0x827b7d0.marker + 1*UILabel:0x827adc0.Width UILabel:0x827a8e0.minX == 18 + 1*0x827b7d0.marker + 1*0x827b8d0.marker + -1*0x8281210.marker + 0.5*0x8281260.marker + 1*AsyncImageView:0x827aa00.Width UILabel:0x827a8e0.minY == 19.5 + -1*0x827b810.marker + 1*0x827b890.marker + -1*0x827b910.marker + -1*0x82812a0.marker + 0.5*0x82812e0.marker + 0.5*0x82823f0.marker + 0.5*AsyncImageView:0x827aa00.Height + -1*UILabel:0x827a8e0.Height + -1*UILabel:0x827b140.Height UILabel:0x827adc0.minX == 18 + 1*0x827b730.marker + 1*0x827b8d0.marker + -1*0x8281210.marker + 0.5*0x8281260.marker + 1*AsyncImageView:0x827aa00.Width UILabel:0x827adc0.minY == 20.5 + 1*0x827b6f0.marker + -1*0x82812a0.marker + 0.5*0x82812e0.marker UILabel:0x827b140.minX == 18 + 1*0x827b850.marker + 1*0x827b8d0.marker + -1*0x8281210.marker + 0.5*0x8281260.marker + 1*AsyncImageView:0x827aa00.Width UILabel:0x827b140.minY == 27.5 + 1*0x827b890.marker + -1*0x827b910.marker + -1*0x82812a0.marker + 0.5*0x82812e0.marker + 0.5*0x82823f0.marker + 0.5*AsyncImageView:0x827aa00.Height + -1*UILabel:0x827b140.Height UITableViewCellContentView:0x827a560.Height == 55 + 1*0x82812e0.marker UITableViewCellContentView:0x827a560.Width == 300 + 1*0x8281260.marker UITableViewCellContentView:0xlibc++abi.dylib: terminate called throwing an exception827a560.minX == 0 + 1*0x8281210.marker + -0.5*0x8281260.marker UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker objective == <> + <750:-1>*0x8280fc0.negError + <250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker

Constraints: Marker:0x8281210.marker Marker:0x8281260.marker Marker:0x82812a0.marker (Integralization adjustment:0.5) Marker:0x82812e0.marker Marker:0x8281740.marker Marker:0x8281b30.marker Marker:0x8280fc0.posErrorMarker Marker:0x8281030.posErrorMarker Marker:0x827b6f0.marker Marker:0x827b730.marker Marker:0x827b790.marker Marker:0x827b7d0.marker Marker:0x827b810.marker Marker:0x827b850.marker Marker:0x827b890.marker Marker:0x827b8d0.marker Marker:0x827b910.marker Marker:0x8281ee0.marker Marker:0x82823f0.marker }: internal error. Cannot find an outgoing row head for incoming head 0x8280fc0.negError, which should never happen.' * First throw call stack: (0x195d012 0x166ae7e 0x195cdeb 0xefef89 0xf01fcf 0xf020d3 0x7d86dc 0x7d9280 0x7dd4a3 0x3f7e3c 0x3f8022 0x3f8064 0x2f33b 0x277553f 0x2787014 0x27782e8 0x2778450 0x92710e72 0x926f8d2a) (lldb)

can you please tell me why this could be happening? i dont want to keep the NSLog() there because it runs it several times and gets in the way when trying to read output. Thank you in advance =)

Was it helpful?

Solution

It is undefined behavior to set the text property of a label in a background thread, or any other UI changes for that matter. Since it is undefined behavior I can not explain why it works with the NSLog but you need to dispatch setting the label's text to the main thread.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    user_web_communicator *usrWeb = [[user_web_communicator alloc]init];
    NSString *author = [usrWeb getUsernameFromID:author_string];

    dispatch_sync(dispatch_get_main_queue(), ^{
        [_author_label setText:[NSString stringWithFormat:@"Author: %@",author]];
    })
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top