Wyłącz Monitorowanie wydajności Firebase

Aby umożliwić użytkownikom włączanie lub wyłączanie monitorowania wydajności Firebase, tak skonfigurować aplikację, aby móc włączyć lub wyłączyć Monitorowanie wydajności. Ty Może się też okazać przydatne podczas tworzenia i testowania aplikacji.

Oto kilka opcji, które warto wziąć pod uwagę:

  • Podczas tworzenia aplikacji możesz wyłączyć pakiet SDK Performance Monitoring oraz opcję włącz ją ponownie w czasie działania aplikacji.

  • Możesz utworzyć aplikację z włączonym pakietem SDK Performance Monitoring, ale także wyłącz je w czasie działania za pomocą Zdalnej konfiguracji Firebase.

  • Możesz całkowicie wyłączyć pakiet SDK Performance Monitoring bez możliwości jego włączenia w czasie działania aplikacji.

Wyłączanie monitorowania wydajności podczas kompilacji aplikacji

Jedną z sytuacji, w której wyłączenie Monitorowania wydajności podczas tworzenia aplikacji może być warto uniknąć raportowania danych o skuteczności z przedpremierowej wersji podczas tworzenia i testowania aplikacji.

Aby wyłączyć lub wyłączyć Monitorowanie wydajności, możesz dodać jeden z dwóch kluczy do plik z listą właściwości (Info.plist) Twojej aplikacji Apple:

  • Aby wyłączyć Monitorowanie wydajności, ale zezwolić aplikacji na jego włączenie w czasie działania, ustaw Od firebase_performance_collection_enabled do false w aplikacji Info.plist.

  • Aby całkowicie wyłączyć Monitorowanie wydajności bez możliwości jego włączenia w czasie działania, ustaw firebase_performance_collection_deactivated na true w aplikacji Info.plist.

Wyłączaj aplikację w czasie działania za pomocą Zdalnej konfiguracji

Zdalna konfiguracja Firebase umożliwia zmianę działania i wyglądu jest idealnym sposobem na wyłączenie Monitorowania wydajności instancji wdrożonych aplikacji.

Aby wyłączyć zbieranie danych w Monitorowaniu wydajności przy następnym uruchomieniu aplikacji Apple, skorzystaj z przykładowego kodu widocznego poniżej. Więcej informacji o korzystaniu z Zdalna konfiguracja w aplikacji Apple: zobacz Używanie Zdalnej konfiguracji Firebase na platformach Apple

  1. Upewnij się, że w Podfile jest używana Zdalna konfiguracja:

    pod 'Firebase/RemoteConfig'
    
  2. Na początku pliku AppDelegate z informacjami o aplikacji dodaj:

    Swift

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    import FirebaseRemoteConfig
    

    Objective-C

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    @import FirebaseRemoteConfig;
    
  3. W pliku AppDelegate dodaj do sekcji launchOptions poniższy kod instrukcje w instancji application:didFinishLaunchingWithOptions: :

    Swift

    Uwaga: ta usługa nie jest dostępna na urządzeniach z systemem macOS, Mac Catalyst ani watchOS.
    remoteConfig = RemoteConfig.remoteConfig()
    // You can change the "false" below to "true" to permit more fetches when validating
    // your app, but you should change it back to "false" or remove this statement before
    // distributing your app in production.
    let remoteConfigSettings = RemoteConfigSettings(developerModeEnabled: false)
    remoteConfig.configSettings = remoteConfigSettings!
    // Load in-app defaults from a plist file that sets perf_disable to false until
    // you update values in the Firebase console.
    remoteConfig.setDefaultsFromPlistFileName("RemoteConfigDefaults")
    // Important! This needs to be applied before FirebaseApp.configure()
    if !remoteConfig["perf_disable"].boolValue {
        // The following line disables all automatic (out-of-the-box) monitoring
        Performance.sharedInstance().isInstrumentationEnabled = false
        // The following line disables all custom monitoring
        Performance.sharedInstance().isDataCollectionEnabled = false
    }
    else {
        Performance.sharedInstance().isInstrumentationEnabled = true
        Performance.sharedInstance().isDataCollectionEnabled = true
    }
    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    

    Objective-C

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    self.remoteConfig = [FIRRemoteConfig remoteConfig];
    // You can change the NO below to YES to permit more fetches when validating
    // your app, but you should change it back to NO or remove this statement before
    // distributing your app in production.
    FIRRemoteConfigSettings *remoteConfigSettings =
        [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:NO];
    self.remoteConfig.configSettings = remoteConfigSettings;
    // Load in-app defaults from a plist file that sets perf_disable to false until
    // you update values in the Firebase console.
    [self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"];
    // Important! This needs to be applied before [FIRApp configure]
    if (!self.remoteConfig[@"perf_disable"].numberValue.boolValue) {
        // The following line disables all automatic (out-of-the-box) monitoring
        [FIRPerformance sharedInstance].instrumentationEnabled = NO;
        // The following line disables all custom monitoring
        [FIRPerformance sharedInstance].dataCollectionEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].instrumentationEnabled = YES;
        [FIRPerformance sharedInstance].dataCollectionEnabled = YES;
    }
    // Use Firebase library to configure APIs
    [FIRApp configure];
    
  4. W pliku ViewController.m lub innym pliku implementacji używanym przez aplikację dodaj ten kod do pobrania i aktywowania wartości Zdalnej konfiguracji:

    Swift

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    //RemoteConfig fetch and activation in your app, shortly after startup
    remoteConfig.fetch(withExpirationDuration: TimeInterval(30.0)) { (status, error) -> Void in
      if status == .success {
        print("Config fetched!")
        self.remoteConfig.activateFetched()
      } else {
        print("Config not fetched")
        print("Error \(error!.localizedDescription)")
      }
    }
    

    Objective-C

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    //RemoteConfig fetch and activation in your app, shortly after startup
    [self.remoteConfig fetchWithExpirationDuration:30.0 completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
      if (status == FIRRemoteConfigFetchStatusSuccess) {
        NSLog(@"Config fetched!");
        [self.remoteConfig activateFetched];
      } else {
        NSLog(@"Config not fetched");
        NSLog(@"Error %@", error.localizedDescription);
      }
    }];
    
  5. Aby wyłączyć Monitorowanie wydajności w konsoli Firebase, utwórz perf_disable, w projekcie aplikacji, a potem ustaw jego wartość na true.

    Jeśli ustawisz wartość perf_disable na false, monitorowanie wydajności pozostanie bez zmian. .

Osobne wyłączanie automatycznego lub niestandardowego zbierania danych

Możesz wprowadzić pewne zmiany w kodzie pokazanym powyżej i w konsoli Firebase umożliwiając wyłączenie całego automatycznego monitorowania niezależnie i monitorowaniu niestandardowego.

  1. Dodaj ten kod do instrukcji launchOptions w tabeli application:didFinishLaunchingWithOptions: (zamiast co widać powyżej dla tej samej metody instancji):

    Swift

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    remoteConfig = FIRRemoteConfig.remoteConfig()
    let remoteConfigSettings = FIRRemoteConfigSettings(developerModeEnabled: true)
    remoteConfig.configSettings = remoteConfigSettings!
    // Important! This needs to be applied before FirebaseApp.configure()
    if remoteConfig["perf_disable_auto"].boolValue {
        // The following line disables all automatic (out-of-the-box) monitoring
        Performance.sharedInstance().isInstrumentationEnabled = false
    }
    else {
        Performance.sharedInstance().isInstrumentationEnabled = true
    }
    if remoteConfig["perf_disable_manual"].boolValue {
        // The following line disables all custom monitoring
        Performance.sharedInstance().isDataCollectionEnabled = false
    }
    else {
        Performance.sharedInstance().isDataCollectionEnabled = true
    }
    // Use Firebase library to configure APIs
    FirebaseApp.configure()
    

    Objective-C

    Uwaga: ta usługa Firebase nie jest dostępna na systemy docelowe macOS, Mac Catalyst i watchOS.
    self.remoteConfig = [FIRRemoteConfig remoteConfig];
    FIRRemoteConfigSettings *remoteConfigSettings =
        [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
    self.remoteConfig.configSettings = remoteConfigSettings;
    // Important! This needs to be applied before [FirebaseApp configure]
    if (self.remoteConfig[@"perf_disable_auto"].numberValue.boolValue) {
        // The following line disables all automatic (out-of-the-box) monitoring
        [FIRPerformance sharedInstance].instrumentationEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].instrumentationEnabled = YES;
    }
    if (self.remoteConfig[@"perf_disable_manual"].numberValue.boolValue) {
        // The following line disables all custom monitoring
        [FIRPerformance sharedInstance].dataCollectionEnabled = NO;
    }
    else {
        [FIRPerformance sharedInstance].dataCollectionEnabled = YES;
    }
    // Use Firebase library to configure APIs
    [FirebaseApp configure];
    
  2. W konsoli Firebase wykonaj te czynności:

    • Aby wyłączyć całe automatyczne (gotowe) monitorowanie, utwórz perf_disable_auto w projekcie aplikacji, a następnie ustaw jego na true.
    • Aby wyłączyć całe niestandardowe monitorowanie, utwórz perf_disable_manual w projekcie aplikacji, a potem ustaw jego wartość na true.
    .