ios - 此应用程序正在从 objective-c 中的后台线程修改自动布局引擎

标签 ios objective-c delegates nsurlsession

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

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

-(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];
}

委托(delegate)类: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];

错误的详细信息是:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release. Stack:( 0 CoreFoundation 0x00000001127ece65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000112263deb objc_exception_throw + 48 2 CoreFoundation 0x00000001127ecd9d +[NSException raise:format:] + 205 3 Foundation 0x0000000110504285 _AssertAutolayoutOnMainThreadOnly + 79 4 Foundation 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(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 272 14 UIKit 0x000000011098259e -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159 15 UIKit 0x00000001109924cc -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 744 16 QuartzCore 0x000000010fc3259a -[CALayer layoutSublayers] + 146 17 QuartzCore 0x000000010fc26e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 18 QuartzCore 0x000000010fc26cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 19 QuartzCore 0x000000010fc1b475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 20 QuartzCore 0x000000010fc48c0a _ZN2CA11Transaction6commitEv + 486 21 QuartzCore 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

最佳答案

你的崩溃日志说,

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.

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

   dispatch_async(dispatch_get_main_queue(), ^{

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

});

希望这会有所帮助:)

关于ios - 此应用程序正在从 objective-c 中的后台线程修改自动布局引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981839/

相关文章:

ios - 使用 LLVM 4.1 编译 Xcode 4.5 中的错误 255

iphone - Autorelease 上的自定义 UITableViewCell 导致应用程序崩溃

ios - 从任意位置播放声音

ios - 在具有最大高度的动态高度的 UITableViewCell 中添加 UITextView

ios - 如何以毫米/厘米为单位显示两幅图像之间的距离

ios - 如何从后台ios打开应用程序

ios - -[UIView setHostedGraph :] unrecognized selector sent to instance

Cocoa - 如何在 Storyboard模式下将 View 的委托(delegate)连接到文件的所有者?

c# - 使用委托(delegate)代替接口(interface)

iphone - delegate 在 xcode ios 项目中到底做了什么?