PureLayout 不是线程安全的
PureLayout is not threadsafe
我将 UITableViewCell 子类化并使用 PureLayout 应用约束,但应用程序终止并出现错误 "PureLayout is not thread safe, and must be used exclusively from the main thread"。
函数中...
initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
我刚刚应用了一个约束
[self.label autoSetDimension:ALDimensionHeight toSize:50];
删除后,应用程序不会崩溃
更新---
可能是因为我正在异步调用 API
将您的 init 调用包装在 dispatch_async
到主线程中然后...
没有看到你的代码的其余部分。
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Something"];
});
但是,如果您需要这样做,我怀疑您的处理方式有误。您应该做的是使用异步调用的结果更新数据并在表视图上调用 reloadData
。
类似...
[SomeAPI loadSomeRemoteDataPleaseWithCompetion:^(NSArray *theNewData){
self.dataArray = theNewData;
//oh hai im a bad API and dont return in main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableview reloadData];
});
}];
不要尝试在主线程上更改非 运行 的 UI 内部函数。
您正在尝试更改 init 中的标签约束,这是您的问题:您不在主线程中。
要解决此问题,请在单元格的 awakeFromNib 函数中添加更改 UI 的行,而不是在 init 函数中。
错误:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
...
[self.label autoSetDimension:ALDimensionHeight toSize:50];
...
}
正确:
- (void)awakeFromNib {
[super awakeFromNib];
...
[self.label autoSetDimension:ALDimensionHeight toSize:50];
...
}
我将 UITableViewCell 子类化并使用 PureLayout 应用约束,但应用程序终止并出现错误 "PureLayout is not thread safe, and must be used exclusively from the main thread"。
函数中...
initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
我刚刚应用了一个约束
[self.label autoSetDimension:ALDimensionHeight toSize:50];
删除后,应用程序不会崩溃
更新--- 可能是因为我正在异步调用 API
将您的 init 调用包装在 dispatch_async
到主线程中然后...
没有看到你的代码的其余部分。
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Something"];
});
但是,如果您需要这样做,我怀疑您的处理方式有误。您应该做的是使用异步调用的结果更新数据并在表视图上调用 reloadData
。
类似...
[SomeAPI loadSomeRemoteDataPleaseWithCompetion:^(NSArray *theNewData){
self.dataArray = theNewData;
//oh hai im a bad API and dont return in main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableview reloadData];
});
}];
不要尝试在主线程上更改非 运行 的 UI 内部函数。
您正在尝试更改 init 中的标签约束,这是您的问题:您不在主线程中。
要解决此问题,请在单元格的 awakeFromNib 函数中添加更改 UI 的行,而不是在 init 函数中。
错误:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
...
[self.label autoSetDimension:ALDimensionHeight toSize:50];
...
}
正确:
- (void)awakeFromNib {
[super awakeFromNib];
...
[self.label autoSetDimension:ALDimensionHeight toSize:50];
...
}