Search code examples
objective-cmfmailcomposeviewcontrollerdelegationretain-cycle

Am I creating a retain cycle between UIViewController and a custom object?


In a project using ARC, I have a UIViewController that is handling too many concerns, so I'm looking to split things up. One obvious thing for me to take out is a method that formats and sends an email, and split that into a separate object.

My controller currently handles the MFMailComposeViewControllerDelegate protocol because, after an email is successfully sent, I have some additional work to do.

My question is, if the controller uses as separate object for emailing, and I give it a pointer back to the controller for use as the MFMailComposeViewController mailComposeDelegate, am I going to create a problem -- specifically retain cycle?

If so, what would be a better approach in this scenario? Would it be enough to set the property pointing back to the controller as weak?


Solution

  • As you suggested in your question, to avoid retain cycle you just need to make your helper object's mailComposeDelegate property a weak property.

    @interface MyViewController : UIViewConroller
    @property(nonatomic, strong) MyHelperEmailObjectClass *emailHelper;
    @end
    
    @interface MyHelperEmailObjectClass : NSObject    
    @property(nonatomic, weak) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;
    @end
    

    You would get a retain cycle if the objects had eachother assigned to strong properties.