ios - 在使用了Operation的时候,NSNotificationCenter收不到消息
PHP中文网
PHP中文网 2017-04-17 11:35:07
[iOS讨论组]

我首先自己定义了AFImageRequestOperation,然后把他加到NSOperationQueue,我是用一个循环加的,加了大概500个左右,如下:

AFImageRequestOperation * _128RequestOperation = [[AFImageRequestOperation alloc] initWithRequest:[download fetchUrlRequest:[download fetch128ImageUrl:tmpWheel]]];
                         
                         __weak AFImageRequestOperation * _128weakOperaion = _128RequestOperation;
                         
                         [_128RequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                             //NSLog(@"in _128 Operation ");
                             if ([_128weakOperaion isCancelled])
                                 return;
                             [download downloadImgAndSave2Disk:responseObject withImgName:[NSString stringWithFormat:@"%@@2x",thumbName] withFileType:@"png"];
                             [self.statusToolbar updateProgressBar:1 withTotalValue:totalProgress];
                         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             ;
                         }];
[self.downloadQueue addOperation:_128RequestOperation];

我在viewDidLoad中加入了,如下:NSNotificationCenter

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appDidEnterBackgroundNotification)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];

然后appDidEnterBackgroundNotification如下:

NSLog(@"hello i am in appDidEnterBackgroundNotification ...");
[self.downloadQueue cancelAllOperations];

当我按我自己itouch的home键的时候,如果我启动了addOperation,则我的那句nslog日志打不出来。operation也停步了。
如果我没有启动过addOperation,则我可以打印nslog的那句日志。
并且非常奇怪的是,我在6.1模拟器中,不管怎么操作都是能停的。
请问是什么原因?急!谢谢

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
PHP中文网

是这样的,我原本在AFImageRequestOperation的setCompletionBlockWithSuccess做了很多其他的事情,我现在把这些事情,都封装成为一个NSOpeartion,然后加入到QUEUE中,这样,按Home键后,就能打印出log了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号