F#油门功能调用频率

时间:2018-08-02 15:02:42

标签: f#

我有以下代码可以每秒限制一次- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSURL *jsCodeLocation; // Debug //jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; // Release jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"MODULE" initialProperties:nil launchOptions:launchOptions]; rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootViewController = [UIViewController new]; rootViewController.view = rootView; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; // Firebase [FIRApp configure]; [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; return YES; } 的调用。但是,该函数的调用没有任何延迟。我有想念吗?

httpRequestStringThrottled

1 个答案:

答案 0 :(得分:2)

据我所知,您的代码实际上在F#交互中运行良好。您只是错过了实际评估按顺序创建的Async的调用:

// Test
[0..100] |> Seq.map (fun _ -> 
    let html = httpRequestStringThrottled "..."
    html)  |> Async.Parallel |> Async.RunSynchronously

您的代码创建101个Async值的序列。这些会立即创建,但不会立即进行评估。仅当您调用Async.StartAsync.RunSynchronously之类的内容时,才会评估它们。您的情况下,根据上一次通话的时间,它们将被正确阻止长达1秒钟,但是该阻止仅在评估Async时发生,因此您必须强制他们进行评估以查看效果。

为了测试您的代码,我编写了httpRequestStringAsync的模拟版本,其中显示了实际调用的时间。

let httpRequestStringAsync url =
   async {
       printfn "Requesting Html @ %A..." DateTime.Now
       do! Async.Sleep(50)
       return "html"
   }

然后,当运行上面测试的修改版本时,我得到以下输出:

Requesting Html @ 8/2/2018 11:15:17 AM...
Requesting Html @ 8/2/2018 11:15:18 AM...
Requesting Html @ 8/2/2018 11:15:19 AM...
Requesting Html @ 8/2/2018 11:15:20 AM...
Requesting Html @ 8/2/2018 11:15:21 AM...