השבת את ניטור הביצועים של Firebase

כדי לאפשר למשתמשים שלך להצטרף או לבטל את הסכמתך לשימוש בניטור ביצועים של Firebase, ייתכן שתרצה להגדיר את האפליקציה שלך כך שתוכל להפעיל ולהשבית את ניטור הביצועים. ייתכן שתמצא גם יכולת זו שימושית במהלך פיתוח ובדיקות של אפליקציות.

להלן מספר אפשרויות שכדאי לשקול:

  • אתה יכול להשבית את SDK לניטור ביצועים בעת בניית האפליקציה שלך, עם אפשרות להפעיל אותה מחדש בזמן ריצה.

  • אתה יכול לבנות את האפליקציה שלך עם ה-SDK לניטור ביצועים מופעל, אבל יש לך אפשרות להשבית אותה בזמן ריצה באמצעות Firebase Remote Config.

  • אתה יכול לבטל לחלוטין את ה-SDK לניטור ביצועים, ללא אפשרות להפעיל אותו בזמן ריצה.

השבת את ניטור הביצועים במהלך תהליך בניית האפליקציה שלך

מצב אחד שבו השבתת ניטור הביצועים במהלך תהליך בניית האפליקציה שלך יכולה להיות שימושית היא להימנע מדיווח על נתוני ביצועים מגרסה מוקדמת של האפליקציה שלך במהלך פיתוח ובדיקה של האפליקציה.

כדי להשבית או לבטל את ניטור הביצועים, אתה יכול להוסיף אחד משני מפתחות לקובץ רשימת הנכסים ( Info.plist ) עבור אפליקציית Apple שלך:

  • כדי להשבית את ניטור הביצועים, אך לאפשר לאפליקציה שלך להפעיל אותו בזמן ריצה, הגדר את firebase_performance_collection_enabled ל- false בקובץ Info.plist של האפליקציה שלך.

  • כדי לבטל לחלוטין את ניטור הביצועים, ללא אפשרות להפעיל אותו בזמן ריצה, הגדר firebase_performance_collection_deactivated ל- true בקובץ Info.plist של האפליקציה שלך.

השבת את האפליקציה שלך בזמן ריצה באמצעות Remote Config

Firebase Remote Config מאפשר לך לבצע שינויים בהתנהגות ובמראה של האפליקציה שלך, כך שהיא מספקת דרך אידיאלית לאפשר לך להשבית את ניטור הביצועים במופעים שנפרסו באפליקציה שלך.

כדי להשבית את איסוף הנתונים של ניטור ביצועים בפעם הבאה שהאפליקציה של Apple שלך ​​מופעלת, השתמש בקוד לדוגמה המוצג להלן. למידע נוסף על שימוש בתצורה מרחוק באפליקציית Apple, ראה שימוש בתצורה מרחוק של Firebase בפלטפורמות של Apple .

  1. ודא כי נעשה שימוש בתצורה מרחוק ב- Podfile שלך:

    pod 'Firebase/RemoteConfig'
    
  2. הוסף את הדברים הבאים לחלק העליון של קובץ AppDelegate של האפליקציה שלך:

    מָהִיר

    הערה: מוצר Firebase זה אינו זמין ביעדי macOS, Mac Catalyst, watchOS.
    import FirebaseRemoteConfig
    

    Objective-C

    הערה: מוצר Firebase זה אינו זמין ביעדי macOS, Mac Catalyst, watchOS.
    @import FirebaseRemoteConfig;
    
  3. בקובץ AppDelegate שלך, הוסף את הקוד הבא להצהרות launchOptions בשיטת המופע application:didFinishLaunchingWithOptions: ::

    מָהִיר

    הערה: מוצר זה אינו זמין ביעדי macOS, Mac Catalyst, 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

    הערה: מוצר Firebase זה אינו זמין ביעדי macOS, Mac Catalyst, 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. ב- ViewController.m , או בקובץ יישום אחר המשמש את האפליקציה שלך, הוסף את הקוד הבא כדי לאחזר ולהפעיל ערכי Config מרחוק:

    מָהִיר

    הערה: מוצר Firebase זה אינו זמין ביעדי macOS, Mac Catalyst, 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

    הערה: מוצר Firebase זה אינו זמין ביעדי macOS, Mac Catalyst, 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. כדי להשבית את ניטור הביצועים במסוף Firebase, צור פרמטר perf_disable בפרויקט של האפליקציה שלך, ולאחר מכן הגדר את הערך שלו ל- true .

    אם תגדיר את הערך של perf_disable ל- false , ניטור הביצועים יישאר מופעל.

השבת איסוף נתונים אוטומטי או מותאם אישית בנפרד

אתה יכול לבצע כמה שינויים בקוד המוצג לעיל ובמסוף Firebase כדי לאפשר לך להשבית את כל הניטור האוטומטי (מהקופסה) בנפרד מהניטור המותאם אישית.

  1. הוסף את הקוד הבא להצהרות launchOptions application:didFinishLaunchingWithOptions: שיטת מופע (במקום מה שמוצג לעיל עבור אותה שיטת מופע):

    מָהִיר

    הערה: מוצר Firebase זה אינו זמין ביעדי macOS, Mac Catalyst, 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

    הערה: מוצר Firebase זה אינו זמין ביעדי macOS, Mac Catalyst, 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. השלם את הפעולות הבאות במסוף Firebase:

    • כדי להשבית את כל הניטור האוטומטי (מהקופסה), צור פרמטר perf_disable_auto בפרויקט של האפליקציה שלך, ולאחר מכן הגדר את הערך שלו ל- true .
    • כדי להשבית את כל הניטור המותאם אישית, צור פרמטר perf_disable_manual בפרויקט של האפליקציה שלך, ולאחר מכן הגדר את הערך שלו ל- true .