当前位置: 首页 > 知识库问答 >
问题:

CKAsset未保存到CloudKit中-所有其他字段都已保存

楚茂实
2023-03-14

我有一个CloudKit应用程序,它基本上是一个带有一个额外功能的主细节设置。任何详细信息对象都可以标记为ActiveNote。当应用程序在iPad上时,只显示这个ActiveNote(没有用户交互)。该应用程序包括通知和订阅,所有数据都在专用数据库的自定义区域中。该应用程序运行良好,但有一个例外。

只有两种记录类型。所有数据都以cnote类型存储。当一个细节项目被选择显示在iPad上时,我会将该数据上传到一个类型为ActiveNote的记录中。ActiveNote数据仅由iPad用于填充其只读版本的详细信息视图。每当手机用户将记录标记为活动时,iPad就会动态变化。

如能提供任何指导,不胜感激。下面是保存到单个ActiveNote的代码。iOS 11,Xcode 9.3

func queryActiveNote() {

    recordZone = CKRecordZone(zoneName: "CNotes")

    let query = CKQuery(recordType: "ActiveNote", predicate: NSPredicate(format: "TRUEPREDICATE"))
    let sortDescriptor = NSSortDescriptor(key: "noteName", ascending: true)
    query.sortDescriptors = [sortDescriptor]

    privateDatabase.perform(query, inZoneWith: recordZone?.zoneID) { (results, error) in
        if error != nil {
            print("error querying database\(String(describing: error?.localizedDescription))")
        } else {

            guard results?.count > 0 else {return}
            print("results?.count is \(String(describing: results?.count))")

            self.activeNote = results![0]

            self.activeNote!["noteName"] = self.detailItem?["noteName"]

            //save a bunch of other fields too

            let tmpDir = NSTemporaryDirectory()
            let tmpFile = tmpDir.appending("test.png")
            let tmpFileURL = URL(fileURLWithPath: tmpFile)

            //Main queue to avoid runtime warning that image needs to be on main thread
            DispatchQueue.main.async {
                guard let tmpImage = self.imageView.image else {
                    print("guard let tmpImage failed")
                    return
                }

                guard let tmpImageData = UIImageJPEGRepresentation(tmpImage, 1.0) else {
                    print("guard let tmpImageData failed")
                    return
                }

                do {
                    try tmpImageData.write(to: tmpFileURL, options: .atomic)
                    print("the write of chosenImageData succeeded")
                } catch {
                    print("error writing chosenImageData to a file")
                }//do catch

                let asset : CKAsset = CKAsset(fileURL: tmpFileURL)
                self.activeNote!["noteImageData"] = asset

            }//main

            self.privateDatabase.save(self.activeNote!, completionHandler: { (record, error) in
                if error != nil {
                    print("privateDatabase error saving activeNote: \(String(describing: error?.localizedDescription))")
                } else {
                    print("modified activeNote record saved successfully")

                    DispatchQueue.main.async {
                        let ac = UIAlertController(title: nil , message: "Record was successfully saved as Active", preferredStyle: .alert)
                        let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
                        ac.addAction(okAction)
                        self.present(ac, animated: true, completion: nil)
                    }//main

                }//if error else
            })//save block

        }//if error else
    }//perform query block

}//queryActiveNote

共有1个答案

席言
2023-03-14

用户3069232的答案应该得到赞扬。上面的代码确实在图像文件保存完成之前将记录保存到CloudKit。我在do catch块中移动了CloudKit save块,这个过程成功了。因为我保证所有记录中都有一个图像(有一个默认值),所以以成功保存图像为条件进行CloudKit保存是可以的。

 类似资料:
  • 我在项目中使用Crudepository进行数据库操作。当我使用由Spring实现的保存方法更新持久化数据时,其他字段将被覆盖。例如,我只发送firstName进行更新,但lastName被转换为空字段。 简单地说,我用这样的实体调用保存方法: 我正在将此JSON发送到更新方法: Spring 将此 JSON 转换为 Rest api endpoint的成员。成员.java是: 如何防止覆盖其他字

  • 首先,我像这样扩展抽象用户: 然后参考如下其他模型: 这是形式: 这是视图: 我的问题是: > < li> 如何显示全名字段 以及如何通过应用程序数据的视图将这些字段保存到MyModel模型中的成员字段中? 更新 我更新了AbstractUser模型,因为我忘记添加first_name

  • 我想知道为什么我的铁轨没有拯救我的encrypted_password场。 这是我的UserController 这是我的用户模型 此外,我使用此函数保存它 有一件事我怀疑是我创造了我的第一次迁徙 然而,我将:password字段更改为:encrypted_password,它反映在表中。我已经坚持了两个小时了。我想知道是否有什么突出的地方。谢谢 日志显示,注入的数据减去加密密码 插入“用户”(“

  • 这个活动我有1个活动,3个片段。 在3个片段中,当复选框被选中或edittext不为空时,我有布尔值返回true。 当EditText不为空且我的复选框处于选中状态时,我想禁用此按钮。。 当我添加了像sexMan这样的东西。isChecked()到方法EditTextCompleted()我的应用程序崩溃。。。。 那么我如何将这些条件连接到一个地方呢? 复选框侦听器 编辑文本监听器 @在TextC

  • 我正在尝试在我的设计表单上添加一个额外的字段,如下所示: 我必须允许额外的字段,所以我创建了一个新的初始值设定项devise_permitted_parameters.rb,如下所示 输出如下: "email"=? LIMIT?[["email","wtgwg@gmail.com "], ["LIMIT",1]]SQL(1.1ms)插入到"用户"("email","encrypted_passwo

  • 问题内容: 如果我们为特定的设置,java如何为它设置时间,例如,如果我们将其设置为时区,它怎么知道确切的时区,它在哪里维护? 提前致谢 问题答案: 时区数据源是特定于实现的,但是我相信大多数实现都使用IANA数据库(即zoneinfo,aka tz,aka Olson,aka TZDB)。 请注意,PST实际上不是时区-它是时区的“一部分”。时区为“太平洋时间”,通常以ID标识。