From 5e6b90b8c24d9d954520d656a108cb614dee1980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Sun, 13 Dec 2015 17:21:12 +0100 Subject: [PATCH] Add local fetch method --- AppNet/Networking.swift | 16 +++++++++++----- AppNet/ViewController.swift | 6 +++--- AppNet/global.json | 30 ++++++++++++++++++++++++++++++ Demo.xcodeproj/project.pbxproj | 4 ++++ 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 AppNet/global.json diff --git a/AppNet/Networking.swift b/AppNet/Networking.swift index 45af28a5..fb3abeea 100644 --- a/AppNet/Networking.swift +++ b/AppNet/Networking.swift @@ -15,15 +15,21 @@ class Networking { let request = NSURLRequest(URL: NSURL(string: AppNetURL)!) session.dataTaskWithRequest(request, completionHandler: { data, response, error in if let data = data, json = (try? NSJSONSerialization.JSONObjectWithData(data, options: [])) as? [String: AnyObject] { - Sync.changes(json["data"] as! Array, - inEntityNamed: "Data", - dataStack: self.dataStack, - completion: { error in - completion(error) + Sync.changes(json["data"] as! Array, inEntityNamed: "Data", dataStack: self.dataStack, completion: { error in + completion(error) }) } else { completion(error) } }).resume() } + + func fetchLocalItems(completion: (NSError?) -> Void) { + guard let url = NSURL(string: "global.json"), filePath = NSBundle.mainBundle().pathForResource(url.URLByDeletingPathExtension?.absoluteString, ofType: url.pathExtension) else { fatalError() } + guard let data = NSData(contentsOfFile: filePath) else { fatalError() } + let json = try! NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject] + Sync.changes(json["data"] as! Array, inEntityNamed: "Data", dataStack: self.dataStack, completion: { error in + completion(error) + }) + } } diff --git a/AppNet/ViewController.swift b/AppNet/ViewController.swift index 1394158d..4813e33b 100644 --- a/AppNet/ViewController.swift +++ b/AppNet/ViewController.swift @@ -39,7 +39,7 @@ class ViewController: UITableViewController { // MARK: Private methods - private func changeNotification(notification: NSNotification) { + func changeNotification(notification: NSNotification) { let updatedObjects = notification.userInfo?[NSUpdatedObjectsKey] let deletedObjects = notification.userInfo?[NSDeletedObjectsKey] let insertedObjects = notification.userInfo?[NSInsertedObjectsKey] @@ -49,7 +49,7 @@ class ViewController: UITableViewController { print("insertedObjects: \(insertedObjects)") } - private func fetchNewData() { + func fetchNewData() { networking.fetchItems { _ in self.fetchCurrentObjects() @@ -57,7 +57,7 @@ class ViewController: UITableViewController { } } - private func fetchCurrentObjects() { + func fetchCurrentObjects() { let request = NSFetchRequest(entityName: "Data") request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: true)] items = (try! dataStack.mainContext.executeFetchRequest(request)) as! [Data] diff --git a/AppNet/global.json b/AppNet/global.json new file mode 100644 index 00000000..52817a45 --- /dev/null +++ b/AppNet/global.json @@ -0,0 +1,30 @@ +{ + "meta": { + "min_id": "66582440", + "code": 200, + "max_id": "66582467", + "more": true + }, + "data": [ + { + "created_at": "2015-12-13T15:43:35Z", + "text": "Hei there", + "id": "66582467", + "user": { + "username": "sportsmole", + "id": "213778", + "name": "Sports Mole" + } + }, + { + "created_at": "2015-12-13T15:43:25Z", + "text": "Interesting tweet", + "id": "66582466", + "user": { + "username": "observer", + "id": "293913", + "name": "kayotinsky" + } + } + ] +} diff --git a/Demo.xcodeproj/project.pbxproj b/Demo.xcodeproj/project.pbxproj index a2b9e186..f1c20445 100644 --- a/Demo.xcodeproj/project.pbxproj +++ b/Demo.xcodeproj/project.pbxproj @@ -74,6 +74,7 @@ 141894A81BDD64AF00EE52CE /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141894A41BDD64AF00EE52CE /* User.swift */; }; 141894A91BDD64AF00EE52CE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141894A51BDD64AF00EE52CE /* ViewController.swift */; }; 14959D141C065350004EF790 /* notes_with_user_id.json in Resources */ = {isa = PBXBuildFile; fileRef = 14959D131C065350004EF790 /* notes_with_user_id.json */; }; + 14D292681C1DD07E00467F6B /* global.json in Resources */ = {isa = PBXBuildFile; fileRef = 14D292671C1DD07E00467F6B /* global.json */; }; 8C1190CE4B3821813B6A3421 /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E482492041434DB374B1948 /* Pods.framework */; }; BD2A2CB4A6B4EC694D5E358A /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E482492041434DB374B1948 /* Pods.framework */; }; E9FABD68F0CA454B8D54104E /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E482492041434DB374B1948 /* Pods.framework */; }; @@ -173,6 +174,7 @@ 14C0AF811BD6D4230009ECBE /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = ""; }; 14C0AF821BD6D4230009ECBE /* CONTRIBUTING.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CONTRIBUTING.md; sourceTree = ""; }; 14C0AF831BD6D4230009ECBE /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + 14D292671C1DD07E00467F6B /* global.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = global.json; sourceTree = ""; }; 9E482492041434DB374B1948 /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; }; C13281341A77530FC5AE626A /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = ""; }; E80F7331DFE41909E28F2702 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = ""; }; @@ -327,6 +329,7 @@ 1418948C1BDD635800EE52CE /* LaunchScreen.storyboard */, 1418948F1BDD635800EE52CE /* Info.plist */, 141894871BDD635800EE52CE /* AppNet.xcdatamodeld */, + 14D292671C1DD07E00467F6B /* global.json */, ); path = AppNet; sourceTree = ""; @@ -541,6 +544,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 14D292681C1DD07E00467F6B /* global.json in Resources */, 1418948B1BDD635800EE52CE /* Assets.xcassets in Resources */, 1418948E1BDD635800EE52CE /* LaunchScreen.storyboard in Resources */, );