简体   繁体   English

iOS swift:使用coredata(cloudkit)存储缓存

[英]iOS swift: Store cache using coredata (cloudkit)

I'm learning to use cloudkit to save and fetch records, but I got confused about saving cache to coredata. 我正在学习使用cloudkit来保存和获取记录,但我对将缓存保存到coredata感到困惑。

For example, I fetched several records and display a few attributes of this record type(say, A, C and F) using a tableview. 例如,我使用tableview获取了几条记录并显示了这种记录类型的一些属性(比如A,C和F)。 And when I click a cell, it'll show this record's detail ( all attributes of this record: ABCDEF, but don't including reference attributes record). 当我单击一个单元格时,它将显示此记录的详细信息(此记录的所有属性:ABCDEF,但不包括引用属性记录)。 I was wondering should I store these things into coredata when I fetched the record at the first time: "ACF and recordID" ? 当我第一次拿到记录时,我想知道是否应将这些东西存入coredata:“ACF和recordID”? And when user click to see detail, I fetch again using recordID? 当用户点击查看详细信息时,我使用recordID再次获取? And the key point is what attribute type should I use to store CKRecordID/CKRecord? 关键是我应该使用什么属性类型来存储CKRecordID / CKRecord?

I know that I could store things like image to local cache file(also confusing..), but it's not a persistent store right? 我知道我可以将像image这样的东西存储到本地缓存文件中(也令人困惑..),但它不是持久存储吗? And the reason why I don't store record's all attributes directly is because this record is an "invitation", only if user choose to accept it, it will download all attributes including some reference type attributes. 而且我没有直接存储记录的所有属性的原因是因为此记录是“邀请”,只有当用户选择接受它时,它才会下载所有属性,包括一些引用类型属性。

Any help would be helpful, thank you!! 任何帮助都会有所帮助,谢谢!

You should archive only the system fields when cacheing, like this: 您应该在缓存时仅存档系统字段,如下所示:

private func dataFromRecord(record:CKRecord) -> NSData{
    let archivedData = NSMutableData()
    let archiver = NSKeyedArchiver(forWritingWithMutableData: archivedData)
    archiver.requiresSecureCoding = true
    record.encodeSystemFieldsWithCoder(archiver)
    archiver.finishEncoding()
    return archivedData
}

private func recordFromData(archivedData:NSData) -> CKRecord?{
    let unarchiver = NSKeyedUnarchiver(forReadingWithData: archivedData)
    unarchiver.requiresSecureCoding = true
    let unarchivedRecord = CKRecord(coder: unarchiver)
    return unarchivedRecord
}

31:10 WWDC 2015 - Session 715 - iOS, OS X 31:10 WWDC 2015 - Session 715 - iOS,OS X. 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM