//
// Action.h
//
// Created by Yongho Ji on 10. 12. 7..
// Copyright 2010 Wecon Communications. All rights reserved.
//
#import <Foundation/Foundation.h>
//target/selector를 하나로 묶고 실행해주는 클래스
@interface Action : NSObject
{
@private
id _target; //selector를 가지고 있는 클래스 객체 참조
SEL _selector; //target안에 정의된 selector 메서드
}
//초기화
-(id)initWithTarget:(id)target selector:(SEL)selector;
//target/selector를 실행
-(void)run;
//target/selector를 실행하되 selector인자로 data를 넘김
-(void)runWithData:(id)data;
//(Thread안에서 호출)target/selector를 실행.
-(void)runInThreadWithWaitUntilDone:(BOOL)waitUntilDone;
//(Thread안에서 호출)target/selector를 실행하되 selector인자로 data를 넘김
-(void)runInThreadWithData:(id)data waitUntilDone:(BOOL)waitUntilDone;
@end
//
// Action.m
//
// Created by Yongho Ji on 10. 12. 7..
// Copyright 2010 Wecon Communications. All rights reserved.
//
#import "Action.h"
@implementation Action
-(void)dealloc
{
[super dealloc];
//[_target release];
}
-(id)initWithTarget:(id)target selector:(SEL)selector
{
if (self = [super init])
{
//[target retain];
//[_target release];
_target = target; //retain하지 않고 assign만 한다. 왜냐하면 retain하면 메모리 누수를 야기시킬 수 있으므로
_selector = selector;
}
return self;
}
-(void)run
{
@try {
if (_target != nil && _selector != nil)
{
[_target performSelector:_selector];
}
}
@catch (NSException * e) {
NSLog(@"%@",e);
}
}
-(void)runWithData:(id)data
{
@try {
if (_target != nil && _selector != nil)
{
@try {
[_target performSelector:_selector withObject:data];
}
@catch (NSException * e) {
[_target performSelector:_selector];
}
}
}
@catch (NSException * e) {
NSLog(@"%@",e);
}
}
-(void)runInThreadWithWaitUntilDone:(BOOL)waitUntilDone
{
@try {
if (_target != nil && _selector != nil)
{
[_target performSelectorOnMainThread:_selector withObject:nil waitUntilDone:waitUntilDone];
}
}
@catch (NSException * e) {
NSLog(@"%@",e);
}
}
-(void)runInThreadWithData:(id)data waitUntilDone:(BOOL)waitUntilDone
{
@try {
if (_target != nil && _selector != nil)
{
@try {
[_target performSelectorOnMainThread:_selector withObject:data waitUntilDone:waitUntilDone];
}
@catch (NSException * e) {
[_target performSelectorOnMainThread:_selector withObject:nil waitUntilDone:waitUntilDone];
}
}
}
@catch (NSException * e) {
NSLog(@"%@",e);
}
}
@end
'소프트웨어 개발 > iOS' 카테고리의 다른 글
[iOS] Local Notification(지역알림, 내부통지). APNS와 비교 (27) | 2011.03.22 |
---|---|
[iOS 개발팁] 아이폰 어플에서 AppStore 검색페이지로 이동하기 (1) | 2011.02.27 |
아이폰,아이팟터치,아이패드 장비 종류 및 세대 알아오기 (0) | 2011.01.04 |
[iOS 어플]오히려 retain을 하면 안되는 경우 (메모리 릭 문제 막자!) (3) | 2010.12.30 |
[아이폰어플] Reachability로 네트워크 상태 감지 (1) | 2010.12.18 |