Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dengsauve/94ddbf0d4cb79cd9ac1906522cb8581e to your computer and use it in GitHub Desktop.
Save dengsauve/94ddbf0d4cb79cd9ac1906522cb8581e to your computer and use it in GitHub Desktop.
Saving to the Cloud - ViewController.swift created by dengsauve - https://repl.it/@dengsauve/Saving-to-the-Cloud-ViewControllerswift
/* Notes based on the tutorial here: https://www.youtube.com/watch?v=2Y45vk7d_Bg
Covered Here:
- Saving to iCloud with CloudKit
- Querying iCloud with CloudKit
- Pull Motion Refresh
- Using a cell programatically instead of dequeing a model
Useful for quick coding
Must have the following:
- CloudKit Enabled
Project > Capabilities > iCloud > CloudKit
- Import CloudKit
*/
let database = CKContainter.default().privateCloudDatabase
// Kilo runs into authentication issues, must sign into iPhone with apple account
let notes = [CKRecord]
override func viewDidLoad() {
super.viewDidLoad()
// Code for Pull Refresh
// Create refreshControl
let refreshControl = UIRefreshControl()
// Set the text that displays when activated
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
// Set the action that occurs when refresh is pulled
refreshControl.addTarget(self, action: #selector(queryDatabase), for: .valueChanged)
// Set the tableView refreshControl to your created refreshControl
self.tableView.refreshControl = refreshControl
// Get existing data
queryDatabase()
}
//...
// Save a Note to the cloud database
func saveToCloud(note: String){
// Create a new CloudKit Record type: "Note"
let newNote = CKRecord(recordType: “Note”)
// Save the note to the CloudKit Record key: "Content"
newNote.setValue(note, forKey: “content”)
// Save the CloudKit Record to the Database
database.save(newNote) { (record _) in
// Check that the record isn't nil
guard record != nil else {return}
print("saved record with note")
}
}
// Query all notes from cloud database
// Needs @objc to be selectored
@objc func queryDatabase(){
let query = CKQuery(recordType: "Note", predicate: NSPredicate(value: true))
// Not segregating data -> no zones
database.perform(query, inZoneWith: nil) { (records, _) in
guard let records = records else {return}
let sortedRecords = records.sorted(by: { $0.creationDate! > $1.creationDate! })
self.notes = sortedRecords
DispatchQueue.main.async {
// gets rid of the refresh spinner
self.tableView.refreshControl?.endRefreshing()
self.tableView.reloadData()
}
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let note = notes[indexPath.row].value(forKey:"content") as! String
cell.textLabel?.text = note
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment