From 05d830191c288034f3f97c31ff2a6f76e3653b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Sun, 13 Dec 2015 17:59:31 +0100 Subject: [PATCH] Improve notification example --- AppNet/Networking.swift | 35 +++++++++++++++++++++++++++++------ AppNet/ViewController.swift | 14 -------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/AppNet/Networking.swift b/AppNet/Networking.swift index fb3abeea..71263787 100644 --- a/AppNet/Networking.swift +++ b/AppNet/Networking.swift @@ -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 @@ -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)") } } diff --git a/AppNet/ViewController.swift b/AppNet/ViewController.swift index 4813e33b..3e781067 100644 --- a/AppNet/ViewController.swift +++ b/AppNet/ViewController.swift @@ -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() {