API呼叫终止屏幕下降时(或应用程序进行后台)iOS飘动

发布于 2025-01-23 07:29:18 字数 1773 浏览 3 评论 0原文

我有一个登录页面,该页面在用户输入用户名和密码后开始下载该应用程序的基本数据,并且在下载数据中间,如果屏幕下载并锁定锁定,则在iOS中进行了长时间的操作

,该操作终止。

这是代码 登录部分部分:

var repository = GlobalRestRepository();
var db = BasicDB();
List<basicModel> notDownloaded = await db.selectByLoaded(false);
for (int i = 0; i < notDownloaded.length; i++) {
    await repository.getBasic(notDownloaded.elementAt(i));
}

GlobalRestReSpository部分:

class GlobalRestRepository {
  final HttpClient http = HttpClient();

Future<void> getBasic(basicModel model) async {
    String url = "${Variables.mainUrl + basicModelUrl}";

    var response = await http.postExtraToken(url);
    .
    .
    .
 }
}

httpclient部分:

import 'package:http/http.dart';
...
class HttpClient {

  static final HttpClient _instance = HttpClient._privateConstructor();

  factory HttpClient() {
    return _instance;
  }

  Future<dynamic> postExtraToken(String path) async {
    Response response;
    try {
      response = await post(Uri.parse(path),
              headers: {"extra": Variables.extra, "token": Variables.token});
      final statusCode = response.statusCode;
      if (statusCode >= 200 && statusCode < 299) {
        if (response.body.isEmpty) {
          return [];
        } else {
          return jsonDecode(utf8.decode(response.bodyBytes));
        }
      } else if (statusCode >= 400 && statusCode < 500) {
        throw ClientErrorException();
      } else if (statusCode >= 500 && statusCode < 600) {
        throw ServerErrorException();
      } else {
        throw UnknownException();
      }
    } on SocketException {
      throw ConnectionException();
    }
  }
}

有人可以帮助我吗?

I have a login page which starts downloading base data for the app after user enters username and password, and its a long duration operation like 2 or 3 minutes

In IOS at the middle of downloading data if screen gets off and locked, the operations terminates.

Here is the code
LoginPage part:

var repository = GlobalRestRepository();
var db = BasicDB();
List<basicModel> notDownloaded = await db.selectByLoaded(false);
for (int i = 0; i < notDownloaded.length; i++) {
    await repository.getBasic(notDownloaded.elementAt(i));
}

GlobalRestRepository part:

class GlobalRestRepository {
  final HttpClient http = HttpClient();

Future<void> getBasic(basicModel model) async {
    String url = "${Variables.mainUrl + basicModelUrl}";

    var response = await http.postExtraToken(url);
    .
    .
    .
 }
}

HttpClient part:

import 'package:http/http.dart';
...
class HttpClient {

  static final HttpClient _instance = HttpClient._privateConstructor();

  factory HttpClient() {
    return _instance;
  }

  Future<dynamic> postExtraToken(String path) async {
    Response response;
    try {
      response = await post(Uri.parse(path),
              headers: {"extra": Variables.extra, "token": Variables.token});
      final statusCode = response.statusCode;
      if (statusCode >= 200 && statusCode < 299) {
        if (response.body.isEmpty) {
          return [];
        } else {
          return jsonDecode(utf8.decode(response.bodyBytes));
        }
      } else if (statusCode >= 400 && statusCode < 500) {
        throw ClientErrorException();
      } else if (statusCode >= 500 && statusCode < 600) {
        throw ServerErrorException();
      } else {
        throw UnknownException();
      }
    } on SocketException {
      throw ConnectionException();
    }
  }
}

Can anyone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2025-01-30 07:29:18

使用 wakelock 插件,我们可以解决这个问题,但我不知道这是最好的解决方案或最好的解决方案或不是。
注意:此插件适用于除Linux以外的所有平台。

在启动API请求的屏幕中添加以下代码。

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    checkAndEnableWakeLock();
  }

  void checkAndEnableWakeLock() async {
    bool wakeLockEnabled = await Wakelock.enabled;
    if (!wakeLockEnabled) {
      Wakelock.enable();
    }
  }

当所有API调用完成时,呼叫禁用方法。

Wakelock.disable();

使用上述代码,您可以避免屏幕脱离问题。

Using Wakelock plugin, we can solve this problem, but I don't know this is the best solution or not.
Note: This plugin works for all the platforms except linux.

Add the below code in the screen where you are initiating the api request.

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    checkAndEnableWakeLock();
  }

  void checkAndEnableWakeLock() async {
    bool wakeLockEnabled = await Wakelock.enabled;
    if (!wakeLockEnabled) {
      Wakelock.enable();
    }
  }

Call disable method when all the api calls are completed.

Wakelock.disable();

Using above code you can avoid screen getting off issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文