简体   繁体   中英

issue with NSOperationQueue, weakSelf and blocks

I have the following code:

 [[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);
             }];

For some reason this worked just fine in iOS 5, but not iOS 6 (it crashes). Now I am not going to ask about iOS 6 because it's still under NDA. What I want to know is, whether the code above is wrong? If yes how do I fix it.

If I put the code inside the block outside of the mainQueue then it's fine. What I am trying to do here is to do the NSOperationQueue mainQueue only after the [self doSomeHeavyComputation] is done. So this is a dependency, how should I add this dependency?

Update:

Here's the crash log if it helps:

在此处输入图片说明

It is recommended to “unwind” weak references in the block, so please try this:

__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);
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM