1

I have a number of methods in blocks, firing one inside the next, in order to sync some data with a web service. Most of these behave completely fine, but one method won't let me mention self after it's been called, giving me a capturing self strongly in this block is likely to lead to a retain cycle warning.

Here's what I mean:

[self deleteEntriesCorrespondingToDeletedNotesInNotebook:notebook success:^{
    [self deleteNotesToMatchDeletedEntriesWithCompletion:^{
       [self deleteResourcesToMatchDeletedMediaItemsWithCompletion:^{
           [self addOrUpdateEntriesCorrespondingToUpdatedNotesInNotebook:notebook success:^{
               //Anything calling a property or self after this point is a problem and gives the warning
               [self addOrUpdateNotesCorrespondingToUpdatedEntriesWithCompletion:^{

               }];
           }failure:^{

           }];
       }];
   }];
}failure:^{

}];

Any ideas why only items passed this point have a problem with this? If I replace the method before it with another similar method, there isn't a problem. The problem is only existent after addOrUpdateEntriesCorrespondingToUpdatedNotesInNotebook: is used.

Andrew
  • 15,935
  • 28
  • 121
  • 203
  • Could you also add the internal implementations of each method? – Stavash Jul 10 '13 at 09:43
  • Google "Objective-C blocks weak self", this is a dupe of lot of identical questions. –  Jul 10 '13 at 09:44
  • possible duplicate of [capturing self strongly in this block is likely to lead to a retain cycle](http://stackoverflow.com/questions/14556605/capturing-self-strongly-in-this-block-is-likely-to-lead-to-a-retain-cycle) –  Jul 10 '13 at 09:45
  • and of [How do I avoid capturing self in blocks when implementing an API?](http://stackoverflow.com/questions/7853915/how-do-i-avoid-capturing-self-in-blocks-when-implementing-an-api) –  Jul 10 '13 at 09:46
  • I know the ways to patch over the problem, using weak references, but I want to actually stop it from happening, since it doesn't happen with any other method. – Andrew Jul 10 '13 at 09:49
  • 2
    If I understand it correctly, the question is **not** why this code generates a retain cycle, but why does `addOrUpdateEntries...` cause that warning, but `deleteEntries...` causes no warning. - I tried to answer that here: http://stackoverflow.com/a/15536473/1187415. – Martin R Jul 10 '13 at 09:51
  • 1
    possible duplicate of [Clang - Blocks retain cycle from naming convention?](http://stackoverflow.com/questions/15535899/clang-blocks-retain-cycle-from-naming-convention) – rob mayoff Jul 10 '13 at 09:57

2 Answers2

3
[self deleteEntriesCorrespondingToDeletedNotesInNotebook:notebook success:^{
    [self deleteNotesToMatchDeletedEntriesWithCompletion:^{  //this line here and the rest in your downward loop

dont use self. Instead do this before the first line

__typeof__(self) __weak _weakSelf = self; and then from second line onwards, replace self with weakSelf

try this. cheers

devluv
  • 2,007
  • 13
  • 13
2

All of your methods could "behave fine" or create a retain cycle, depending on what they do with the completion block.

As explained here: Blocks retain cycle from naming convention?, the clang compiler uses naming conventions to decide whether to emit a warning or not: All methods add... and set... (but not addOperationWithBlock!) cause a warning, other methods don't.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382