退出视图控制器后不要调用回调块



当我构建应用程序时,我创建了一个名为NetEngine的类,以管理所有网络请求。但是我发现,退出使用NetEngine的视图控制器后,我仍然称为成功或故障块。

ViewController:

    [[NetEngine engine] GET:httpUrl success:^(id responseObject) {
         //some code here
         //It's still called after I quit viewctroller
    } failure:^(NSError *error) {
         //some code here
         //It's still called after I quit viewctroller
    }];

Netengine:

typedef void(^SuccessBlock) (id responseObject);
typedef void(^FailureBlock) (NSError *error);
@interface NetEngine ()
@property (nonatomic, strong) AFHTTPSessionManager *httpManager;
@end
@implementation NetEngine
+ (NetEngine *)engine {
    static NetEngine *_sharedEngengine = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedEngengine = [[self alloc] init];
    });
    return _sharedEngengine;
}
- (id)init {
    if (self = [super init]) {
        _httpManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BZTVBaseServerPath]];
        [_httpManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"accept"];
        _httpManager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        _httpManager.operationQueue.maxConcurrentOperationCount = 4;
        [_httpManager.requestSerializer setTimeoutInterval:10.0];
    }
    return self;
}
- (void)GET:(NSString *)URLString success:(SuccessBlock)successBlock failure:(FailureBlock)failureBlock {
    // some thing about cache
    [_httpManager GET:encodeUrl parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        [[self defaultCache] setObject:responseObject forKey:cacheKey];
        if (successBlock) {
            successBlock(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (isCache) {            
            if (successBlock) {
                successBlock([[self defaultCache] objectForKey:cacheKey]);
            }
        }
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

从文档中:"块是添加到C,Objective-C和C 的语言级功能,它使您可以创建可以将可以传递到方法或方法或起作用,好像它们是价值"。因此,块本身是一个对象,这意味着它具有不依赖于创建它的对象的生命周期。

如果您不希望NetEngine使用给它给出的代码,则需要一种"退出"视图控制器时传递该信息的方法。

基本上:

[[NetEngine engine] GET:httpUrl success:^(id responseObject) {
    // if the view controller still wants me to do this {
       //some code here
    // }
} failure:^(NSError *error) {
    // if the view controller still wants me to do this {
       //some code here
    // }
}];

如果您要在退出视图控制器时取消网络调用,则是您必须做的:

创建cancel BOOL变量

添加viewWillDisappear方法:

-(void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    cancel = YES;
}

检查回调方法中是否是"是":

   [[NetEngine engine] GET:httpUrl success:^(id responseObject) {
         //some code here
         //It's still called after I quit viewctroller
         if (!cancel) {
            //do what you have to do
         }
    } failure:^(NSError *error) {
         //some code here
         //It's still called after I quit viewctroller
         if (!cancel) {
            //do what you have to do
         }
    }];

相关内容

  • 没有找到相关文章

最新更新