【问题标题】:This application is modifying the autolayout engine from a background thread in objective c此应用程序正在从目标 c 中的后台线程修改自动布局引擎
【发布时间】:2016-05-02 22:08:24
【问题描述】:

我的应用程序出现奇怪的崩溃。

Mainviewcontroller.m : 有一个方法调用 webservice 方法(在另一个委托类中),然后当收到响应时,该方法得到回调。

-(IBAction)signupclick:(id)sender{
     webservice_obj=[[Webservice alloc] init];
    [webservice_obj setDelegate:self];
    [webservice_obj Init_withurl:@"register" withparameter:data];
}
-(void)getresponse:(NSMutableDictionary *)response_dictionary{
    NSLog(@"dictionary==>%@",response_dictionary);
    AppDelegate *appDelegate =(AppDelegate*)[[UIApplication sharedApplication]delegate];
    [appDelegate.window setRootViewController:appDelegate.frostedViewController];
}

委托类:Webservice.m

-(void)Init_withurl:(NSString *)name withparameter:(NSMutableDictionary *)reqparameter{

    __block NSMutableDictionary *res_dictionary;
    NSError *error;
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];




    NSURL *url = [NSURL URLWithString:@"http://XXXXXXX"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setHTTPMethod:@"PUT"];
     NSData *postData = [NSJSONSerialization dataWithJSONObject:reqparameter options:0 error:&error];

     [request setHTTPBody:postData];
      NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        res_dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];



          [self fetchresponse:res_dictionary];



    }];

    [postDataTask resume];

}

-(void)fetchresponse:(NSMutableDictionary*)response{
    [[self delegate] getresponse:response];
}

当我在 getresponse 方法中调用以下代码时出现错误:

 AppDelegate *appDelegate =(AppDelegate*)[[UIApplication sharedApplication]delegate];
    [appDelegate.window setRootViewController:appDelegate.frostedViewController];

错误的详细信息是:

此应用程序正在从后台修改自动布局引擎 线程,这可能导致引擎损坏和奇怪的崩溃。这个 将在未来的版本中导致异常。 堆:( 0 核心基础 0x00000001127ece65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000112263deb objc_exception_throw + 48 2 核心基础 0x00000001127ecd9d +[NSException raise:format:] + 205 3 基础 0x0000000110504285 _AssertAutolayoutOnMainThreadOnly + 79 4 基础 0x0000000110364c1e -[NSISEngine withBehaviors:performModifications:] + 31 5 UIKit 0x00000001111aa1ca-[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 58 6 UIKit 0x00000001111a9b66 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 646 7 UIKit 0x00000001111aa1d3-[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 67 8 UIKit 0x00000001111a989b-[UIView(AdditionalLayoutSupport)_switchToLayoutEngine:]+242 9 UIKit 0x000000011119ae6b-[UIWindow(UIConstraintBasedLayout)_switchToLayoutEngine:] + 81 10 UIKit 0x000000011119af99-[UIWindow(UIConstraintBasedLayout)_initializeLayoutEngine] + 282 11 UIKit 0x000000011119b022-[UIWindow(UIConstraintBasedLayout)_layoutEngineCreateIfNecessary] + 62 12 UIKit 0x000000011119b2e3 -[UIWindow(UIConstraintBasedLayout) updateConstraintsIfNeeded] + 60 13 UIKit 0x00000001111aba22-[UIView(附加布局支持)_updateConstraintsAtEngineLevelIfNeeded] + 272 14 UIKit 0x000000011098259e -[UIView(层次结构)_updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159 15 UIKit 0x00000001109924cc -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 744 16 QuartzCore 0x000000010fc3259a -[CALayer layoutSublayers] + 146 17 石英核心 0x000000010fc26e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 18 石英核心 0x000000010fc26cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 19 石英核心 0x000000010fc1b475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 20 石英核心 0x000000010fc48c0a _ZN2CA11Transaction6commitEv + 486 21 石英核心 0x000000010fc48efc _ZN2CA11Transaction14release_threadEPv + 224 22 libsystem_pthread.dylib 0x0000000112ff339c _pthread_tsd_cleanup + 470 23 libsystem_pthread.dylib 0x0000000112ff2f78 _pthread_exit + 117 24 libsystem_pthread.dylib 0x0000000112ff1596 pthread_attr_getschedpolicy + 0 25 libsystem_pthread.dylib 0x0000000112fef375 start_wqthread + 13 )

请帮助并建议调用 API 和执行回调的标准方法以及如何解决此错误。

P.s : 我不想使用 AFNetowrking

【问题讨论】:

  • -(void)getresponse:(NSMutableDictionary *)response_dictionary,检查你是否在主线程中。如果不是,那就是问题。您只能在主线程中更改 UI。使用 GCD(大量示例)了解如何在主线程中执行此操作。
  • @Larme 谢谢你的回复。但是如何从后台线程导航到主线程?

标签: ios objective-c delegates nsurlsession


【解决方案1】:

您的崩溃日志表明,

此应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。

所以你应该在main thread 上执行这个任务(修改自动布局引擎),

   dispatch_async(dispatch_get_main_queue(), ^{

    AppDelegate *appDelegate =(AppDelegate*)[[UIApplication sharedApplication]delegate];
    [appDelegate.window setRootViewController:appDelegate.frostedViewController];

});

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2018-01-23
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多