Skip to content

Instantly share code, notes, and snippets.

@alectogeek
Created March 17, 2020 10:44
Show Gist options
  • Save alectogeek/259ec7b15a54470ec70e6eec7fcd96bb to your computer and use it in GitHub Desktop.
Save alectogeek/259ec7b15a54470ec70e6eec7fcd96bb to your computer and use it in GitHub Desktop.

Revisions

  1. alectogeek created this gist Mar 17, 2020.
    42 changes: 42 additions & 0 deletions FirebaseRemoteConfig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    FIRRemoteConfig *remoteConfig = [FIRRemoteConfig remoteConfig];
    long expiration = (long)call.arguments[@"expiration"];

    [remoteConfig
    fetchWithExpirationDuration:expiration
    completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
    NSNumber *lastFetchTime = [[NSNumber alloc]
    initWithLong:(long)[[remoteConfig lastFetchTime] timeIntervalSince1970] *
    1000];
    NSString *lastFetchStatus =
    [self mapLastFetchStatus:(FIRRemoteConfigFetchStatus)[remoteConfig
    lastFetchStatus]];
    NSMutableDictionary *resultDict = [[NSMutableDictionary alloc] init];
    resultDict[@"lastFetchTime"] = lastFetchTime;
    resultDict[@"lastFetchStatus"] = lastFetchStatus;

    if (status != FIRRemoteConfigFetchStatusSuccess) {
    FlutterError *flutterError;
    if (status == FIRRemoteConfigFetchStatusThrottled) {
    int mills =
    [[error.userInfo
    valueForKey:FIRRemoteConfigThrottledEndTimeInSecondsKey] intValue] *
    1000;
    resultDict[@"fetchThrottledEnd"] = [[NSNumber alloc] initWithInt:mills];
    NSString *errorMessage =
    @"Fetch has been throttled. See the error's fetchThrottledEnd "
    "field for throttle end time.";
    flutterError = [FlutterError errorWithCode:@"fetchFailedThrottled"
    message:errorMessage
    details:resultDict];
    } else {
    NSString *errorMessage = @"Unable to complete fetch. Reason is unknown "
    "but this could be due to lack of connectivity.";
    flutterError = [FlutterError errorWithCode:@"fetchFailed"
    message:errorMessage
    details:resultDict];
    }
    result(flutterError);
    } else {
    result(resultDict);
    }
    }];