【问题标题】:Is it possible save images separate from the record they are used in inside CloudKit?是否可以将图像与它们在 CloudKit 中使用的记录分开保存?
【发布时间】:2020-08-28 02:11:13
【问题描述】:

我想让应用程序的用户为提要创建帖子,但我想单独存储帖子图像以加快加载时间。是否可以将图像存储在一个记录中,然后在我获取帖子的同时将它们与它们所属的帖子一起获取?如果是这样,我将如何在 SwiftUI 中实现它?

我已经弄清楚如何保存/获取帖子而不是图像。

【问题讨论】:

    标签: ios swiftui cloudkit


    【解决方案1】:

    是的,您当然可以这样做。我将假设您的 recordType 是一个 Post,其中包含您的提要中某个项目的内容。

    我可以想到两种选择。无论您采用哪种方法,您都应该在 CloudKit 上将图像存储为 CKAsset

    == 选项 1 ==

    您可以在Post 上拥有一个类型为Asset List 的字段,并使用CKAsset 记录来加载它,这将是您的图像。当您访问每个CKAsset 时,它会在您拥有原始Post 记录后下载。这可能是最干净的方法。

    == 选项 2 ==

    创建另一个名为ImagerecordType,它可以将ReferenceCKReference)类型的字段返回到Post。因此,在创建每个 Image 时,都会为其分配一个 Post 并指向它。

    当您查询您的Post 记录时,您还会查询连接到Post 的所有Image 记录。可以这样做:

    let query = CKQuery(recordType: "Image", predicate: NSPredicate(format: "post = %@", CKRecord.Reference(recordID: CKRecord.ID(recordName: yourPostRecordName), action: .deleteSelf)))
    let operation = CKQueryOperation(query: query)
      
    var images = [CKRecord]()
    operation.recordFetchedBlock = { record in
      //Load up all the images
      images.append(record)
    }
    
    operation.queryCompletionBlock = { cursor, error in
      //All the image records have been downloaded, or there was an error
    }
    
    yourContainerDatabase.add(operation)
    

    【讨论】:

    • 非常感谢!这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多