我有一个从互联网下载东西的功能。然后它映射项目并将所有内容返回给控制器。问题是可能有很多项目(facebook 不允许划分它们),我必须在后台映射它们。但是用户已经可以退出控制器,我不知道如何检查我的success()
所有者是否仍然存在。
将映射完全放在调度中是否是一个好的解决方案......?
称呼:
[[MyHTTPClient sharedInstance] getSomethingWithAbc:abc successBlock:^{
[self reloadMyView];
} failureBlock:^(NSError *error) {
//nothing atm
}];
在哪里:
@interface MyHTTPClient : AFHTTPClient
+ (OHTTPClient *)sharedInstance;
- (void)getSomethingWithAbc:(ABC *)abc successBlock:(void (^)())success failureBlock:(void (^)(NSError *error))failure;
方法实现:
- (void)getSomethingWithAbc:(ABC *)abc successBlock:(void (^)())success failureBlock:(void (^)(NSError *error))failure {
// request something from facebook API, wait for response then run:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// here map these items - there may be a lot of them so i have to do it in background
dispatch_async(dispatch_get_main_queue(), ^{
success(); // this may call a function from controller that does not exist already
});
}