由于某种原因,我有一个dispatch_async线程,并且它崩溃,除非我在它前面执行了NSLog()方法。该块运行一个从数据库中检索用户名的方法。
碰撞:
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]];
});
工作:
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]];
});
错误
你可以告诉我为什么会这样吗?我不想保留NSLog(),因为它运行了几次并在尝试读取输出时妨碍了。提前谢谢=)2013-08-19 13:56:06.149 Poll Me[4995:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '{
行: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
约束:标记:0x8281210.marker 标记:0x8281260.marker 标记:0x82812a0.marker(积分调整:0.5)标记:0x82812e0.marker 标记:0x8281740.marker 标记:0x8281b30.marker 标记:0x8280fc0.posErrorMarker 标记:0x8281030.posErrorMarker 标记:0x827b6f0.marker 标记:0x827b730.marker 标记:0x827b790.marker 标记:0x827b7d0.marker 标记:0x827b810.marker 标记:0x827b850.marker 标记:0x827b890.marker 标记:0x827b8d0.marker 标记:0x827b910.marker 标记:0x8281ee0.marker 标记:0x82823f0.marker}:内部 错误。找不到传入头的传出行头 0x8280fc0.negError,绝不应该发生。' *第一次抛出调用堆栈:(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民意调查[4995:4f03] * 由于未被捕获而终止应用 例外' NSInternalInconsistencyException',原因:' {行: 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调用抛出异常827a560.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
约束:标记:0x8281210.marker 标记:0x8281260.marker 标记:0x82812a0.marker(积分调整:0.5)标记:0x82812e0.marker 标记:0x8281740.marker标记:0x8281b30.marker 标记:0x8280fc0.posErrorMarker 标记:0x8281030.posErrorMarker 标记:0x827b6f0.marker 标记:0x827b730.marker 标记:0x827b790.marker 标记:0x827b7d0.marker 标记:0x827b810.marker 标记:0x827b850.marker 标记:0x827b890.marker 标记:0x827b8d0.marker 标记:0x827b910.marker 标记:0x8281ee0.marker 标记:0x82823f0.marker}:内部 错误。找不到传入头的传出行头 0x8280fc0.negError,绝不应该发生。' * 第一次抛出调用堆栈:(0x195d012 0x166ae7e 0x195cdeb 0xefef89 0xf01fcf 0xf020d3 0x7d86dc 0x7d9280 0x7dd4a3 0x3f7e3c 0x3f8022 0x3f8064 0x2f33b 0x277553f 0x2787014 0x27782e8 0x2778450 0x92710e72 0x926f8d2a)(lldb)
答案 0 :(得分:6)
在后台线程中设置标签的text属性或此类任何其他UI更改是未定义的行为。由于它是未定义的行为,我无法解释为什么它适用于NSLog
,但您需要将标签的文本设置为主线程。
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]];
})
});