2014-01-17 1 views
4

나는 다음 한 코드 :__block 변수를 해제 할 위치는 어디입니까?

-(void) doSomething 
{ 
    __block NSMutableArray *objArray = [[NSMutableArray alloc] initWithCapacity:0]; 
     [self performOperationWithBlock:^(void) 
     { 
     //adding objects to objArray 
     . 
     . 
     //operation with objArray finished 

     // 1. should objArray be released here? 
     }]; 

     //2. should objArray be released here? 
} 

내가 objArray를 autorelease를해야 하는가?

+0

'__block'은 블록에 rray, 그래서 버전 2는 더 논리적이다. 나는 건설에'autorelease'를 사용할 것입니다. –

+2

블록의 배열에 개체를 추가하려는 경우에는 '__block'이 필요하지 않습니다. –

답변

5

: 그래서 하나의 옵션 왼쪽이 블록 (비동기 작업 기간 동안 만 의미가 있음)이므로 최종 릴리스에서는 사용한 후에 사용해야합니다. 또는 간단히 다음을 수행 할 수 있습니다.

 NSMutableArray *objArray = [NSMutableArray array]; 

그리고이 경우에는 해제 할 필요가 없습니다.

동기화 전화 인 경우 차단 후 release해야합니다.


참고 : 나는 당신이 블록이 시작되기 전에 생성 할 말이 의미 블록에 사용되기 전에 NSMutableArray을 채우기되어 가정입니다.

비동기 방식 :

-(void) doSomething 
{ 
    // Remove the `__block` qualifier, you want the block to `retain` it so it 
    // can live after the `doSomething` method is destroyed 
    NSMutableArray *objArray = // created with something useful 

    [self performOperationWithBlock:^(void) 
    { 
     // You do something with the objArray, like adding new stuff to it (you are modyfing it). 
     // Since you have the __block qualifier (in non-ARC it has a different meaning, than in ARC) 
     // Finally, you need to be a good citizen and release it. 
    }]; 

    // By the the time reaches this point, the block might haven been, or not executed (it's an async call). 
    // With this in mind, you cannot just release the array. So you release it inside the block 
    // when the work is done 
} 

동기화 접근는 :

이 블록 후, 즉시 결과를해야하고, 당신이 배열에 추가 작업을 할 때 의미가 있다고 가정 실행되었습니다 :

-(void) doSomething 
{ 
    // Keep `__block` keyword, you don't want the block to `retain` as you 
    // will release it after 
    __block NSMutableArray *objArray = // created with something useful 

    [self performOperationWithBlock:^(void) 
    { 
     // You do something with the objArray, like adding new stuff to it (you are modyfing it). 
    }]; 
    // Since it's a sync call, when you reach this point, the block has been executed and you are sure 
    // that at least you won't be doing anything else inside the block with Array, so it's safe to release it 

    // Do something else with the array 

    // Finally release it: 

    [objArray release]; 
} 
+0

이 배열을 블록 외부에 채워야한다고 가정하면 그림이 어떻게됩니까? –

+0

@devgr, 비동기입니까, 아니면 블록 호출을 동기화합니까? – Peres

+0

나는 Synch와 Asynch 관점 모두를 이해할 필요가있다. –

1

performOperationWithBlock: 메서드가 완료된 후에 제 의견으로는 (즉, 호출 스레드와 동일한 스레드에서 작동 함) 메서드를 제공해야합니다.

해당 방법이 비동기이면 블록 내에서 해제되어야합니다.

0

ARC를 사용하지 않는 경우 더 이상 필요하지 않은 경우 배열을 해제해야합니다. 추가 한 주석에 따르면 doSomething 메서드가 블록 외부의 배열에 아무런 영향을주지 않는다고 가정하면 1.이라는 표시가 있어야합니다.

0

옵션 2. [self performOperationWithBlock:...] 이후에 출시하십시오. 블록은 objArray를 그대로 유지하고 해제합니다. 내부 블록을 해제하는 것은 위험합니다. 블록을 두 번 수행하면 objArray가 두 번 릴리스되지만 한번 해제해야합니다. 당신이 후를 필요로하지 않습니다으로

[self performOperationWithBlock:^(void) 
    { 
    NSMutableArray *objArray = [[NSMutableArray alloc] initWithCapacity:0]; 

    //adding objects to objArray 
    . 
    . 
    //operation with objArray finished 

    // 1. should objArray be released here? 
    }]; 

: 그것은 비동기 호출이 있다면, 그것은 의미가 실제 블록 내부의 NSMutableArray을 만들 수 것 2.

+0

__block 변수를 유지하거나 복사하는지 혼란 스럽습니다. –

+0

@devgr, 모든 객체가 블록으로 유지되었습니다. 복사는 의미가 없으며 모든 수업이 사본을 지원하지는 않습니다. 나쁜 당신은 정답을 표시하지 않았습니다. –

+0

블록을 복사 할 때만 객체가 유지됩니다. –

관련 문제