When does a patch take effect after it is published?

更新时间:
复制 MD 格式

A new patch is requested only when `queryAndLoadNewPatch` is called.

For example, you write the following code in the `onCreate` method of your application:

SophixManager.getInstance().setContext(this)
                .setAppVersion(appVersion)
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        // Patch loading callback.
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            // The patch loaded successfully.
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            // The new patch requires a restart to take effect. Prompt the user or force a restart.
                            // Recommended: Listen for the event when the application enters the background, and then terminate the application.
                        } else if (code == PatchStatus.CODE_LOAD_FAIL) {
                            // Internal engine error. Clear local patches to prevent the failed patch from being loaded repeatedly.
                            // SophixManager.getInstance().cleanPatches();
                        } else {
                            // For other error messages, see the PatchStatus class.
                        }
                    }
                }).initialize();
SophixManager.getInstance().queryAndLoadNewPatch();

If you publish a new patch and `queryAndLoadNewPatch` is not called elsewhere, the time it takes for the patch to take effect varies depending on the following three scenarios:

One: `queryAndLoadNewPatch` is called when no patch is loaded. Instant hotpatches are applied immediately. Hotpatches that require a cold start are applied after the next restart.


Two: A local patch without resources is already applied, and you publish a new patch.

On the first restart, `queryAndLoadNewPatch` is called. The old patch is deleted, and the new patch is downloaded and preloaded. However, because the application is already initialized, the new patch is not loaded immediately.

On the second restart, `initialize` finds and loads the preloaded new patch. The new patch then takes effect.

This process typically requires two restarts.


Three: A local patch with resources is already applied, and you publish a new patch.

On the first restart, `queryAndLoadNewPatch` is called and finds the new patch. However, the old patch is not deleted yet. This ensures that the old patch can correctly access its resources at runtime. The old patch is scheduled for deletion on the next restart.

On the second restart, the system detects that a new patch was requested. During initialization, the old patch is deleted. Then, `queryAndLoadNewPatch` is called again to download and preload the new patch. However, because the application is already initialized, the new patch is not loaded immediately.

On the third restart, `initialize` finds and loads the preloaded new patch. The new patch then takes effect.

This process typically requires three restarts.


To reduce the number of restarts, you can periodically call `queryAndLoadNewPatch` while the application is running. This queries for new patches and preloads them in advance.

The timing for loading a new patch depends on when `queryAndLoadNewPatch` is called.

Starting with version 3.1.5, if a patch already exists when you pull a new patch, the new patch takes effect after a single restart.

Note

This also applies when you pull a new patch and the currently loaded patch contains resource changes.