【问题标题】:CKQueryOperation not returning error when device offline设备离线时 CKQueryOperation 不返回错误
【发布时间】:2023-03-17 22:28:01
【问题描述】:

我正在尝试在我的 CloudKit 数据库上使用 CKQueryOperation,而不是 performQuery

两者都有效,但是当使用 CKQueryOperation 时,设备离线时我没有收到错误消息,但在使用 performQuery 时会出现错误

这是我的performQuery 示例,数据库是我的CKDatabase

database.performQuery(q, inZoneWithID: nil) { (records:[CKRecord]?, error:NSError?) in
    if error != nil {
        print(error!.localizedDescription)
        return
    }
}

设备离线时报错,让我提示用户。错误是

The internet connection appears to be offline

但是,当我使用 CKQueryOperation 时,我没有收到任何错误

let p = NSPredicate(format:"recordID IN %@", student.courses)
let q = CKQuery(recordType: String(Course), predicate: p)

let queryOperation = CKQueryOperation(query: q)

queryOperation.recordFetchedBlock = { record in
    // not called without network connection - doesn't enter scope
    print(record)
}


queryOperation.queryCompletionBlock = { (cursor: CKQueryCursor?, error: NSError?) in
    // not called without network connection - doesn't enter scope
    print(cursor)
    print(error)
}

database.addOperation(queryOperation)

通过连接,我收到了两种方法的数据,因此它按预期工作。

使用CKQueryOperation 时如何/在哪里得知错误?

谢谢

【问题讨论】:

    标签: ios cloudkit


    【解决方案1】:

    像往常一样,我发布赏金并在接下来的一两个小时内找到答案。不知道我最初是如何错过this,但它包含我正在寻找的答案。

    所以通过添加这一行

    queryOperation.qualityOfService = .UserInitiated
    

    幕后发生了一些变化,我们有一些不错的动作

    queryOperation.queryCompletionBlock = { (cursor: CKQueryCursor?, error: NSError?) in
        // We get an error message... Finally !!
        print(error)
    }
    

    在 Apple Docs 中也找不到任何提示。

    【讨论】:

    • 我还发现timeoutIntervalForResource的操作默认值是7天......所以可能需要一周的时间才能放弃请求,即使听起来很傻。我把它改成了 6 秒,瞧
    • 看起来超时间隔是deprecated,但幸运的是 qualityOfSetting 仍然有效。现在是 Swift 3 中的 .userInitiated
    • 在添加 .qualityOfService 之前,我的 .queryCompletionBlock 根本没有被调用。谢谢!
    猜你喜欢
    • 2012-05-27
    • 1970-01-01
    • 2017-03-13
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    相关资源
    最近更新 更多