Skip to content

Commit

Permalink
Add local fetch method
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Dec 13, 2015
1 parent 69b99c4 commit 5e6b90b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
16 changes: 11 additions & 5 deletions AppNet/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
6 changes: 3 additions & 3 deletions AppNet/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -49,15 +49,15 @@ class ViewController: UITableViewController {
print("insertedObjects: \(insertedObjects)")
}

private func fetchNewData() {
func fetchNewData() {
networking.fetchItems { _ in
self.fetchCurrentObjects()

self.refreshControl?.endRefreshing()
}
}

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]
Expand Down
30 changes: 30 additions & 0 deletions AppNet/global.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
4 changes: 4 additions & 0 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -173,6 +174,7 @@
14C0AF811BD6D4230009ECBE /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
14C0AF821BD6D4230009ECBE /* CONTRIBUTING.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CONTRIBUTING.md; sourceTree = "<group>"; };
14C0AF831BD6D4230009ECBE /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
14D292671C1DD07E00467F6B /* global.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = global.json; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -327,6 +329,7 @@
1418948C1BDD635800EE52CE /* LaunchScreen.storyboard */,
1418948F1BDD635800EE52CE /* Info.plist */,
141894871BDD635800EE52CE /* AppNet.xcdatamodeld */,
14D292671C1DD07E00467F6B /* global.json */,
);
path = AppNet;
sourceTree = "<group>";
Expand Down Expand Up @@ -541,6 +544,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
14D292681C1DD07E00467F6B /* global.json in Resources */,
1418948B1BDD635800EE52CE /* Assets.xcassets in Resources */,
1418948E1BDD635800EE52CE /* LaunchScreen.storyboard in Resources */,
);
Expand Down

0 comments on commit 5e6b90b

Please sign in to comment.