当我的ViewController导航的应用崩溃时,AFURLSessionManager崩溃了 [英] AFURLSessionManager when my ViewController was navigated app crashed

查看:144
本文介绍了当我的ViewController导航的应用崩溃时,AFURLSessionManager崩溃了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-C iOS的AFURLSessionManager中遇到问题。我有2个viewController,并使用AFURLSessionManager从服务器获取数据。

I have problem in AFURLSessionManager for Objective-C iOS. I have 2 viewController and use AFURLSessionManager for getData from server.

当用户导航到具有AFURLSessionManager请求的viewContoller2且用户的网络非常差并按返回按钮时,AFURLSessionManager想要呼叫代表,当机并显示 EXC_BAD_ACCESS(code = EXC_I386_GPFLT)

when user navigate to viewContoller2 that have AFURLSessionManager request and user has very bad network and press back button, AFURLSessionManager want to call delegate, crashed and show EXC_BAD_ACCESS(code=EXC_I386_GPFLT)

当我导航到viewController时,AFURLSessionManager任务延迟完成,并且用户更改了ViewController且AFURLSessionManager尝试在内存中不存在调用委托。

when i navigate to a viewController, AFURLSessionManager task finish late and viewController was changed by user and AFURLSessionManager try call delegates not exists in MEMORY.

有人有解决这个问题的想法吗?

Anyone has any idea fix this problem?

这是请求代码:

- (void) getData
{
    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"GET" URLString:@"http://----------" parameters:nil error:nil];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    req.timeoutInterval= 15;
    [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    self.dataTask = [manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error)
     {

         if (!error)
         {
             NSLog(@"Reply JSON >>>>>> responseObject <<<<<: %@", responseObject);


             if ([responseObject isKindOfClass:[NSDictionary class]])
             {
                 if (self.delegate && [self.delegate respondsToSelector:@selector(success:)]) {
                     [self.delegate success:responseObject];
                 }
                 //                  self.colorView.backgroundColor = [UIColor blueColor];
                 return;
             }
             else
             {
                 NSLog(@"Error No dictionary: %@, %@, %@", error, response, responseObject);
                 {
                     //                      self.colorView.backgroundColor = [UIColor redColor];
                     [self.delegate error:error];
                     return;
                 }

             }
         }
         else
         {
             NSLog(@"Error: %@, %@, %@", error, response, responseObject);
             [self.delegate error:error];
             return;

         }

     }];

    [self.dataTask resume];

    if (self.cancelRequest) {
        [self.dataTask cancel];
    }
}

头文件:

@protocol WebServicesDelegate <NSObject>

- (void) success:(NSDictionary *) dic;
- (void) error:(NSError *) error;

@end

@interface WebServices : NSObject

@property (assign,nonatomic) id<WebServicesDelegate> delegate;
@property (nonatomic) BOOL cancelRequest;

-(void) setCancelRequest:(BOOL)cancelRequest;
//+(instancetype) shareInstance;

- (void) getData;

@end

预先感谢

推荐答案

调试self.delegate是什么。绝对不是零,否则它不会崩溃。

Debug what self.delegate is. It is definitely not nil otherwise it wouldn't have crashed.

它崩溃了,因为self.delegate不是NSObject子类。它可以是标量或某些垃圾值。

It crashed because self.delegate is not an NSObject subclass. It is either a scalar or some garbage value.

我想您使用的是分配而不是

I guess you have used assign instead of weak while declaring delegate and because it is assigned, it is resulting in a dangling pointer.

EDIT (在问题更新后)
更改时,声明委托并由于已分配它而导致指针悬空。

EDIT(after question update) Change

@property (assign,nonatomic) id<WebServicesDelegate> delegate;

@property (weak,nonatomic) id<WebServicesDelegate> delegate;

这篇关于当我的ViewController导航的应用崩溃时,AFURLSessionManager崩溃了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆