Skip to content

Instantly share code, notes, and snippets.

@juliengrimault
Created May 14, 2012 08:48
Show Gist options
  • Select an option

  • Save juliengrimault/2692793 to your computer and use it in GitHub Desktop.

Select an option

Save juliengrimault/2692793 to your computer and use it in GitHub Desktop.

Revisions

  1. Julien Grimault revised this gist May 14, 2012. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -10,12 +10,20 @@ @interface MyViewController : UITableViewController
    @end

    @implementation MyViewController
    @synthesize operation = operation;
    - (void)setOperation:(MKNetworkOperation*)operation
    {
    if (_operation == operation) return;

    [_operation cancel];
    _operation = operation;
    }

    - (void)viewDidAppear:(BOOL)animated
    {
    self.operation = [[Engine sharedEngine] someOperation:^(NSArray* images)
    {
    self.flickrImages = images; //retain cycle here
    self.flickrImages = images;
    [self.tableView reloadData];//retain cycle here
    }

    @@ -25,6 +33,17 @@ - (void)viewDidAppear:(BOOL)animated

    @end

    My VC has a strong reference to the operation that in turn copies the completion block that retains `self`, this creates a retain cycle I believe.

    A way to break it would be set the operation to nil in `viewDidDisapear:`
    - (void)viewDidDisappear:(BOOL)animated
    {
    self.operation = nil;
    }


    or to create a weak reference of self

    - (void)viewDidAppear:(BOOL)animated
    {
    __weak MyViewController* weakSelf = self;
  2. Julien Grimault revised this gist May 14, 2012. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    @interface Engine
    + (id)sharedEngine;
    - (MKNetworkOperation*)someOperation:(CompletionBlock)block onError:(ErrorBlock)errorBlock;
    @@ -28,12 +27,11 @@ - (void)viewDidAppear:(BOOL)animated

    - (void)viewDidAppear:(BOOL)animated
    {
    __weak FlickrTableViewController weakSelf = self; //to prevent retain cycle
    self.operation = [ApplicationDelegate.flickrEngine imagesForTag:@"Singapore"
    onCompletion:^(NSMutableArray* images)
    __weak MyViewController* weakSelf = self;
    self.operation = [[Engine sharedEngine] someOperation:^(NSArray* images)
    {
    weakSelf.flickrImages = images;
    [weakSelf.tableView reloadData];
    [weakSelf.tableView reloadData];
    }

    onError:^(NSError* error) {
  3. Julien Grimault revised this gist May 14, 2012. 1 changed file with 16 additions and 2 deletions.
    18 changes: 16 additions & 2 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,20 @@

    @interface Engine
    + (id)sharedEngine;
    - (MKNetworkOperation*)someOperation:(CompletionBlock)block onError:(ErrorBlock)errorBlock;
    @end



    @interface MyViewController : UITableViewController
    @property (nonatomic, strong) MKNetworkOperation* operation;
    @end

    @implementation MyViewController

    - (void)viewDidAppear:(BOOL)animated
    {
    self.operation = [ApplicationDelegate.flickrEngine imagesForTag:@"Singapore"
    onCompletion:^(NSMutableArray* images)
    self.operation = [[Engine sharedEngine] someOperation:^(NSArray* images)
    {
    self.flickrImages = images; //retain cycle here
    [self.tableView reloadData];//retain cycle here
    @@ -11,6 +24,7 @@ - (void)viewDidAppear:(BOOL)animated
    }];
    }

    @end

    - (void)viewDidAppear:(BOOL)animated
    {
  4. Julien Grimault revised this gist May 14, 2012. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@

    - (void)viewDidAppear:(BOOL)animated
    {
    [ApplicationDelegate.flickrEngine imagesForTag:@"Singapore"
    self.operation = [ApplicationDelegate.flickrEngine imagesForTag:@"Singapore"
    onCompletion:^(NSMutableArray* images)
    {
    self.flickrImages = images; //retain cycle here
    @@ -16,7 +15,7 @@ - (void)viewDidAppear:(BOOL)animated
    - (void)viewDidAppear:(BOOL)animated
    {
    __weak FlickrTableViewController weakSelf = self; //to prevent retain cycle
    [ApplicationDelegate.flickrEngine imagesForTag:@"Singapore"
    self.operation = [ApplicationDelegate.flickrEngine imagesForTag:@"Singapore"
    onCompletion:^(NSMutableArray* images)
    {
    weakSelf.flickrImages = images;
  5. Julien Grimault created this gist May 14, 2012.
    28 changes: 28 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@

    - (void)viewDidAppear:(BOOL)animated
    {
    [ApplicationDelegate.flickrEngine imagesForTag:@"Singapore"
    onCompletion:^(NSMutableArray* images)
    {
    self.flickrImages = images; //retain cycle here
    [self.tableView reloadData];//retain cycle here
    }

    onError:^(NSError* error) {
    }];
    }


    - (void)viewDidAppear:(BOOL)animated
    {
    __weak FlickrTableViewController weakSelf = self; //to prevent retain cycle
    [ApplicationDelegate.flickrEngine imagesForTag:@"Singapore"
    onCompletion:^(NSMutableArray* images)
    {
    weakSelf.flickrImages = images;
    [weakSelf.tableView reloadData];
    }

    onError:^(NSError* error) {
    }];
    }