1

出于某种原因,我有一个 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]];
});

错误

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。标记 + 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*0x82218 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。宽度 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 + *AsyncImageView:0x827aa00.Height + -1*UILabel:0x827a8e0.Height + -1*UILabel:0x827b140.Height UILabel:0x827adc0.minX == 18 + 1*0x827b730.marker + 1*0x827b8d0.marker + -1*0x8281210。标记 + 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 == + 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:0x8271a56 *0x8281210.marker + -0.5*0x8281260.marker UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker 目标 == <> + <750:-1>*0x8280fc0.negError + < 250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker标记 UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker 目标 == <> + <750:-1>*0x8280fc0.negError + <250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker标记 UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker 目标 == <> + <750:-1>*0x8280fc0.negError + <250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker

约束: Marker:0x8281210.marker Marker:0x8281260.marker Marker:0x82812a0.marker (积分调整:0.5) Marker:0x82812e0.marker Marker:0x8281740.marker Marker:0x8281b30.marker Marker:0x8280fc0.posErrorMarker.posErrorMarker2:0x8 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 }:内部错误。找不到传入头 0x8280fc0.negError 的传出行头,这永远不会发生。 * 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 轮询我[4995:4f03] *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:'{ 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*0x82823 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。标记 + 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.高度 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. + 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*0x .marker + 1*AsyncImageView:0x827aa00.Width UILabel:0x827b140.minY == 27.5 + 1*0x827b890。标记 + -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 = 55 + 1*0x82812e0.marker UITableViewCellContentView:0x827a560.Width == 300 + 1*0x8281260.marker UITableViewCellContentView:0xlibc++abi.dylib: 终止调用抛出异常827a560.minX == 0 + 1*0x8281210.marker + -0.5 *0x8281260.marker UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker 目标 == <> + <750:-1>*0x8280fc0.negError + <250:-1>*0x8280fc0 .posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker高度 + -1*UILabel:0x827b140.Height UITableViewCellContentView:0x827a560.Height == 55 + 1*0x82812e0.marker UITableViewCellContentView:0x827a560.Width == 300 + 1*0x8281260.marker UITableViewCellContentView:0xlibc++abi.dylib:终止调用异常827a560.minX == 0 + 1*0x8281210.marker + -0.5*0x8281260.marker UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker 目标 == <> + <750: -1>*0x8280fc0.negError + <250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker高度 + -1*UILabel:0x827b140.Height UITableViewCellContentView:0x827a560.Height == 55 + 1*0x82812e0.marker UITableViewCellContentView:0x827a560.Width == 300 + 1*0x8281260.marker UITableViewCellContentView:0xlibc++abi.dylib:终止调用异常827a560.minX == 0 + 1*0x8281210.marker + -0.5*0x8281260.marker UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker 目标 == <> + <750: -1>*0x8280fc0.negError + <250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker终止称为抛出异常827a560.minX == 0 + 1*0x8281210.marker + -0.5*0x8281260.marker UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker 目标 == <> + <750:-1>*0x8280fc0.negError + <250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker终止称为抛出异常827a560.minX == 0 + 1*0x8281210.marker + -0.5*0x8281260.marker UITableViewCellContentView:0x827a560.minY == 0.5 + 1*0x82812a0.marker + -0.5*0x82812e0.marker 目标 == <> + <750:-1>*0x8280fc0.negError + <250:-1>*0x8280fc0.posErrorMarker + <750:-1>*0x8281030.negError + <250:-1>*0x8281030.posErrorMarker

约束: Marker:0x8281210.marker Marker:0x8281260.marker Marker:0x82812a0.marker (积分调整:0.5) Marker:0x82812e0.marker Marker:0x8281740.marker Marker:0x8281b30.marker Marker:0x8280fc0.posErrorMarker.posErrorMarker2:0x8 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 }:内部错误。找不到传入头 0x8280fc0.negError 的传出行头,这永远不会发生。 * First throw call stack: (0x195d012 0x166ae7e 0x195cdeb 0xefef89 0xf01fcf 0xf020d3 0x7d86dc 0x7d9280 0x7dd4a3 0x3f7e3c 0x3f8022 0x3f8064 0x2f33b 0x277553f 0x2787014 0x27782e8 0x2778450 0x92710e72 0x926f8d2a) (lldb)

你能告诉我为什么会这样吗?我不想将 NSLog() 保留在那里,因为它会多次运行它并且在尝试读取输出时会妨碍它。提前谢谢你=)

4

1 回答 1

6

在后台线程中设置标签的文本属性或任何其他 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]];
    })
});
于 2013-08-19T17:56:07.290 回答