RACSignal * updateSignalμ΄ μΌλΆ λΉλκΈ° μμ
μ μΊ‘μννλ€κ³ κ°μ ν΄ λ³΄κ² μ΅λλ€. ꡬλ
μ΄ λ°μνλ©΄ λΉλκΈ° μμ
μ€ λͺ¨λ μ ꡬλ
μ΄ μ²« λ²μ§Έ ꡬλ
μμ λμΌν κ²°κ³Όλ₯Ό λ°κΈ°λ₯Ό μν©λλ€.
μ§κΈ λ΄ μ½λλ λ€μκ³Ό κ°μ΅λλ€.
- (RACSignal *)doWork
{
if (_sharedWorkSignal) return _sharedWorkSignal;
RACSignal *requestSignal = ...;
_sharedWorkSignal = [[[[requestSignal publish] autoconnect] doCompleted:^{
_sharedWorkSignal = nil;
}] doError:^(NSError *error) {
_sharedWorkSignal = nil;
}];
return _sharedWorkSignal;
}
μλνμ§λ§ μ΄λ»κ² 보μ΄λμ§ μ λ§ λ§μμ λ€μ§ μμ΅λλ€. μ¬κΈ°μ μ€μν κ²μ΄ λμΉκ³ μμ΅λκΉ?
-replay
λλ -replayLazily
λ₯Ό μ¬μ©νμ¬μ΄λ₯Ό λ¬μ± ν μ μμ΅λλ€.
νμ§λ§ μ¬μ ν λΉλκΈ° μμ μ΄ μνλλ λμ μ¬μ λ μ νΈλ₯Ό μ μ§νκΈ° μν΄μ΄ instnace λ³μλ₯Ό μ¬μ©ν΄μΌν©λκΉ?
κ·νμ μ§λ¬Έμ μ΄ν΄νλμ§ μ λͺ¨λ₯΄κ² μ΅λλ€. μ μ¬ν ν¨ν΄μ μ¬μ©νκΈ° λλ¬Έμ API νΈμΆ μ μ μΈμ¦ ν ν° μ
λ΄κ° μλͺ»λ μ€λͺ μ μ 곡νμ μλ μμ΅λλ€. 첫 λ²μ§Έ ꡬλ μΌλ‘ μΈν΄ λΉλκΈ° μμ μ΄ μ€νλλ λμ λ€λ₯Έ ꡬλ μμ κ²°κ³Όλ₯Ό 곡μ νκ³ μΆμ§λ§μ΄ μμ μ΄ μλ£λκ³ μ νΈκ° μλ£λλ©΄ μ ꡬλ μ΄μ΄ μμ μ λ€μ μμνκΈ°λ₯Ό μν©λλ€. λ΄ μμ΄ μ£μ‘ν©λλ€;)
μ΄ κ²½μ°, λΉμ μ΄ ν κ²λ³΄λ€ ν¨μ¬ λ λμ μ κ·Ό λ°©λ²μ΄ μλμ§ μ λͺ¨λ₯΄κ² μ΅λλ€. κ·Έλ¬λ λͺ κ°μ§ μ μν μ¬νμ΄ μμ΅λλ€.
_sharedWorkSignal
μ½κΈ° λ° μ°κΈ°λ μ€λ λλ‘λΆν° μμ νμ§ μμ΅λλ€. μ΄λ₯Ό μν΄ atomic
μμ±μ μ¬μ©ν΄μΌν©λλ€.-publish
λ° -autoconnect
λμ -replayLazily
μ (λ₯Ό) μ¬μ©νκ³ μΆμ κ²μ
λλ€. κ·Έλ μ§ μμΌλ©΄ μ ꡬλ
μκ° μ°κ²°λκΈ° μ μ μ μ‘ λ κ°μλ°μ§ λͺ»ν©λλ€.-doCompleted:
λ° -doError:
λμ -finally:
λ₯Ό μ¬μ©ν μ μμ΅λλ€.ν°! μ μ€ν΄ μ λ§ κ°μ¬ν©λλ€! -finally
μ΄λ»κ² λμΉ μ μμ΅λκΉ? κ·Έλ¦¬κ³ κ·νμ λ©λͺ¨μ κ°μ¬λ립λλ€! λ«μ.
: μ€νν΄ :
λ©μλ, μΈμ€ν΄μ€ λ³μ λλ μμ±μ μ¬μ©νμ§ μμΌλ €λ©΄ λ€μκ³Ό κ°μ΄ μλ ν μ μμ΅λλ€.
RACSignal *testSig = [[RACSignal return:[NSMutableArray arrayWithCapacity:1]] map:^id(NSMutableArray *array) {
if (array.count == 0) {
RACSignal *sig = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// Do stuff
[subscriber sendNext:something];
[subscriber sendCompleted];
return nil;
}] doCompleted:^{
[array removeAllObjects];
}];
[array addObject:sig.replayLast];
}
return array.lastObject;
}].replayLazily;
λ°°μ΄μ΄λ μ£Όμ λ₯Ό μ¬μ©νλ©΄ λ³μλ₯Ό μ μΈνκ±°λ μμ±μ μ¬μ©νμ§ μκ³ λ 무μΈκ°μ λν μ°Έμ‘°λ₯Ό μ μ§ν μ μμ΅λλ€.
μ΄ μ½λλ μ€λ λλ‘λΆν° μμ νμ§ μμ΅λλ€. λλλ‘ λ³μλ₯Ό μ¬μ©νλ κ²μ΄ κ°μ₯ μ’μ λλ΅μ λλ€.
μ’μ΅λλ€. κ°μ₯ μμ ν μ λ΅μ μμ μμ±μ "μ΅μ»€"λ‘ μ¬μ©νλ κ²μ λλ€. νΉν RAC λΈλ‘μμ μ§λ΄λ λͺ¨λ λ³μμ λν΄ κ·Έλ μ΅λλ€.
λλ νμν λ "λ€μ νΈλ¦¬κ±°"νμ§ μμ 곡μ μ νΈμ κ°μ μ’
λ₯μ λμΌν λ¬Έμ μ μ§λ©΄νκ³ μμ΅λλ€.
RACMultiConnection λ°©μμΌλ‘ κ°λ₯ν λ§μ μ루μ
μ μλνμ§λ§ νμ λμΌν λ¬Έμ μ μ§λ©΄ ν΄ μμ΅λλ€. μμ€ μ νΈλ₯Ό λ€μ νΈλ¦¬κ±° ν λλ§λ€ λ€μμΌλ‘ μ μ‘λλ λͺ¨λ μ΄μ ꡬλ
μμκ² μ μ‘λ©λλ€.
λ μμΈν :
1) λ€νΈμν¬ μμ²μ μ΄μ λ리μ ꡬνμ λννλ νΈλ¦¬ν λ©μλμ μν΄ μ΄λ£¨μ΄μ§λλ€.
RACSignal *performRequestSignal =
[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// Create data received signal
RACSignal *dataReceivedSignal = [[self rac_signalForSelector:@selector(dataReceived:forRequest:) fromProtocol:@protocol(MainDataControllerDelegate)] filter:^BOOL(RACTuple *returnTuple) {
DataRequest *request = returnTuple.second;
DataResponse *dataResponse = returnTuple.first;
return ((request.type == dataRequest.type) && ([dataResponse.value isKindOfClass:dataResponseClass]));
}];
// Subscribe
[dataReceivedSignal subscribeNext:^(RACTuple *returnTuple) {
DataResponse *dataResponse = returnTuple.first;
[subscriber sendNext:dataResponse.value];
[subscriber sendCompleted];
}];
// Create data request failed signal
RACSignal *requestFailedSignal = [self rac_signalForSelector:@selector(requestFailed:error:) fromProtocol:@protocol(MainDataControllerDelegate)];
// Subscribe
[requestFailedSignal subscribeNext:^(RACTuple *returnTuple) {
NSError *error = returnTuple.second;
[subscriber sendError:error];
}];
// Trigger the request to API
[[MainDataController sharedInstance] addRequest:dataRequest delegate:self priority:queuePriority];
return [RACDisposable disposableWithBlock:^{
[[MainDataController sharedInstance] cancelRequest:dataRequest];
}];
}];
return performRequestSignal;
2)μ΄ λ©μλλ μ΄μ λ©μλλ₯Ό μ¬μ©νμ¬ "retrieveThingsSignal"μ΄λΌκ³ λΆλ₯΄λ νΈμ μ νΈλ₯Ό μμ±ν©λλ€.
- (RACSignal *)retrieveThings
{
if (_retrieveDebtors) return _retrieveDebtors;
RACSignal *requestSignal = [self retrieveThingsSignal];
_retrieveDebtors = [[requestSignal replayLast] finally:^{
_retrieveThings = nil;
}];
return _retrieveThings;
3)μ΄ μ½λλ₯Ό μ€ννλ μ νΈλ₯Ό λ€μ νΈλ¦¬κ±°νμ§ μμ λλ§λ€
[[self retrieveThings] subscribeNext:^(NSArray *things) {
@strongify(self);
self.things = things;
} error:^(NSError *error) {
@strongify(self)
self.things = nil;
}];
self.things
λ retreiveThings
μ μμ²μν΄μΌ ν λλ§λ€ κ·Έμ λ°λΌ μ
λ°μ΄νΈλμ§λ§, 1) λ¨κ³μ μ€λ¨ μ μλλ©΄ next:
λ λͺ¨λ μ΄μ ꡬλ
μμκ² μ μ‘λ©λλ€. retrieveThings
λ λ² νΈμΆνλ©΄ ν΄λΉ μ€λ¨ μ μμ κΈ°λ³Έ μ νΈκ° next:
λ₯Ό λ λͺ
μ λ€λ₯Έ ꡬλ
μμκ² μ μ‘νλ€λ κ²μ μμ ν©λλ€ (νλλ μ€μ λ‘ subscribeNext:
λΈλ‘μ μ€ν ν¨). . κ°μ
μκ° 3 ~ 3 λͺ
μ΄λ©΄ ...
μ΄ λμμ λ°©μ§ ν μ μμ΅λκΉ?
λ§€λ² λ€νΈμν¬ μμ²μ νΈλ¦¬κ±°νμ§ μκ³ retrieveThings
λ©μλμμ λ§μ§λ§μΌλ‘ λ³΄λΈ κ°μ 곡μ νκ³ μΆμ§λ§ νμν κ²½μ° λ€μ ꡬλ
μ΄ μ€μ λ‘ λ€νΈμν¬ μμ²μ νΈλ¦¬κ±°νλλ‘ μ 리ν΄μΌν©λλ€.