Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let tQueue = NSOperationQueue()
- let testThread1 = testThread()
- tQueue.addOperation(testThread1)
- testThread1.threadPriority = 0
- testThread1.completionBlock = {() -> () in
- println("Thread Completed")
- }
- class testThread: NSOperation{
- var delegate = UIApplication.sharedApplication().delegate as AppDelegate
- var threadContext:NSManagedObjectContext?
- init(){
- super.init()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "contextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: nil)
- }
- override func main(){
- self.threadContext = NSManagedObjectContext()
- threadContext!.persistentStoreCoordinator = delegate.persistentStoreCoordinator
- ...
- //Code that actually does a fetch, or JSON parsing
- ...
- threadContext!.save(nil)
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- func contextDidSave(notification: NSNotification){
- let sender = notification.object as NSManagedObjectContext
- if sender !== self.threadContext{
- self.threadContext!.mergeChangesFromContextDidSaveNotification(notification)
- }
- }
- }
- managedObjectContext?.save(nil)
- func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
- switch(type){
- ... other cases
- case NSFetchedResultsChangeType.Update:
- self.configureCell(self.tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
- ...other cases
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "contextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: nil)
- ...
- //Code here calls the function that starts the thread shown previously to do a background fetch
- }
- func contextDidSave(notification: NSNotification){
- let sender = notification.object as NSManagedObjectContext
- if sender !== self.managedObjectContext!{
- println("Save Detected Outside Thread Main")
- self.managedObjectContext!.mergeChangesFromContextDidSaveNotification(notification)
- }
- }
- func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
- switch(type){
- case NSFetchedResultsChangeType.Insert:
- self.tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
- case NSFetchedResultsChangeType.Delete:
- self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
- case NSFetchedResultsChangeType.Update:
- if self.tableView.cellForRowAtIndexPath(indexPath!) != nil{
- self.configureCell(self.tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
- }
- case NSFetchedResultsChangeType.Move:
- self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
- self.tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
- }
- }
- let childContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement