我们的应用程序允许用户在 iPhone 上选择他们希望在 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 也会收到所有“取消”的文件传输。
最佳答案
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/