functionality for loading saved data from Core Data context
This commit is contained in:
parent
1d919465d5
commit
0e3e5f0ba3
|
@ -11,6 +11,7 @@
|
|||
F35ABF8C2233E65F00206C90 /* Commit+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = F35ABF8A2233E65F00206C90 /* Commit+CoreDataProperties.swift */; };
|
||||
F35ABF8E2233F5CE00206C90 /* GithubAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = F35ABF8D2233F5CE00206C90 /* GithubAPI.swift */; };
|
||||
F35ABF912234BDE000206C90 /* CodingUserInfoKey+Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = F35ABF902234BDE000206C90 /* CodingUserInfoKey+Util.swift */; };
|
||||
F35ABFD22235548400206C90 /* UIViewController+Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = F35ABFD12235548400206C90 /* UIViewController+Util.swift */; };
|
||||
F3A237752233870A00986BB6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F3A2376B2233870A00986BB6 /* Assets.xcassets */; };
|
||||
F3A237772233870A00986BB6 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F3A2376F2233870A00986BB6 /* LaunchScreen.storyboard */; };
|
||||
F3A237782233870A00986BB6 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3A237702233870A00986BB6 /* AppDelegate.swift */; };
|
||||
|
@ -25,6 +26,7 @@
|
|||
F35ABF8A2233E65F00206C90 /* Commit+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Commit+CoreDataProperties.swift"; sourceTree = "<group>"; };
|
||||
F35ABF8D2233F5CE00206C90 /* GithubAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GithubAPI.swift; sourceTree = "<group>"; };
|
||||
F35ABF902234BDE000206C90 /* CodingUserInfoKey+Util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CodingUserInfoKey+Util.swift"; sourceTree = "<group>"; };
|
||||
F35ABFD12235548400206C90 /* UIViewController+Util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+Util.swift"; sourceTree = "<group>"; };
|
||||
F3A2375422335D8100986BB6 /* Github Commits.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Github Commits.app"; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
F3A2376B2233870A00986BB6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||
F3A2376D2233870A00986BB6 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
|
@ -51,6 +53,7 @@
|
|||
isa = PBXGroup;
|
||||
children = (
|
||||
F35ABF902234BDE000206C90 /* CodingUserInfoKey+Util.swift */,
|
||||
F35ABFD12235548400206C90 /* UIViewController+Util.swift */,
|
||||
);
|
||||
path = Extensions;
|
||||
sourceTree = "<group>";
|
||||
|
@ -224,6 +227,7 @@
|
|||
F35ABF8B2233E65F00206C90 /* Commit+CoreDataClass.swift in Sources */,
|
||||
F3A2377A2233870A00986BB6 /* HomeViewController.swift in Sources */,
|
||||
F35ABF8C2233E65F00206C90 /* Commit+CoreDataProperties.swift in Sources */,
|
||||
F35ABFD22235548400206C90 /* UIViewController+Util.swift in Sources */,
|
||||
F35ABF8E2233F5CE00206C90 /* GithubAPI.swift in Sources */,
|
||||
F3A237842233C30200986BB6 /* GithubCommits.xcdatamodeld in Sources */,
|
||||
);
|
||||
|
|
|
@ -29,9 +29,15 @@ class HomeViewController: UITableViewController {
|
|||
DispatchQueue.global(qos: .background).async { [weak self] in
|
||||
self?.fetchCommits()
|
||||
}
|
||||
|
||||
loadSavedData()
|
||||
}
|
||||
|
||||
|
||||
override func numberOfSections(in tableView: UITableView) -> Int {
|
||||
return 1
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return commits.count
|
||||
}
|
||||
|
@ -40,7 +46,7 @@ class HomeViewController: UITableViewController {
|
|||
let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
|
||||
let commit = commits[indexPath.row]
|
||||
|
||||
cell.textLabel?.text = commit.sha
|
||||
cell.textLabel?.text = commit.message
|
||||
cell.detailTextLabel?.text = commit.date.description
|
||||
|
||||
return cell
|
||||
|
@ -55,7 +61,7 @@ class HomeViewController: UITableViewController {
|
|||
let data = try Data(contentsOf: commitsURL)
|
||||
parseCommits(fromJSON: data)
|
||||
} catch {
|
||||
fatalError("Error fetching data from Github API: \(error.localizedDescription)")
|
||||
showError(title: "Error fetching data from Github API", message: error.localizedDescription)
|
||||
}
|
||||
} else {
|
||||
fatalError("Error constructing URL to GitHub API")
|
||||
|
@ -74,15 +80,32 @@ class HomeViewController: UITableViewController {
|
|||
decoder.userInfo[mangedObjectContextKey] = managedObjectContext
|
||||
|
||||
do {
|
||||
commits = try decoder.decode([Commit].self, from: json)
|
||||
print("Received \(commits.count) new commits")
|
||||
let commits = try decoder.decode([Commit].self, from: json)
|
||||
print("Parsed \(commits.count) new commits from API")
|
||||
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
// TODO: more with commits here?
|
||||
self?.saveData()
|
||||
self?.loadSavedData()
|
||||
}
|
||||
} catch {
|
||||
print("Error while parsing `Commit` json data:\n\n\(error.localizedDescription)")
|
||||
showError(title: "Error while parsing `Commit` json data", message: error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func loadSavedData() {
|
||||
let fetchRequest = Commit.createFetchRequest()
|
||||
let sort1 = NSSortDescriptor(key: "date", ascending: false)
|
||||
let sort2 = NSSortDescriptor(key: "message", ascending: true)
|
||||
|
||||
fetchRequest.sortDescriptors = [sort1, sort2]
|
||||
|
||||
do {
|
||||
commits = try dataContainer.viewContext.fetch(fetchRequest)
|
||||
print("Fetched \(commits.count) from our persistent container context")
|
||||
tableView.reloadData()
|
||||
} catch {
|
||||
showError(title: "Error while fetching data from persistent container context", message: error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,7 +117,7 @@ class HomeViewController: UITableViewController {
|
|||
print("Saving persistent data container view context")
|
||||
try dataContainer.viewContext.save()
|
||||
} catch {
|
||||
print("Error while saving persistent data store: \(error.localizedDescription)")
|
||||
showError(title: "Error while saving persistent data store", message: error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
//
|
||||
// UIViewController+Util.swift
|
||||
// Github Commits
|
||||
//
|
||||
// Created by Brian Sipple on 3/10/19.
|
||||
// Copyright © 2019 Brian Sipple. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIViewController {
|
||||
func showError(
|
||||
title: String,
|
||||
message: String = "",
|
||||
completionHandler: ((UIAlertAction) -> Void)? = nil
|
||||
) {
|
||||
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
||||
|
||||
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: completionHandler))
|
||||
|
||||
present(alertController, animated: true)
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@ import CoreData
|
|||
|
||||
extension Commit {
|
||||
|
||||
@nonobjc public class func fetchRequest() -> NSFetchRequest<Commit> {
|
||||
@nonobjc public class func createFetchRequest() -> NSFetchRequest<Commit> {
|
||||
return NSFetchRequest<Commit>(entityName: "Commit")
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue