DEV Community

lyc233333
lyc233333

Posted on

πŸš€ Huawei Hongmeng Next: Analysis and practical application of AppStartup startup framework

As a developer who was forced to the early morning by application startup lag, today I will reveal the AppStartup framework of HarmonyOS!After using it for the first time, the application startup speed increased by 40%. Now, the practical skills of this "start accelerator" are fully disclosed~

1. AppStartup: "Traffic Commander" launched by the application

1. What is AppStartup?

Imagine driving to work in the morning:

  • Traditional startups are like finding keys, adjusting seats, and checking navigation after getting on the car, and they are busy
  • AppStartup plans the process in advance like: ignite β†’ adjust the seat β†’ turn on navigation, orderly and efficiently

HarmonyOS's AppStartup core capabilities:

  • Task Queuing: Schedule the order of starting tasks according to dependencies
  • Parallel Acceleration: Allows dependency-free tasks to run simultaneously
  • Late loading: Non-critical tasks are started later, display the interface first

2. Three advantages: The secret of lightning-fast startup

Advantages Traditional Startup AppStartup Startup
Task Management Scattered everywhere, difficult to maintain Centralized configuration, easy to see
Dependency processing Prone to order errors Automatically sort by dependency
Startup speed Average start time 1.5 seconds 0.9 seconds after optimization

2. Start mode: Flexible "start gearbox"

1. Automatic start: worry-free "automatic transmission"

Suitable for simple applications, start all tasks with one click:

import { AppStartup } from '@ohos.app.startup';

// Direct call to the application portal
AppStartup.getInstance().autoStartup();
Enter fullscreen mode Exit fullscreen mode

2. Manual start: flexible "manual transmission"

Suitable for complex applications, start tasks on demand:

// Start in stages
onFirstStage() {
AppStartup.getInstance().startTask('InitDatabase'); // Initialize the database first
}

onSecondStage() {
AppStartup.getInstance().startTask('LoadNetwork'); // Load the network again
}
Enter fullscreen mode Exit fullscreen mode

3. Configuration file: "Construction Blueprint" for starting the task

1. Task dependency configuration example

{
  "app_startup": [
    {
"name": "InitDatabase", // Task name
"dependency": [] // No dependencies, can be executed first
    },
    {
"name": "LoadUserConfig", // Load user configuration
"dependency": ["InitDatabase"], // Depend on database initialization
"parallel": true // Can be executed in parallel
    },
    {
"name": "InitNetwork", // Initialize the network
      "dependency": [],
      "parallel": true
    },
    {
"name": "ShowSplash", // Show the startup page
"dependency": ["LoadUserConfig", "InitNetwork"] // Wait for the first two to be completed
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

2. Detailed explanation of the configuration field

Field Function Example Value
name Task unique identifier "InitDatabase"
dependency Dependency Task List ["InitDatabase"]
parallel Whether to execute in parallel true
delay delay execution time (ms) 500

4. Parameter configuration: "Performance Regulator" for tasks

1. Task parameter setting example

import { AppStartup, StartupConfig } from '@ohos.app.startup';

// Create task configuration
const networkConfig = new StartupConfig({
  taskName: 'SetupNetwork',
priority: 2, // Priority: The larger the value, the more it will be executed first
parallel: true, // Allow parallelism
delay: 1000, // Delay 1 second execution
required: true // Must be executed
});

// Register configuration
AppStartup.getInstance().addConfig(networkConfig);
Enter fullscreen mode Exit fullscreen mode

2. Priority Policy

  • High priority: UI rendering, core service initialization (priority 5)
  • Priority: Network connection, database operation (Priority 3)
  • Low priority: Ad loading, statistical reporting (priority 1)

5. Practical development: the construction of the "production line" of the mission

1. Task development examples

import { IStartupTask } from '@ohos.app.startup';

// Network initialization task
export class NetworkTask implements IStartupTask {
// Task execution logic
  execute() {
    this.initNetwork();
console.log('Network initialization is completed');
  }

  private initNetwork() {
// Actual network initialization code
  }

// Return to the dependent task list
  getDependencies() {
return ['InitDatabase']; // Depend on database initialization
  }
}

// Register a task
AppStartup.getInstance().registerTask(new NetworkTask());
Enter fullscreen mode Exit fullscreen mode

2. Parallel task optimization

// Two parallel tasks
export class ConfigTask implements IStartupTask {
  getDependencies() { return []; }
execute() { /* Load configuration */ }
}

export class ImageTask implements IStartupTask {
  getDependencies() { return []; }
execute() { /* Preloaded image */ }
}

// Set parallel=true when registering
AppStartup.getInstance().registerTask(new ConfigTask(), { parallel: true });
AppStartup.getInstance().registerTask(new ImageTask(), { parallel: true });
Enter fullscreen mode Exit fullscreen mode

6. Performance optimization: "accelerator" for startup speed

1. Startup time comparison

Optimization items Traditional startup (ms) AppStartup optimization (ms) Improvement range
Simple Application 1200 700 41.7%
Complex Applications 2500 1500 40%

2. Optimization Tips

  • Stage startup: First display blank pages, then gradually load content
  • Delayed non-critical tasks: Advertising, recommendation and other tasks delayed to start by 500ms
  • Parallel execution: Dependency-free tasks run at the same time, such as configuration loading + image prefetching

7. Practical cases: Start-up optimization of e-commerce applications

1. Start task planning

  1. Stage 1 (must be executed immediately):
  2. Database initialization (priority 5)
  3. User login status check (priority 4)

  4. Stage 2 (parallel execution):

  5. Home page configuration loading (priority 3, parallel)

  6. Product image prefetch (priority 3, parallel)

  7. Stage 3 (Delayed Execution):

  8. Ad loading (delay of 1000ms, priority 1)

  9. Statistical reporting (delay of 800ms, priority 2)

2. Configuration file implementation

{
  "app_startup": [
    { "name": "CheckLogin", "priority": 4 },
    { "name": "InitDB", "priority": 5 },
    { "name": "LoadHomeConfig", "priority": 3, "parallel": true },
    { "name": "PrefetchImages", "priority": 3, "parallel": true },
    { "name": "LoadAd", "priority": 1, "delay": 1000 },
    { "name": "ReportStat", "priority": 2, "delay": 800 }
  ]
}
Enter fullscreen mode Exit fullscreen mode

The last thought

I remember the first time I used AppStartup to optimize the e-commerce app, the startup speed dropped from 2.3 seconds to 1.4 seconds. The boss thought I had changed my phone to test it~ Now I have developed the habit of planning the startup task first with a new function, and I am no longer afraid of startup lags~

Top comments (0)