iOS:长-运行任务
iOS: Long-Running task
这是来自 developer.apple.com
的示例
Finite-Length Tasks
Starting a background task at quit time
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
我想执行 Long-运行 任务,而不是 "Finite-Length" 任务。我没有找到任何用 objective-c 为当前版本的 sdk 编写的示例。我可以在应用程序启动时启动它,并且 运行 无论应用程序是在前台还是在后台,它都可以连续启动吗?我怎么做?
我是一名 react-native 开发人员,我才刚刚开始学习 objective-c。因此,我可能只需要简单的示例即可。我已经实现了到 Cocoa Touch Class
的桥接实例,它工作正常。我只需要在这个 class 中启动 Long-运行 任务。我需要它用于 BLE,但为了简单起见,我想说,让我们使用位置跟踪,因为它更容易和更快地测试。
我的LongRunningTask.m:
#import "LongRunningTask.h"
@implementation LongRunningTask
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(start:(RCTResponseSenderBlock)callback)
{
// start the Long-Running Task here
callback(@[@"done"]);
}
不明白,long-运行ning任务是怎么定义的?似乎没有特定的方法或任何标记可以将任务声明为 long-运行ning。所以,从技术上讲,如果我得到用户的许可 运行 特定类型的长期 运行ning 任务,我可以连续 运行 在 applicationDidEnterBackground
中我想要的任何代码?即使它与我获得的许可没有任何共同之处?
影响此任务是否会在 10 分钟内终止的唯一因素是我是否获得许可?
谢谢!
对于术语 Long-运行 任务 表示在应用程序被杀死之前一直处于活动状态的任务,我给你一个简单的例子 LocationManager
当您将您的应用设置为接收位置更新并初始化 LocationManager 时,该应用会接收位置更新,直到您在前台停止更新,BLE 也是如此。
查看示例,
_locationManager=[[CLLocationManager alloc] init];
_locationManager.delegate=self;
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
[_locationManager requestAlwaysAuthorization];
}
[_locationManager startUpdatingLocation];
如果用户允许应用程序接收 GPS 位置,并且如果应用程序的 GPS 设置为 ON 以接收更新,则上面的代码会启动应用程序的 LocationManager 以接收 GPS 位置更新,下面的方法将获取调用直到您的应用程序处于 Foreground
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
BLE
也是如此
如果您希望您的应用能够在后台接收 GPS 或 BLE 更新,您需要从应用的项目设置中打开相关的后台模式,如下图所示
该图片显示了应用程序在后台时您可以 运行 的服务列表,除了列表之外,您还可以进行某些网络活动,例如您在示例中显示的下载和上传,这将 运行一个Long-运行任务,直到你杀死应用程序,或者服务被用户手动设置中断。
希望以上能消除您的疑虑。
干杯。
这是来自 developer.apple.com
的示例Finite-Length Tasks
Starting a background task at quit time
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
我想执行 Long-运行 任务,而不是 "Finite-Length" 任务。我没有找到任何用 objective-c 为当前版本的 sdk 编写的示例。我可以在应用程序启动时启动它,并且 运行 无论应用程序是在前台还是在后台,它都可以连续启动吗?我怎么做?
我是一名 react-native 开发人员,我才刚刚开始学习 objective-c。因此,我可能只需要简单的示例即可。我已经实现了到 Cocoa Touch Class
的桥接实例,它工作正常。我只需要在这个 class 中启动 Long-运行 任务。我需要它用于 BLE,但为了简单起见,我想说,让我们使用位置跟踪,因为它更容易和更快地测试。
我的LongRunningTask.m:
#import "LongRunningTask.h"
@implementation LongRunningTask
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(start:(RCTResponseSenderBlock)callback)
{
// start the Long-Running Task here
callback(@[@"done"]);
}
不明白,long-运行ning任务是怎么定义的?似乎没有特定的方法或任何标记可以将任务声明为 long-运行ning。所以,从技术上讲,如果我得到用户的许可 运行 特定类型的长期 运行ning 任务,我可以连续 运行 在 applicationDidEnterBackground
中我想要的任何代码?即使它与我获得的许可没有任何共同之处?
影响此任务是否会在 10 分钟内终止的唯一因素是我是否获得许可?
谢谢!
对于术语 Long-运行 任务 表示在应用程序被杀死之前一直处于活动状态的任务,我给你一个简单的例子 LocationManager
当您将您的应用设置为接收位置更新并初始化 LocationManager 时,该应用会接收位置更新,直到您在前台停止更新,BLE 也是如此。
查看示例,
_locationManager=[[CLLocationManager alloc] init];
_locationManager.delegate=self;
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
[_locationManager requestAlwaysAuthorization];
}
[_locationManager startUpdatingLocation];
如果用户允许应用程序接收 GPS 位置,并且如果应用程序的 GPS 设置为 ON 以接收更新,则上面的代码会启动应用程序的 LocationManager 以接收 GPS 位置更新,下面的方法将获取调用直到您的应用程序处于 Foreground
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
BLE
也是如此如果您希望您的应用能够在后台接收 GPS 或 BLE 更新,您需要从应用的项目设置中打开相关的后台模式,如下图所示
该图片显示了应用程序在后台时您可以 运行 的服务列表,除了列表之外,您还可以进行某些网络活动,例如您在示例中显示的下载和上传,这将 运行一个Long-运行任务,直到你杀死应用程序,或者服务被用户手动设置中断。
希望以上能消除您的疑虑。
干杯。