iphone - NSOperationQueue、weakSelf 和 block 的问题

标签 iphone objective-c ios ipad

我有以下代码:

 [[AHPinterestAPIClient sharedClient] getPath:requestURLPath parameters:nil 
             success:^(AFHTTPRequestOperation *operation, id response) {


                 [weakSelf.backgroundQueue_ addOperationWithBlock:^{
                     [self doSomeHeavyComputation];                    


                     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                           [weakSelf.collectionView_ setContentOffset:CGPointMake(0, 0)];
                     [weakSelf.collectionView_ reloadData];
                     [weakSelf.progressHUD_ hide:YES];
                     [[NSNotificationCenter defaultCenter] performSelector:@selector(postNotificationName:object:) withObject:@"UIScrollViewDidStopScrolling" afterDelay:0.3];
                     [weakSelf.progressHUD_ hide:YES];


                      }];

                  }];

             }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [weakSelf.progressHUD_ hide:YES];
                [weakSelf.collectionView_.pullToRefreshView stopAnimating];
                 NSLog(@"Error fetching user data!");
                 NSLog(@"%@", error);
             }];

出于某种原因,这在 iOS 5 中运行得很好,但在 iOS 6 中却不行(它会崩溃)。现在我不会询问有关 iOS 6 的问题,因为它仍处于 NDA 状态。我想知道的是,上面的代码是否有错误?如果是的话我该如何修复它。

如果我将代码放在 mainQueue 之外的 block 内,那就没问题了。 我在这里想做的是仅在 [self doSomeHeavyComputation] 完成后才执行 NSOperationQueue mainQueue 。那么这是一个依赖项,我应该如何添加这个依赖项?

更新:

这是崩溃日志(如果有帮助的话):

enter image description here

最佳答案

建议在 block 中“展开”弱引用,因此请尝试以下操作:

__weak id weakSelf = self;
[client getPath:path parameters:nil success:^(id op, id response) {
    id strongSelf = weakSelf;
    if (strongSelf == nil) return;

    __weak id internalWeakSelf = strongSelf;
    [strongSelf.backgroundQueue_ addOperationWithBlock:^{
        id internalStrongSelf = internalWeakSelf;
        if (internalStrongSelf == nil) return;

        [internalStrongSelf doSomeHeavyComputation];                    

        __weak id internalInternalWeakSelf = internalStrongSelf;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            id internalInternalStrongSelf = internalInternalWeakSelf;
            if (internalInternalStrongSelf == nil) return;

            [internalInternalStrongSelf reloadCollectionView];
        }];
    }];
}
failure:^(id op, NSError *error) {
    id strongSelf = weakSelf;
    if (strongSelf == nil) return;

    [strongSelf stopProgress];
    NSLog(@"Error fetching user data: %@", error);
}];

关于iphone - NSOperationQueue、weakSelf 和 block 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12470820/

相关文章:

iphone - 如何显示被虚拟键盘隐藏的文本字段?

iphone - 使用 GPUImageRGBFilter

iphone - 在 iPhone 上滚动时重复图像

ios - XCode 8 不正确的背景颜色 - Storyboard 更新的颜色空间

ios - 如何将保存的相册中的图像添加到 Collection View 单元格中?

ios - 如何像原生应用一样扩展视频裁剪框?

iphone - 如何在 iOS 7 锁屏上设置当前播放持续时间和耗时?

iphone - 核心数据 : How to sort NSManagedObject by its primary key?

ios - 如何将出生日期 UIDatePicker 添加到 UITextField?

ios - 如何关闭 UIScrollView 的轻拂滚动功能