2014-05-18 1 views
0

나는이 문제를 해결하기 위해 하루 종일 고생했습니다. 아이템의 NSMutableArray가 있습니다. 때로는 응용 프로그램 수명주기 동안 응용 프로그램이 거기에 객체를 밀고 팝합니다. 나는 보통 $ scope. $ watch와 함께 AngularJS 측에서 이것을 할 수 있습니다. 나는이 행동의 일부를하기 위해 그 동안 ReactiveCocoa를 사용 해왔다.

본질적으로 나는이 사건을 일으키는 블록을 원한다.

- (void) fetchAllItems; - (void) 푸시 : (id) 항목; - (void) pop : (NSUInteger) index;

ReactiveCocoa로 시도했지만 결코 실행되지 않습니다! 당신은 속성 setter를 구현하고 있기 때문에

[RACObserve(singletonStore, items) subscribeNext:^(NSArray *wholeArray) { 
     NSUInteger count = [wholeArray count]; 
     [self.offerCircleButton setTitle:[NSString stringWithFormat:@"%d", count]] 
}]; 

항목 속성이

@property (nonatomic, copy) NSArray *items; 

게터와 세터로 singletonStore 객체 선언은 전용 변수

@interface SHSingletonStore() 
{ 
    NSMutableArray *_items; 
} 
@end 

-(NSArray *)items { 
    return [_items copy]; 
} 

-(void)setItems:(NSArray *)items{ 
    if([_items isEqualToArray:items] == NO){ 
     _items = [items mutableCopy]; 
    } 
} 

답변

1

는 기본적으로 나는이 이벤트에 화재로 블록을 싶습니다.

- (void) fetchAllItems; - (void) push : (id) item; - (void) pop : (NSUInteger) index;

당신이 말하는 것은 당신이 그 방법 중 하나가 호출 될 때마다 값을 전송하는 신호를 원하는, 당신은 -rac_signalForSelector:에, 그래서 같은 것을 할 수있는 경우 :

SEL fetchAllSEL = @selector(fetchAllItems); 
RACSignal *fetchAllSignal = [singletonStore rac_signalForSelector:fetchAllSEL]; 

SEL pushSEL = @selector(push:); 
RACSignal *pushSignal = [singletonStore rac_signalForSelector:pushSEL]; 

SEL popSEL = @selector(pop:); 
RACSignal *popSignal = [singletonStore rac_signalForSelector:popSEL]; 

@weakify(self); 
[[RACSignal merge:@[ fetchAllSignal, pushSignal, popSignal ]] subscribeNext:^(id _) { 

    @strongify(self); 
    [self.offerCircleButton setTitle:[NSString stringWithFormat:@"%d", singletonStore.items.count]] 

}]; 

이 솔루션은 종류를 왜냐하면 각 병합 된 신호는 서로 다른 유형의 값을 전송하기 때문에 (보통 3 가지 다른 유형의 값을 전송할 수있는 신호로 끝나기는하지만 일반적으로 좋지는 않음), 값을 무시하면 일부 전역 상태를 기반으로 일부 부작용을 수행 할 수 있지만 실제로는 중요하지 않습니다.

+0

모든 종류의 배열 문제에 대해 선택기 수신기 블록이 내장되어 있다고 생각했지만 직접해야한다고 생각합니다! –

+0

당신의 질문은 ReactiveCocoa로 어레이 변경 사항을 관찰하는 것과 관련된 다른 질문과 비슷합니다. 도움이 필요할 경우를 대비해서 제가 지적했습니다 : http://stackoverflow.com/questions/25162893/racobserve-for-value -update-in-nsmutablearray – erikprice

0

같이있는 NSMutableArray입니다 수동으로 KVO 알림을 트리거해야합니다. 속성이 변경되었다는 알림을 보내야합니다. 자세한 내용은 여기를 참조하십시오. Apple Documentation

기본적으로 속성 구현을 제공하지 않으면 해당 알림이 자동으로 트리거되지만 사용자 지정 구현을 제공하는 경우 직접 알림을 보내야합니다.

- (void)setItems:(NSArray *)items { 
    if([_items isEqualToArray:items] == NO) { 
     [self willChangeValueForKey:@"items"]; 
     _items = [items mutableCopy]; 
     [self didChangeValueForKey:@"items"]; 
    } 
} 
관련 문제