Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios reactivecococoa和NSThread_Ios_Objective C_Reactive Programming_Reactive Cocoa - Fatal编程技术网

Ios reactivecococoa和NSThread

Ios reactivecococoa和NSThread,ios,objective-c,reactive-programming,reactive-cocoa,Ios,Objective C,Reactive Programming,Reactive Cocoa,我发现了一些代码,但我不太明白它是如何工作的: RACSignal *oneNumberEverySecond = [[RACSignal interval:1.0f onScheduler:[RACScheduler scheduler]] scanWithStart:@0 redu

我发现了一些代码,但我不太明白它是如何工作的:

RACSignal *oneNumberEverySecond = [[RACSignal
                                    interval:1.0f
                                    onScheduler:[RACScheduler scheduler]]
                                   scanWithStart:@0 reduce:^id(NSNumber *running, NSDate *next) {
                                       return @(running.unsignedIntegerValue + 1);
                                   }];
[oneNumberEverySecond subscribeNext:^(id x) {
     NSLog(@"%@", x);
 }];

[NSThread sleepForTimeInterval:5.0f];

当我注释掉
[NSThread sleepForTimeInterval:5.0f]
时,信号停止发送值。为什么?

原因是
onScheduler:[RACScheduler]
使此信号在后台线程上运行。这意味着只有当其线程由操作系统运行时,它才会工作。调用
[NSThread sleepForTimeInterval:5.0f]
时,它看起来会正常工作的原因是,这将在给定时间内阻止正在调用它的任何线程。在您的情况下,它将阻止主线程,这将允许操作系统给您的背景信号触发时间。如果注释掉
[NSThread sleepForTimeINterval:5.0f]您将看到它仍然有效,但我的代码可能是pathcy,而不是总是fire