ios - watchOS 2.0 - 无法取消 WCSessionFileTransfer

标签 ios iphone watchkit apple-watch watchos-2

我们的应用程序允许用户在 iPhone 上选择他们希望在 watch 应用程序中显示的记录。

它是这样工作的:

  • 用户在他们的 iPhone
  • 上点击记录上的“添加到观看”
  • 新版本的 watch 数据库生成并发送到 watch
  • watch 应用程序接收并保存文件并更新其界面

  • 一个新的数据库文件被发送到 watch 并针对每次更改进行处理。如果 watch 是醒着的,这很好,因为它会给用户实时更新,但如果 watch 在用户进行 7 次更改时处于 sleep 状态,这意味着 watch 一醒来就接受并处理 7 个新文件。

    我们真的只关心 watch 数据库的最新版本,所以我正在尝试取消所有旧的未完成文件传输。

    代码:

    在 iPhone 上,每次从监视数据库中添加/删除记录时,我们都会尝试(不成功)取消挂起的文件传输,然后将最新的数据库文件排队:
    // create watch database and store it at self.urlToDatabase
    [self generateNewWatchDatabase];
    
    if ([WCSession isSupported])
    {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    
        // this is the problem - cancel doesn't seem to do anything
        for (WCSessionFileTransfer *fileTransfer in session.outstandingFileTransfers)
            [fileTransfer cancel];
    
        [session transferFile:self.urlToDatabase metadata:nil]; 
    }
    

    在上面的代码中,调用 [fileTransfer cancel]成功删除 WCSessionFileTransfer来自 session.outstandingFileTransfers 的对象, 但是 didReceiveFile下面仍然被多次调用。

    在 watch 上接受文件:
    - (void)session:(WCSession *)session didReceiveFile:(WCSessionFile *)file
    {
        // this method gets called once for every time -transferFile:metadata: is called above,
        // even after cancelling outstanding file transfers
        [self replaceDatabaseWithFile:file];
        [self refreshItemsTable];
    }
    

    我们如何取消未完成的文件传输?

    编辑

    根据@ccjensen 的建议,我在用户向 watch 添加/删除记录时触发的方法中尝试了以下操作:
    // store a reference to the file transfer and immediately cancel it
    WCSessionFileTransfer *transfer = [session transferFile:self.urlToDatabase metadata:nil];
    [transfer cancel];
    

    这仍然会导致文件被发送到 watch ,而不是像预期的那样取消它。

    我还尝试了以下方法:
  • 杀死 watch 应用程序(按住侧面按钮直到出现“关机”,然后再次按住)
  • 从 iPhone 添加/删除记录
  • 重启 watch 应用

  • 即使在这种情况下, watch 也会收到所有“取消”的文件传输。

    最佳答案

    documentation对于取消方法说:

    Use this method to cancel a file transfer before it completes. If the file has already been transferred, calling this method has no effect.



    因此,听起来取消是“尽力而为”,并且可能最终无法在所有情况下取消它们,尤其是在文件已经传输的情况下。

    您是否看到它永远不起作用,即使您立即调用取消(尝试在不运行 watch 应用程序的情况下进行测试,因为这似乎加快了传输速度)?

    关于ios - watchOS 2.0 - 无法取消 WCSessionFileTransfer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32834643/

    相关文章:

    iOS swift 3 编码 tableview 编辑按钮使用 UIButton 而不是 UIBarButton

    ios - 推送通知配置文件

    iphone - 尝试使用 animateWithDuration 时找不到方法 '+animateWithDuration:delay:options:animations:'

    iPhone - 这会泄漏吗?

    ios - Apple Watch Kit 无法获取图像

    ios - 应用程序在模拟器中运行但不在 Apple Watch 上

    ios - HTTPBody - Swift POST 请求 - 推特

    iphone - 应用程序在后台时出现后台问题

    push-notification - 如何从不同的通知启动Apple Watch应用程序的不同屏幕?

    android - React Native 广播并发现附近的设备