如何在iOS上获取所有长期操作 [英] How to fetch all long-lived operations on iOS

查看:95
本文介绍了如何在iOS上获取所有长期操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我


  1. 创建寿命较长的CKModifyRecordsOperation在iPhone上处于飞行模式(没有互联网连接)时

  2. 然后通过双击主屏幕按钮并轻扫应用程序退出创建CKModifyRecordsOperation的应用程序

  3. 禁用飞行模式并通过wifi网络连接互联网(仍然退出应用程序)

我可以看到(在CloudKit仪表板中)该操作已发送到iCloud服务器并已正确执行。
但是,当我随后重新打开我的应用程序并在同一CKContainer上调用 fetchAllLongLivedOperationIDs(...)时,我得到( no 错误和) 0个操作的列表。

I can see (in the CloudKit dashboard) that the operation was sent to the iCloud servers and executed properly. But when I then reopen my app and call fetchAllLongLivedOperationIDs(...) on the same CKContainer, I get (no error and) a list of 0 operations.

-> 如何检索在其中创建的操作(1。 )

我正在一个简单的CRUD应用程序中试用CloudKit(可以在GitHub上找到)。该应用程序由一个macOS应用程序和一个iOS应用程序组成。这两个应用程序共享大多数CloudKit代码。

I am trying out CloudKit in a simple CRUD application (source can be found on GitHub). The application consists of a macOS app and a iOS app. The two applications share most of the CloudKit code.

参考文档指出,从fetchAllLongLivedOperationIDs调用返回的数组为:

The reference docs state that the array returned from the fetchAllLongLivedOperationIDs call is:


一个数组,其中包含所有活动的长期
操作的标识符。如果长期操作被取消或完成,则
不再是活动操作,并且其标识符也不会包含在此数组中。

An array containing the identifiers for all the active long-lived operations. If a long-lived operation is canceled or completed, it is no longer an active operation, and its identifier will not be included in this array.

如果应用成功
接收到完成回调,则操作完成。

An operation is complete if the app successfully receives the completion callback.

当我添加一个长期的这样的操作(但未连接到互联网):

When I add a long-lived operation like this (while not connected to the internet):

let deletion = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: [someRecordID])  
deletion.isLongLived = true  
deletion.start()

可以在我的示例应用程序中此处找到此代码在github

this code can be found here in my sample application on github

然后退出我的应用程序,连接到互联网并重新打开我的应用程序,如下所示:(GitHub链接)

And then quit my app, connect to the internet and reopen my app, the following fetch: (GitHub link)

cloudContainer.fetchAllLongLivedOperationIDs { operationIDs, error in
  if let error = error {print(error)}
  print(operationIDs.count)
}

在iPhone上尝试操作时返回0,并且没有错误。

returns 0 operations and no error when I try it on my iPhone.

就像我之前说过的,还要为macOS应用使用完全相同的代码。而且令人惊讶的是,在macOS上,当我做完全相同的事情时,它确实会返回1操作。

Like I said before, I also use the exact same code for a macOS app. And surprisinly, on macOS it does return 1 operation when I do the exact same things.

好,所以我想:也许由于某些原因,在iOS中操作完成了表示:该应用程序未运行时(因为我自己退出了),因此该应用程序成功地收到了完成回调(如文档中所述)。因此,我重复了整个序列(在摘要中进行了描述),但是我手动保存了operationID,然后在重新打开应用程序时,使用 fetchLongLivedOperation(withID:...,completeHandler)通过其ID来获取操作:...)来查看操作是否确实在后台完成。但是,不,我恢复了操作,并且仍被标记为活动状态(或如其在debugDescription中所写的那样出色)(GitHub链接)
因此,由于某些原因,该操作仍处于活动状态,但是我无法使用 fetchAllLongLivedOperationIDs 检索该操作。

OK so I thought: maybe for some reason, in iOS the operation completes (which means: the app successfully receives the completion callback, as described in the documentation) while the app is not running (because I quit it myself). So I repeat this whole sequence (described in the summary) but I manually save the operationID, and then when I reopen the app, I fetch the operation by its ID with fetchLongLivedOperation(withID: ..., completionHandler: ...) to see if the operation was really completed in the background. But no, I get back my operation and it is still marked as active (or outstanding as it is written in its debugDescription) (GitHub link). So for some reason the operation is still active but I am not able to retreive it using fetchAllLongLivedOperationIDs.

这是一个已知的错误还是我做错了什么?

Is this a known bug or am I doing something wrong?

我正在使用


  • macOS 10.12(16A323)

  • <在iPhone 5上strong> iOS 10.0.1

  • macOS 10.12 (16A323) on a MacBook Pro 15" (late 2013)
  • iOS 10.0.1 on an iPhone 5

推荐答案

在您的代码上,它看起来像不是在长期CKOperations上设置了 longLivedOperationWasPersistedBlock

Taking a look at your code, it does not look like you set the longLivedOperationWasPersistedBlock on the long-lived CKOperations.

longLivedOperationWasPersistedBlock 文档


如果您的应用在调用此块之前退出,则<$的结果中将不包含长期操作的标识符c $ c> f etchAllLongLivedOperationIDs(completionHandler:)方法。

如果您是在长期使用之前强制退出应用程序操作持续存在(您目前不检查该操作-建议您指定该阻止和日志记录),这确实可能导致问题。

If you are force-quitting the app before the long-lived operation is persisted (which you currently don't check for - I'd suggest specifying that block and logging), that could indeed cause the issue.

这篇关于如何在iOS上获取所有长期操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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