OzzieChen
OzzieChen

Reputation: 127

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.

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!!

Upvotes: 2

Views: 2308

Answers (1)

Ben
Ben

Reputation: 3862

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 enter image description here

Upvotes: 10

Related Questions