首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >后台模式下的循环

后台模式下的循环
EN

Stack Overflow用户
提问于 2016-03-01 02:10:12
回答 1查看 148关注 0票数 1

我想在后台搜索RSSI。我正在尝试通过在同一函数中调用该函数来完成此操作。有没有办法让这个循环处于后台模式。在我看来,它应该生活在背景中,但我在几个毫秒后就死了。如何让它保持搜索?

代码语言:javascript
复制
- (void)applicationWillResignActive:(UIApplication *)application
{
    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
    {
        NSLog(@"Multitasking Supported");
    }
    else
    {
        NSLog(@"Multitasking Not Supported");
    }

    NSLog(@"Start background task");
    mBeaconDeviceList = [[NSMutableArray alloc] init];
    mBeaconConfigManager = [[JLEBeaconConfigManager alloc] init];
    mBeaconConfigManager.delegate = self;
    [mBeaconConfigManager startJaaleeBeaconsDiscovery];
}

#pragma mark - JLEBeaconConfigManager delegate
-(void)beaconConfigManager:(JLEBeaconConfigManager *)manager didDiscoverBeacon:(JLEBeaconDevice *)beacon RSSI:(NSNumber *)RSSI AdvData:(NSDictionary *)AdvData
{
    NSLog(@"Find RSSI");
    if ([RSSI intValue] > 0 || [RSSI intValue] < -40) {
        return;
    }

    if ([mBeaconDeviceList containsObject:beacon]) {
        [mBeaconDeviceList removeObject:beacon];
    }
    [mBeaconDeviceList addObject:beacon];
    NSLog(@"FOUND: %@", RSSI);

    NSLog(@"Start again");
    mBeaconDeviceList = [[NSMutableArray alloc] init];
    mBeaconConfigManager.delegate = self;
    [mBeaconConfigManager startJaaleeBeaconsDiscovery];
}
EN

回答 1

Stack Overflow用户

发布于 2016-03-01 02:21:01

您可以将后台时间延长到3分钟,这将使您有更多的时间从RSSI中进行搜索。您可以像这样在applicationDidEnterBackground中扩展它:

代码语言:javascript
复制
- (void)extendBackgroundRunningTime {
    if (_backgroundTask != UIBackgroundTaskInvalid) {
        // if we are in here, that means the background task is already running.
        // don't restart it.
        return;
    }
    NSLog(@"Attempting to extend background running time");

    __block Boolean self_terminate = YES;

    _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"DummyTask" expirationHandler:^{
        NSLog(@"Background task expired by iOS");
        if (self_terminate) {
            [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
            _backgroundTask = UIBackgroundTaskInvalid;
        }
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Background task started");

        while (true) {
            NSLog(@"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining);
            [NSThread sleepForTimeInterval:1];
        }

    });
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35706497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档