我们是否应该为所有不同的可下载文件创建不同的唯一会话标识符 - swift

Should we create the difference unique session identifier for all difference downloadable files - swift

我是编程新手IOS。我想创建一个示例应用程序,允许用户从我的服务器下载许多文件。例如,我有 10 个文件,那么我是否应该调用方法 let configuration = URLSessionConfiguration.background(withIdentifier: "firstTask") 10 次,但差异标识符如第一个文件 withIdentifier: "firstTask" 和第二个文件应该是 withIdentifier: "secondTask"?原因是我想让用户下载相同的文件名然后我只是用其他名称修改 withIdentifier: "" 。那正确吗?请给我一些建议。

其他问题:

我们如何使具有特定标识符的会话无效?

根据 documentation:

identifier The unique identifier for the configuration object. This parameter must not be nil or an empty string.

还有一个说法:

If an iOS app is terminated by the system and relaunched, the app can use the same identifier to create a new configuration object and session and to retrieve the status of transfers that were in progress at the time of termination. This behavior applies only for normal termination of the app by the system. If the user terminates the app from the multitasking screen, the system cancels all of the session’s background transfers. In addition, the system does not automatically relaunch apps that were force quit by the user. The user must explicitly relaunch the app before transfers can begin again.

Finally:

Create a background URLSessionConfiguration object with the class method background(withIdentifier:) of URLSession, providing a session identifier that is unique within your app. Because most apps need only a few background sessions (usually one), you can use a fixed string for the identifier, rather than a dynamically generated identifier. The identifier doesn’t need to be unique globally.

因此,为您的应用使用一个独特的 identifier 是合理的。 例如,它可能包含 bundle id,例如整个应用程序的 com.awesomeapps.appname.sessionId

Downloading Files in the Background 建议您“使用少量后台会话——最好只使用一个——并使用这些会话同时启动许多下载任务。”

但是,如果您需要在您的应用程序暂停时下载完成的详细通知,您将需要多个会话。在与会话关联的所有后台传输完成后,系统恢复应用程序并通过 UIApplicationDelegate 方法 application(_:handleEventsForBackgroundURLSession:completionHandler:).

将会话标识符发送回应用程序

会话标识符不需要在全球范围内是唯一的。如果你只有一个会话,你选择什么都没有关系。