return [ RACSignal return : @"hello111" ];
}];
RACSignal *signal2 = [ RACSignal defer :^ RACSignal *{
NSLog ( @"bbbb" );
return [ RACSignal return : @"hello222" ];
NSLog ( @"x = %@" ,x);
NSLog ( @"aaaa" );
return [ RACSignal return : @"hello111" ];
}];
RACSignal *signal2 = [ RACSignal createSignal :^ RACDisposable *( id < RACSubscriber > subscriber) {
NSLog ( @"bbbbb" );
[subscriber sendNext : @"asdfasdf" ];
[subscriber sendCompleted ];
return nil ;
}];
RACSignal *signal3 = [signal2 zipWith :[signal1 logAll ]];
[signal3 subscribeNext :^( id x) {
NSLog ( @"asdfasdf" );
} completed :^{
NSLog ( @"asdfasdfsdf" );
NSLog ( @"print signal1" );
return [ RACSignal return : @"signal1" ];
}];
RACSignal *signal2 = [ RACSignal defer :^ RACSignal *{
NSLog ( @"print signal2" );
return [ RACSignal return : @"signal2" ];
}];
RACSignal *signal3 = [signal1 concat :signal2];
[signal3 subscribeNext :^( id x) {
NSLog ( @"%@" ,x);
2015-06-03 13:23:07.385 DemoCategorizer[21572:2556481] signal1
2015-06-03 13:23:07.385 DemoCategorizer[21572:2556481] print signal2
NSLog ( @"secCodeimageFile == %@" ,value);
return [[ UIImage alloc ] initWithContentsOfFile :value];
[subscriber sendError :[ NSError errorWithDomain : @"my fault" code : 100 userInfo : nil ]];
return [ RACDisposable disposableWithBlock :^{
NSLog ( @"done" );
}];
}];
RACSignal *signal2 = [signal1 catch :^ RACSignal *( NSError *error) {
NSLog ( @"catch error" );
return [ RACSignal return : @"idiot" ];
}];
[signal2 subscribeNext :^( id x) {
NSLog ( @"x = %@" ,x);
[subscriber sendError :[ NSError errorWithDomain : @"my fault" code : 100 userInfo : nil ]];
return [ RACDisposable disposableWithBlock :^{
NSLog ( @"done" );
}];
}];
RACSignal *signal2 = [signal1 catchTo :[ RACSignal return : @"my falut" ]];
[signal2 subscribeNext :^( id x) {
NSLog ( @"x = %@" ,x);
// 处理一个网络请求 , 当成功后把结果 okok 发送出去
[subscriber sendNext : @"okok" ];
[subscriber sendCompleted ];
return [ RACDisposable disposableWithBlock :^{
NSLog ( @"done" );
}];
}] try :^ BOOL ( NSString *s, NSError * __autoreleasing *errorPtr) {
// 在这里尝试判断 signal 的结果 , 当收到了 Next 后 , 判断其值 , 如果结果与预期一样就 , 告诉订阅者
// 比如网络请求的结果是对的
if ([s hasPrefix : @"ok" ]) {
return YES ;
} else {
return NO ;
}
}];
[signal1 subscribeNext :^( id x) {
[subscriber sendNext : @NO ];
[subscriber sendCompleted ];
return [ RACDisposable disposableWithBlock :^{
NSLog ( @"done" );
}];
}];
RACSignal *signal2 = [ RACSignal defer :^ RACSignal *{
NSLog ( @"bbbb" );
return [ RACSignal return : @"hello222" ];
}];
RACSignal *signal3 = [ RACSignal defer :^ RACSignal *{
NSLog ( @"cccc" );
return [ RACSignal return : @"hello333" ];
}];
[[ RACSignal if :signal1 then :signal2 else :signal3] subscribeNext :^( id x) {
return [ NSString stringWithFormat : @"text %d" ,count++];
[combinding subscribeNext :^( id x) {
NSLog ( @"%@" ,x);
} completed :^{
NSLog ( @"completed." );
NSLog ( @"print signal1" );
return [ RACSignal return : @"hello" ];
}];
[signal1 subscribeNext :^( id x) {
NSLog ( @"first %@" ,x);
}];
[signal1 subscribeNext :^( id x) {
NSLog ( @"second %@" ,x);
2015-06-05 14:35:38.149 DemoCategorizer[15252:2226982] first hello
2015-06-05 14:35:38.149 DemoCategorizer[15252:2226982] print signal1
NSLog ( @"print signal1" );
return [ RACSignal return : @"signal1" ];
}];
RACMulticastConnection *connection = [signal1 publish ];
[connection. signal subscribeNext :^( id x) {
NSLog ( @"first next value = %@" ,x);
}];
[connection. signal subscribeNext :^( id x) {
NSLog ( @"second next value = %@" ,x);
}];
2015-06-05 14:38:48.528 DemoCategorizer[15848:2239991] first next value = signal1
+(RACSignal*)importPhotos{
NSURLRequest *request = [self popularURLRequest];
return [[[[[[NSURLConnection rac_sendAsynchronousRequest:request]
reduceEach:^id(NSURLResponse *response, NSData *data){
return data;
}]
deliverOn:[RACSchedulermainThreadScheduler]]
map:^id(NSData*data){
id results = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
return [[[results[@"photos"] rac_sequence]
map:^id(NSDictionary *photoDictionary){
FRPPhotoModel *model = [FRPPhotoModel new];
[self configurePhotoModel:model withDictionary:photoDictionary];
[self downloadThumbnailForPhotoModel:model];
return model;
}] array];
}] publish] autoconnect];
RACReplaySubject *subject = [[ RACReplaySubject alloc ] init ];
dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW , ( int64_t )( 5 * NSEC_PER_SEC )), dispatch_get_main_queue (), ^{
[subject sendNext : @"myword" ];
[subject sendCompleted ];
});
return subject;
NSLog ( @"x1 == %@" ,x);
}];
[[ self importPhotos ] subscribeNext :^( id x) {
NSLog ( @"x2 == %@" ,x);
return [value length ] > 3 ;
}] map :^ id ( id value) {
return value;
}] array ];
[ RACSignal createSignal :^ RACDisposable *( id < RACSubscriber > subscriber) {
RACDisposable *disposable = [ RACDisposable new ];
dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT , 0 ), ^{
NSLog ( @"Start iterating..." );
for ( int i = 0 ; i < 200 && !disposable. isDisposed ; i++) {
NSLog ( @"Send %i to subscriber" , i);
[subscriber sendNext : @( i ) ];
[ NSThread sleepForTimeInterval : 0.1 ];
}
if (!disposable. isDisposed ) {
NSLog ( @"Send completed to subscriber" );
[subscriber sendCompleted ];
}
});
return disposable;
}];
NSLog ( @"About to subscribe" );
[[[signal
deliverOn :[ RACScheduler mainThreadScheduler ]]
timeout : 1.0 onScheduler :[ RACScheduler mainThreadScheduler ]]
subscribeNext :^( id x) {
NSLog ( @"Got next: %@" , x);
} error :^( NSError *error) {
NSLog ( @"Error (timeout): %@" , [error localizedDescription ]);
} completed :^{
NSLog ( @"Completed" );
@weakify(self);
RACSignal *enabled = [[RACObserve(self, viewModels)
// Map _each_ array of view models to a signal determining whether the command
// should be enabled.
map:^(NSArray *viewModels) {
RACSequence *selectionSignals = [[viewModels.rac_sequence
map:^(ViewModel *viewModel) {
// RACObserve() implicitly retains `self`, so we need to avoid
// a retain cycle.
@strongify(self);
// Observe each view model's `isSelected` property for changes.
return RACObserve(viewModel, isSelected);
}]
// Ensure we always have one YES for the -and below.
startWith:[RACSignal return:@YES]];
// Sends YES whenever all of the view models are selected, NO otherwise.
return [[RACSignal
combineLatest:selectionSignals]
and];
}]
// Then, ensure that we only subscribe to the _latest_ signal returned from
// the block above (i.e., the observations from the latest `viewModels`).
switchToLatest];
RACSignal *timer = [[[ RACSignal interval : 1 onScheduler :[ RACScheduler mainThreadScheduler ]] take : 3 ] map :^ id ( id value) {
NSLog ( @"ready time = %ld" ,second--);
return @YES ;
}];
RACSignal *record = [ RACSignal createSignal :^ RACDisposable *( id < RACSubscriber > subscriber) {
DDLogDebug ( @"start recording." );
RACSignal *signal = [[ RACSignal interval : 1 onScheduler :[ RACScheduler mainThreadScheduler ]] take : 10 ];
RACDisposable *disposable = [signal subscribeNext :^( id x) {
NSLog ( @"recording sec %ld" ,second++);
[subscriber sendNext :x];
} completed :^{
NSLog ( @"record completed." );
[subscriber sendCompleted ];
}];
return disposable;
}];
RACSignal *chianSignal = [timer concat :record];
[chianSignal subscribeNext :^( id x) {
NSLog ( @"x = %@" ,x);
} completed :^{
NSLog ( @"all done" );
/// [[[[fileManager
/// rac_createFileAtPath:path contents:data]
/// initially:^{
/// // 2. Second, backup current file
/// [fileManager moveItemAtPath:path toPath:backupPath error:nil];
/// }]
/// initially:^{
/// // 1. First, acquire write lock.
/// [writeLock lock];
/// }]
/// finally:^{
/// [writeLock unlock];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | __block int num = 0; RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id subscriber) { num++; NSLog(@"Increment num to: %i", num); [subscriber sendNext:@(num)]; return nil; }] replay]; NSLog(@"Start subscriptions"); // Subscriber 1 (S1) [signal subscribeNext:^(id x) { NSLog(@"S1: %@", x); }]; // Subscriber 2 (S2) [signal subscribeNext:^(id x) { NSLog(@"S2: %@", x); }]; // Subscriber 3 (S3) [signal subscribeNext:^(id x) { NSLog(@"S3: %@", x); }]; |
Start subscriptions
S1: 1
S2: 1
[[seq. signal aggregateWithStart :[ NSMutableSet set ] reduce :^ id ( id running, id next) {
NSLog ( @"running = %@, next = %@" ,running,next);
NSString *newString = [next stringByAppendingString :next];
[running addObject :newString];
return running;
}] subscribeNext :^( id x) {
NSLog ( @"x = %@" ,x);
} completed :^{
NSLog ( @"completed" );
)}, next = a
2015-07-09 16:19:42.556 DemoCategorizer[74923:2050916] running = {(
aa
)}, next = b
2015-07-09 16:19:42.557 DemoCategorizer[74923:2050916] running = {(
aa,
bb
)}, next = c
2015-07-09 16:19:42.557 DemoCategorizer[74923:2050916] running = {(
cc,
aa,
bb
)}, next = d
2015-07-09 16:19:42.557 DemoCategorizer[74923:2050916] running = {(
cc,
dd,
aa,
bb
)}, next = e
2015-07-09 16:19:42.557 DemoCategorizer[74923:2050916] running = {(
cc,
dd,
aa,
ee,
bb
)}, next = f
2015-07-09 16:19:42.557 DemoCategorizer[74923:2050916] x = {(
cc,
dd,
aa,
ee,
bb,
ff
)}
return @"s2" ;
}];
RACSignal *s3 = [s2 throttle : 3 ]; //3 秒钟之内 , 没有第二个 sendNext, 即 s3 sendNext
[s3 subscribeNext :^( id x) {
NSLog ( @"%@" ,x);
478



被折叠的 条评论
为什么被折叠?



