2016-05-02 21 views
1

我得到在我的应用程序崩溃怪异。此应用程序正在修改从后台线程的自动布局引擎的目标C

Mainviewcontroller.m:有一个方法,该方法调用的web服务方法(这是在另一个委托类),然后当该响应是接收该方法是越来越回叫。

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

详细的错误是:

此应用程序从后台线程 ,这可能会导致发动机损坏和怪异的崩溃修改自动布局引擎。这 将导致在未来的版本异常。 堆栈:( 0的CoreFoundation 0x00000001127ece65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000112263deb objc_exception_throw + 48 2的CoreFoundation 0x00000001127ecd9d + [NSException提高:格式:] + 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的(AdditionalLayout支持)_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的(层次结构)_updateCon straintsAsNecessaryAndApplyLayoutFromEngine] + 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 _pthre ad_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

+0

在' - (空)的GetResponse:(*的NSMutableDictionary) response_dictionary',检查你是否在主线程中。如果不是,这是问题。您只能在主线程中更改UI。使用GCD(大量示例)如何在主线程中执行此操作。 – Larme

+0

http://stackoverflow.com/questions/28302019/getting-a-this-application-is-modifying-the-autolayout-engine-error –

+0

@Larme谢谢你的回复。但我怎么能从后台线程导航到主线程? –

回答

1

你的崩溃日志说,

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

所以,你应该在main thread像执行此任务(修改自动排版引擎),

dispatch_async(dispatch_get_main_queue(), ^{ 

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

}); 

希望这将有助于:)

相关问题