غیرفعال کردن «پایشگر کارایی Firebase»


برای اینکه به کاربران اجازه دهید بااستفاده از Firebase Performance Monitoring موافقت کنند یا از آن انصراف دهند، ممکن است بخواهید برنامه‌تان را به‌گونه‌ای پیکربندی کنید که بتوانید Performance Monitoring را فعال و غیرفعال کنید. همچنین ممکن است این قابلیت را درطول توسعه و آزمایش برنامه مفید بیابید.

در اینجا چند گزینه برای بررسی ارائه شده است:

  • می‌توانید هنگام ساختن برنامه‌تان، کیت توسعه نرم‌افزار Performance Monitoring را غیرفعال کنید و این گزینه را داشته باشید که آن را در زمان اجرا دوباره فعال کنید.

  • می‌توانید برنامه‌تان را با کیت توسعه نرم‌افزار Performance Monitoring فعال بسازید اما این گزینه را دارید که آن را در زمان اجرا بااستفاده از Firebase Remote Config غیرفعال کنید.

  • می‌توانید کیت توسعه نرم‌افزار Performance Monitoring را کاملاً غیرفعال کنید، بدون اینکه گزینه‌ای برای فعال کردن آن در زمان اجرا وجود داشته باشد.

غیرفعال کردن Performance Monitoring درطول فرایند ساخت برنامه

یک موقعیت که در آن غیرفعال کردن Performance Monitoring درطول فرایند ساخت برنامه می‌تواند مفید باشد، اجتناب از گزارش داده‌های عملکرد از نسخه پیش‌انتشار برنامه درطول توسعه و آزمایش برنامه است.

برای غیرفعال کردن یا ازکار انداختن Performance Monitoring، می‌توانید یکی از دو کلید را به فایل فهرست ویژگی (Info.plist) برای برنامه Apple خود اضافه کنید:

  • برای غیرفعال کردن Performance Monitoring، اما اجازه دادن به برنامه‌تان برای فعال کردن آن در زمان اجرا، firebase_performance_collection_enabled را در فایل Info.plist برنامه‌تان روی false تنظیم کنید.

  • برای غیرفعال کردن کامل Performance Monitoring، بدون گزینه فعال کردن آن در زمان اجرا، firebase_performance_collection_deactivated را روی true در فایل Info.plist برنامه‌تان تنظیم کنید.

غیرفعال کردن برنامه در زمان اجرا بااستفاده از Remote Config

Firebase Remote Config به شما امکان می‌دهد تغییراتی در رفتار و ظاهر برنامه‌تان ایجاد کنید، بنابراین روشی ایده‌آل برای غیرفعال کردن Performance Monitoring در نمونه‌های مستقرشده برنامه‌تان ارائه می‌دهد.

برای غیرفعال کردن جمع‌آوری داده‌های Performance Monitoring در دفعه بعدی که برنامه Apple شما شروع می‌شود، از کد نمونه نشان‌داده‌شده در زیر استفاده کنید. برای اطلاعات بیشتر درباره استفاده از Remote Config در برنامه Apple، به استفاده از Firebase Remote Config در پلاتفرم‌های Apple مراجعه کنید.

  1. مطمئن شوید که Remote Config در Podfile شما استفاده شده باشد:

    pod 'Firebase/RemoteConfig'
    
  2. مورد زیر را به بالای فایل AppDelegate برنامه‌تان اضافه کنید:

    Swift

    توجه: این محصول Firebase در macOS،‏ Mac Catalyst، و اهداف watchOS دردسترس نیست.
    import FirebaseRemoteConfig
    

    Objective-C

    توجه: این محصول Firebase در macOS،‏ Mac Catalyst، و هدف‌های watchOS دردسترس نیست.
    @import FirebaseRemoteConfig;
    
  3. در فایل AppDelegate، کد زیر را به عبارت‌های launchOptions در روش نمونه application:didFinishLaunchingWithOptions: اضافه کنید:

    Swift

    توجه: این محصول در 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 یا فایل پیاده‌سازی دیگری که برنامه‌تان استفاده می‌کند، کد زیر را برای واکشی و فعال کردن مقادیر Remote Config اضافه کنید:

    Swift

    توجه: این محصول 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. برای غیرفعال کردن Performance Monitoring در کنسول Firebase، پارامتر perf_disable را در پروژه برنامه‌تان ایجاد کنید، سپس مقدار آن را روی true تنظیم کنید.

    اگر مقدار perf_disable را روی false تنظیم کنید، Performance Monitoring فعال باقی می‌ماند.

جمع‌آوری خودکار یا سفارشی داده‌ها را به‌طور جداگانه غیرفعال کنید

می‌توانید تغییراتی در کد نشان‌داده‌شده در بالا و در کنسول Firebase ایجاد کنید تا بتوانید همه پایش‌های خودکار (پیش‌فرض) را جدا از پایش سفارشی غیرفعال کنید.

  1. کد زیر را به عبارت‌های launchOptions در روش نمونه application:didFinishLaunchingWithOptions: اضافه کنید (به‌جای آنچه در بالا برای همان روش نمونه نشان داده شده است):

    Swift

    توجه: این محصول 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 تنظیم کنید.