iOS swift: Store cache using coredata (cloudkit)

家住魔仙堡 提交于 2019-12-09 21:13:55

问题


I'm learning to use cloudkit to save and fetch records, but I got confused about saving cache to coredata.

For example, I fetched several records and display a few attributes of this record type(say, A, C and F) using a tableview. And when I click a cell, it'll show this record's detail ( all attributes of this record: A B C D E F, but don't including reference attributes record). I was wondering should I store these things into coredata when I fetched the record at the first time: "A C F and recordID" ? And when user click to see detail, I fetch again using recordID? And the key point is what attribute type should I use to store 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? 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!!


回答1:


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



来源:https://stackoverflow.com/questions/28812465/ios-swift-store-cache-using-coredata-cloudkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!