Pergunta

Imagine a situation like this:

- (void)doSomethingWithView:(UIView)view {
   for (UIView *oneView in view.subviews) {
      [self doSomethingWithView:oneView];
   }
}

or a block like

  void (^doSomething)(NSArray *numbers);

  doSomething = ^void(NSArray *numbers){

    // ... bla bla
    if (condition) {
      doSomething(numbers);
    }

  };

What happens in terms of stack and heap? My feeling is that the blocks/functions may generate a lot of stuff on the stack and heap that will never released up to a point the the app will crash without memory.

Do I run this risk?

Foi útil?

Solução

All depend from your code and quantity of recursion. But in any case, you can do something like this:

void (^doSomething)(NSArray *numbers);

  doSomething = ^void(NSArray *numbers){

    // ... bla bla
    if (condition) {
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ull), ^{
          doSomething(numbers);
      });
    }
};

in this case you launch again the block but due the fact that you are using dispatch_async, the outer block return and the new block continue separately in the concurrent queue.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top