Skip to content

Commit

Permalink
Improve notification example
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Dec 13, 2015
1 parent c4709b7 commit 05d8301
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
35 changes: 29 additions & 6 deletions AppNet/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit
import DATAStack
import Sync

class Networking {
class Networking: NSObject {
let AppNetURL = "https://api.app.net/posts/stream/global"
let dataStack: DATAStack

Expand All @@ -24,12 +24,35 @@ class Networking {
}).resume()
}

/*
An example on how to properly receive notifications of changes. Includes a local "global.json" so you can modify it to try it,
but it can easily work with your own JSON too. Please use notifications to react to changes, not to modify the returned elements
since that would be unsafe.
*/
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 url = NSURL(string: "global.json")!
let filePath = NSBundle.mainBundle().pathForResource(url.URLByDeletingPathExtension?.absoluteString, ofType: url.pathExtension)!
let data = NSData(contentsOfFile: filePath)!
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)
})

self.dataStack.performInNewBackgroundContext { backgroundContext in
NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeNotification:", name: NSManagedObjectContextObjectsDidChangeNotification, object: backgroundContext)

Sync.changes(json["data"] as! Array, inEntityNamed: "Data", predicate: nil, parent: nil, inContext: backgroundContext, dataStack: self.dataStack, completion: { error in
NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextObjectsDidChangeNotification, object: nil)

completion(error)
})
}
}

func changeNotification(notification: NSNotification) {
let updatedObjects = notification.userInfo?[NSUpdatedObjectsKey]
let deletedObjects = notification.userInfo?[NSDeletedObjectsKey]
let insertedObjects = notification.userInfo?[NSInsertedObjectsKey]

print("updatedObjects: \(updatedObjects)")
print("deletedObjects: \(deletedObjects)")
print("insertedObjects: \(insertedObjects)")
}
}
14 changes: 0 additions & 14 deletions AppNet/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ class ViewController: UITableViewController {

fetchCurrentObjects()
fetchNewData()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeNotification:", name: NSManagedObjectContextObjectsDidChangeNotification, object: self.dataStack.mainContext)
}

// MARK: Private methods

func changeNotification(notification: NSNotification) {
let updatedObjects = notification.userInfo?[NSUpdatedObjectsKey]
let deletedObjects = notification.userInfo?[NSDeletedObjectsKey]
let insertedObjects = notification.userInfo?[NSInsertedObjectsKey]

print("updatedObjects: \(updatedObjects)")
print("deletedObjects: \(deletedObjects)")
print("insertedObjects: \(insertedObjects)")
}

func fetchNewData() {
Expand Down

0 comments on commit 05d8301

Please sign in to comment.