Search code examples
iosautolayoutpure-layout

PureLayout is not threadsafe


I subclass UITableViewCell and use PureLayout to apply constraints but the app terminates with the error "PureLayout is not thread safe, and must be used exclusively from the main thread".

In the function...

 initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 

I have just applied one constraint

[self.label autoSetDimension:ALDimensionHeight toSize:50];

When this is removed, the app doesn't crash

update--- it's probably because I'm calling an API asynchronously


Solution

  • Wrap your init call in a dispatch_async to the main thread then...

    Without seeing the rest of your code.

    dispatch_async(dispatch_get_main_queue(), ^{
    
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Something"];
    
        });
    

    But if you are needing to do this I suspect you are going about things the wrong way. What you should be doing is updating the data with the result from your async call and calling reloadData on the tableview.

    Something like...

    [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];
    
            });
    
        }];