CS193p Notes - Lecture 13: Persistence
Tags: ios, swift
Published: 2020-07-26 16:40:57

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

```swift
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) ...

```swift
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](https://twitter.com/reallankinen)

- [Notes 1](https://dev.to/lankinen/cs193p-notes-lecture-1-course-logistics-and-introduction-to-swiftui-44ap)
- [Notes 2](https://dev.to/lankinen/cs193p-notes-lecture-2-mvvm-and-the-swift-type-system-406f)
- [Notes 3](https://dev.to/lankinen/cs193p-notes-lecture-3-reactive-ui-protocols-layout-5g8a)
- [Notes 4](https://dev.to/lankinen/cs193p-notes-lecture-4-grid-enum-optionals-4noj)
- [Notes 5](https://dev.to/lankinen/cs193p-notes-lecture-5-viewbuilder-shape-viewmodifier-fgh)
- [Notes 6](https://dev.to/lankinen/cs193p-notes-lecture-6-animation-1a8f)
- [Notes 7](https://dev.to/lankinen/cs193p-notes-lecture-7-multithreading-emojiart-2ak3)
- [Notes 8](https://dev.to/lankinen/cs193p-notes-lecture-8-gestures-json-1f3m)
- [Notes 9](https://dev.to/lankinen/cs193p-notes-lecture-9-data-flow-26e)
- [Notes 10](https://dev.to/lankinen/cs193p-notes-lecture-10-navigation-textfield-3ei9)
- [Notes 11](https://dev.to/lankinen/cs193p-notes-lecture-11-picker-2hgh)
- [Notes 12](https://dev.to/lankinen/cs193p-notes-lecture-12-core-data-eci)
- [**Notes 13**](https://dev.to/lankinen/cs193p-notes-lecture-13-persistence-2jeh)
- [Notes 14](https://dev.to/lankinen/cs193p-notes-lecture-14-uikit-integration-1p7m)

[Originally published here](https://www.notion.so/lankinen/CS193p-2020-iOS-development-course-from-Stanford-81c094238e9d471bbf059ea8d83b3e15)