1

I am getting weird crash in the my application.

Mainviewcontroller.m : there is one method which call the webservice method (which is in another delegate class) and then when the response is receive the method is getting call back.

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

When i execute I am getting error when below code get called in getresponse method:

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

Detail of the error are :

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 )

Please help and advice the standard way to call the API and do the callback and how to solve this error.

P.s : I dont want to use AFNetowrking

user4261201
  • 2,324
  • 19
  • 26
iOS developer
  • 131
  • 1
  • 1
  • 9
  • In `-(void)getresponse:(NSMutableDictionary *)response_dictionary`, check if you are in main thread. If not, it's the issue. You can change UI only in main thread. Use GCD (plenty of example) on how to do it in main thread. – Larme May 02 '16 at 11:59
  • http://stackoverflow.com/questions/28302019/getting-a-this-application-is-modifying-the-autolayout-engine-error – Bhavin Bhadani May 02 '16 at 11:59
  • @Larme thank you for the reply. But how can i navigate to main thread from the background thread ? – iOS developer May 02 '16 at 12:00

1 Answers1

1

Your crash log says that,

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.

So you should perform this task(modifying auto layout engine) on main thread like,

   dispatch_async(dispatch_get_main_queue(), ^{

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

});

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75