DEV Community

Lankinen
Lankinen

Posted on

CS193p Notes - Lecture 13: Persistence

  • UserDefaults
    • Simple. limited. small.
  • Codable/JSON
    • Clean way to turn almost any data structure into an interoperable/storable format
  • UIDocument
    • This is really the way to do things when you have a true document like EmojiArt has
    • UIKit compatibility code is required (no SwiftUI interface to it yet [THIS IS SwiftUI COURSE])
  • Core Data
    • Powerful. Object-oriented. Elegant SwiftUI integration.
  • Cloud Kit
    • Storing data into a database in the cloud
    • Has UserDefaults-like thing
    • Plays nicely with Core Data

Cloud Kit

  • Record Type = like a class or struct
  • Fields = like vars in a class or struct
  • Record = an "instance" of a Record Type
  • Reference = a "pointer" to another Record
  • Database = a place where Records are stored
  • Zone = a sub-area of a Database
  • Container = collection of Databases
  • Query = a Database search
  • Subscription = a "standing Query" which sends push notifications when changes occur

iCloud needs to be turned on from Project Settings under Capabilities tab. Choose CloudKit from Services. Click CloudKit Dashboard.

let db = CKContainer.default.public/shared/privateCloudDatabase
let tweet = CKRecord("Tweet")
tweet["text"] = "140 characters of pure joy"
let tweeter = CKRecord("TwitterUser")
tweet["tweeter"] = CKReference(record: tweeter, action: .deleteSelf)
db.save(tweet) { (savedRecord: CKRecord?, error: NSError?) -> Void in
  if error == nil {
    // hooray!
  } else if error?.errorCode == CKErrorCode.NotAuthenticated.rawValue {
    // tell user he or she has to be logged in to iCloud for this to work
  } else {
    // report other errors (there are 29 different CKErrorCodes!)
  }
}
  • File System
    • Unix file system
    • an app can read and write only it's own "sandbox"
    • Sandbox is useful because
      • No one else can damage app
      • other apps can't view your app's data
      • by deleting the sandbox, it's simple to get rid of all the data
    • Sandbox contains
      • Application directory - Your executable, .jpgs, etc.; not writable
      • Documents directory - Permanent storage created by and always visible to the user
      • Application Support directory - Permanent storage not seen directly by the user
      • Caches directory - Store temporary files here (this is not backed up)
      • Other directories (see documentation) ...
let predicate = NSPredicate(format: "text contains %@", searchString)
let query = CKQuery(recordType: "Tweet", predicate: predicate)
db.perform(query) { (records: [CKRecord]?, error: NSError?) in
  if error == nil {
    // records will be an array of matching CKRecords
  } else if error?.errorCode == CKErrorCode.NotAuthenticated.rawValue {
    // tell user he or she has to be logged in to iCloud for this to work!
  } else {
    // report other errors (there are 29 different CKErrorCodes!)
  }
}

@RealLankinen

Originally published here

Top comments (0)