iOS 5 Twitter Framework & completionHandler block

2020-06-12 04:48发布

I am very new to programming and Objective-C and I am trying to work out what is wrong with my code. I have read a bit about blocks but I don't know how any of what I have read so far is relevant to my code.

My code is using the iOS 5 Twitter Framework. I use most of the sample code that Apple provides so I actually had no clue at first that I was using a block for the completion handler.

Now I get those two messages from Xcode 4 saying "1. Block will be retained by an object strongly retained by the captured object" and "Capturing 'self' strongly in this block is likely to lead to a retain cycle".

Basically, what I did is to remove the code Apple used in their completion handler (switch statement with TWTweetComposeViewControllerResultCancelled & TWTweetComposeViewControllerResultDone) and used my if statements with [imagePickerController sourceType].

So the sendTweet gets called after an image has been added to the tweet.

I hope someone can explain to me why this is happening and how I can solve it. Also: can I put the completion handler code into a method instead of a block?

- (void)sendTweet:(UIImage *)image
{
    //adds photo to tweet
    [tweetViewController addImage:image];

    // Create the completion handler block.
    //Xcode: "1. Block will be retained by an object strongly retained by the captured object"
    [tweetViewController setCompletionHandler:
                             ^(TWTweetComposeViewControllerResult result) {
            NSString *alertTitle,
                     *alertMessage,
                     *otherAlertButtonTitle,
                     *alertCancelButtonTitle;

            if (result == TWTweetComposeViewControllerResultCancelled) 
            {
                //Xcode: "Capturing 'self' strongly in this block is likely to lead to a retain cycle"
                if ([imagePickerController sourceType])
                {
                    alertTitle = NSLocalizedString(@"TCA_TITLE", nil);
                    alertMessage = NSLocalizedString(@"TCA_MESSAGE", nil);
                    alertCancelButtonTitle = NSLocalizedString(@"NO", nil);
                    otherAlertButtonTitle = NSLocalizedString(@"YES", nil);

                    //user taps YES
                    UIAlertView *alert = [[UIAlertView alloc] 
                                             initWithTitle:alertTitle 
                                                   message:alertMessage 
                                                  delegate:self   // Note: self
                                         cancelButtonTitle:alertCancelButtonTitle 
                                         otherButtonTitles:otherAlertButtonTitle,nil];
                    alert.tag = 1;
                    [alert show];                
                }            
            }

4条回答
做个烂人
2楼-- · 2020-06-12 05:30

According to stuff I've seen elsewhere, “weak” won't work for ARC-compliant code, and that you must use “_unsafe_unretained” instead. This is what I did to fix the “Capturing 'self' strongly in this block is likely to lead to a retain cycle” warning in the Apple sample app "AVPlayerDemo":

__unsafe_unretained id unself = self;
mTimeObserver = [mPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(interval, NSEC_PER_SEC) 
                            queue:NULL /* If you pass NULL, the main queue is used. */
                            usingBlock:^(CMTime time) 
                                        {
                                            /* 'unself' replaced 'self' here: */
                                            [unself syncScrubber];
                                        }];
查看更多
Bombasti
3楼-- · 2020-06-12 05:31

There is a rather surprising way to get rid of the warning. Mind you, only do this if you are sure you are not creating a retain cycle, or you are sure that you will break it yourself later on (by setting the completion handler to nil when you're done, for example). If you have control over the interface, then rename your method so it does not start with "set". The compiler seems to give this warning only when the method name starts with "set".

查看更多
地球回转人心会变
4楼-- · 2020-06-12 05:35

Your block is retaining self because you're using self as the delegate of the UIAlertView. If self is also retaining the block, they're retaining each other, which creates a retain cycle.

Try

 __block WhateverTypeSelfIs *nonRetainedSelfForBlock = self;
[tweetViewController setCompletionHandler: 

and

UIAlertView *alert = [[UIAlertView alloc] 
                                 initWithTitle:alertTitle 
                                 message:alertMessage 
                                 delegate:nonRetainedSelfForBlock 
                                 cancelButtonTitle:alertCancelButtonTitle 
                                 otherButtonTitles:otherAlertButtonTitle,nil];

Check the Apple docs, section Object and Block Variables. Objects referenced within a block are retained, unless you use __block.

查看更多
爷的心禁止访问
5楼-- · 2020-06-12 05:52

The basic problem is that you're using self within a block. The block is being retained by the object and the block itself retains the object, too. So you have a retain-cycle and thus both will probably never be released because both have a reference pointing towards them. Fortunaly there is a simple workaround:

By using a so-called weak reference to self the block will no longer retain the object. The object then can later be released which will release the block (set MyClass to the appropriate type):

// before your block
__weak MyObject *weakSelf = self;

Inside your block you now can use weakSelf instead of self. Note that this is only for iOS 5 using ARC.

Looking at this there is also a very good long explanation on How do I avoid capturing self in blocks when implementing an API? which also describes how to avoid this on iOS 4 and without ARC.

查看更多
登录 后发表回答