通过BackgroundTask在后台维护多路连接会话? [英] Maintain a Multipeer Connectivity session in Background via BackgroundTask?

查看:121
本文介绍了通过BackgroundTask在后台维护多路连接会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用程序暂时进入后台时维护MultipeerConnectivity会话,所以我考虑使用后台任务,因为我在这里看过几次......问题是我不知道如何维持与UIBackgroundTask的会话,有人可以发一个提示

I am trying to maintain a MultipeerConnectivity "session" when the application enters temporarily in the background, so I thought about using a background task as I've seen few times here ... The problem is I have no idea how to "maintain" the session with the UIBackgroundTask, can someone please post a hint

我不关心广告商/浏览器,可以阻止他们,但我' d喜欢会话不断开,因为重新连接暂时是超级错误。

I don't care about the advertisers/browsers, it's okay to stop them, but I'd like the session to not disconnect as reconnecting is super buggy for the moment.

推荐答案

根据苹果文档如果应用移动在后台,框架停止广告和浏览并断开任何打开的会话。返回到前台后,框架会自动恢复广告和浏览,但开发人员必须重新建立任何已关闭的会话请参阅: Apple doc

As per apple documentation "If the app moves into the background, the framework stops advertising and browsing and disconnects any open sessions. Upon returning to the foreground, the framework automatically resumes advertising and browsing, but the developer must reestablish any closed sessions" Refer: Apple doc

前一种方式趋势连接如下

One way of extending the connection is as follows

回答我自己的问题,希望它可以帮助处于同样情况的人。
对于刚接触iOS开发的人来说,使用后台服务简单意味着打开目标功能选项卡中的后台模式选项。
单独应该让你的应用程序在被杀死之前在后台运行大约10分钟。

Answering my own question, hoping it would help people in the same situation. For people new to iOS development, "using a background service" simple means turning on the "Background Modes" option in the "Capabilities" tab of your target. That alone should give your app around 10 minutes life in the background before it gets killed.

但是,当应用程序转到后台时,我会使用 backgroundTimeRemaining要知道我剩下多少时间,它只是从180开始(以秒为单位,所以3分钟),然而,打印循环确实继续工作三分钟,这意味着需要手动编写应该发生的事情当达到时间时。

But, when the app goes to background, I use the "backgroundTimeRemaining" to know how much time I have left, it just starts at 180 (in sec, so 3 minutes), yet, the printing loop did continue to work passed three minutes, which means there is a need to manually code what should happen when the time is reached.

对于Multipeer Connectivity,这足以在应用程序进入后台时保持连接活动,并且它仍将接收所有消息/流而无需问题。

For Multipeer Connectivity, this is enough to maintain the connection alive when the app enters background, and it will still receive all messages/streams without a problem.

为了稳定起见,我做了一些清理工作如下:

For the sake of stability, I do some cleaning as follow:

在appDelegate.h中

In the appDelegate.h

@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask; //declaring a background task

在appDelegate.m

In the appDelegate.m

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^
                           {
                               //This is called 3 seconds before the time expires
                               //Here: Kill the session, advertisers, nil its delegates,
                               //      which should correctly send a disconnect signal to other peers
                               //      it's important if we want to be able to reconnect later,
                               //      as the MC framework is still buggy
                               [application endBackgroundTask:self.backgroundTask];
                               self.backgroundTask = UIBackgroundTaskInvalid; //Invalidate the background task
                           }];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Here: We should init back the session, start the advertising and set the delegates from scratch
    // This should allow the app to reconnect to the same session with more often than not
    self.backgroundTask = UIBackgroundTaskInvalid; //Here we invalidate the background task if the timer didn't end already
}

这篇关于通过BackgroundTask在后台维护多路连接会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆