NSInvocation에서 getReturn Value를 사용할 때 고려할 사항

9547 단어 iOS

개시하다


이 항목은 14번째 ydev에서도 말한 내용이다.
슬라이드 여기 있습니다. 하지만 아마 중요한 일은 입으로 말했기 때문에 참고 가치가 없어요(땀
너무 길게 써도 그렇다. 결론부터 말하자.
NSInvocation의 getReturn Value를 사용할 때는 Retain Count를 직접 관리해야 합니다!!

샘플 코드


Giithub에 샘플 코드를 넣었어요.
이 글은 샘플 코드를 바탕으로 쓴 설명입니다. 이쪽 코드를 다운로드하세요.

NSInvocation 정보


기본적인 사용 방법과 파라미터의 교부 방법에 관해서는 샘플과 이쪽 보도를 참고하십시오.
NSInvocation을 사용하여 학급 방법을 부르다

왜 NSInvocation입니까?


이번에 제가 NSInvocation을 사용한 것은 리더 코드를 읽고 영향을 받았기 때문입니다. 코드의 군더더기를 최대한 배제하고 좋은 코드를 쓰려고 합니다!그런 생각이 들어서요.
그렇긴 하지만 NSInvocation이 필요한 장면 자체가 많지 않으니 그 전에 디자인을 고려해 보는 것도 중요하다.

NSInvocation 을 사용하기 전에


말이 주제에서 벗어났다. NS Invocation의 get Return Value에 관해서는.
먼저 샘플 S의 mpleChapter ViewController입니다.m, SimpleDAO.m 보세요.
Chapter<->Section<->Paragraph는 구조가 있는 데이터베이스에서 데이터를 끌어내서 표에 그리는 견본입니다.
하지만 SimpleDAO는m을 보면 알 수 있듯이 상당히 지루하고 어떤 데이터를 얻으려는 SQL 부분은 눈에 띄지 않는 기술이다.

NSInvocation 사용 후


InvokedDAO.m 보세요. 상당히 유창해요.
다만, In voked Chapter ViewController.m이 표시되면 떨어집니다.
NSInvocation의 반환 값으로 얻은 NSDictionary가 Release에 의해 검색되었기 때문에 Instruments에서 디스크를 조사했습니다.
InvokedDAO.m
            // 注:サンプルではここのフラグを false に変えると正常に動作するほうに分岐されます
            if (true) {
                NSDictionary *dataDictionary;

                // invocate method by NSInvocation.
                SEL selector = NSSelectorFromString([NSString stringWithFormat:@"%@:", factoryMethod]);
                NSMethodSignature* signature = [[self class] methodSignatureForSelector:selector];
                NSInvocation* invocation = [ NSInvocation invocationWithMethodSignature:signature];
                [invocation setSelector:selector];
                [invocation setTarget:[self class]];
                [invocation setArgument:&statement atIndex:2];
                [invocation invoke];
                [invocation getReturnValue:&dataDictionary];

                [result addObject:[NSDictionary dictionaryWithDictionary:dataDictionary]];
            }

애플의 공식 문서를 자세히 보면 리테일 같은 건 없겠지?위에 쓰여 있다.
(이번 일은arguments가 아니라return Value인 것 같다.)
This class does not retain the arguments for the contained invocation by default. If those objects might disappear between the time you create your instance of NSInvocation and the time you use it, you should explicitly retain the objects yourself or invoke the retainArguments method to have the invocation object retain them itself.
retain Arguments에 관해서는 언급되었지만, 이번에는 반환값만 Retain에 의해 되돌아오기만 하면 되기 때문에 CFRetain을 사용한 것처럼 기술한 부분이 잘 진행되었다.
참고 문헌: StackOverFlow
InvokedDAO.m
                CFTypeRef data;

                // invocate method by NSInvocation.
                SEL selector = NSSelectorFromString([NSString stringWithFormat:@"%@:", factoryMethod]);
                NSMethodSignature* signature = [[self class] methodSignatureForSelector:selector];
                NSInvocation* invocation = [ NSInvocation invocationWithMethodSignature:signature];
                [invocation setSelector:selector];
                [invocation setTarget:[self class]];
                [invocation setArgument:&statement atIndex:2];
                [invocation invoke];
                [invocation getReturnValue:&data];

                // When out of this method scope, then released data. So, use CFRetain and go well.
                // http://stackoverflow.com/questions/7078109/why-does-nsinvocation-getreturnvalue-loose-object/11569236#11569236
                if (data) CFRetain(data);
                NSDictionary *dataDictionary = (__bridge_transfer NSDictionary *)data;

                [result addObject:[NSDictionary dictionaryWithDictionary:dataDictionary]];
Instruments를 실행해 봐도 화면 이동 실례가 확보되면서 화면이 사라지면 메모리가 방출된다는 것을 똑똑히 알 수 있다.

끝말


애플의 공식 문서를 잘 읽어라!w
그리고리더십 코드!!

좋은 웹페이지 즐겨찾기