Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. let tQueue = NSOperationQueue()
  2. let testThread1 = testThread()
  3.  
  4. tQueue.addOperation(testThread1)
  5. testThread1.threadPriority = 0
  6. testThread1.completionBlock = {() -> () in
  7. println("Thread Completed")
  8. }
  9.  
  10. class testThread: NSOperation{
  11. var delegate = UIApplication.sharedApplication().delegate as AppDelegate
  12. var threadContext:NSManagedObjectContext?
  13.  
  14. init(){
  15. super.init()
  16. NSNotificationCenter.defaultCenter().addObserver(self, selector: "contextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: nil)
  17. }
  18.  
  19. override func main(){
  20. self.threadContext = NSManagedObjectContext()
  21. threadContext!.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  22. ...
  23. //Code that actually does a fetch, or JSON parsing
  24. ...
  25. threadContext!.save(nil)
  26. NSNotificationCenter.defaultCenter().removeObserver(self)
  27. }
  28.  
  29. func contextDidSave(notification: NSNotification){
  30. let sender = notification.object as NSManagedObjectContext
  31. if sender !== self.threadContext{
  32. self.threadContext!.mergeChangesFromContextDidSaveNotification(notification)
  33. }
  34. }
  35. }
  36.  
  37. managedObjectContext?.save(nil)
  38.  
  39. func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
  40. switch(type){
  41. ... other cases
  42. case NSFetchedResultsChangeType.Update:
  43. self.configureCell(self.tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
  44. ...other cases
  45. }
  46. }
  47.  
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. NSNotificationCenter.defaultCenter().addObserver(self, selector: "contextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: nil)
  51.  
  52. ...
  53. //Code here calls the function that starts the thread shown previously to do a background fetch
  54.  
  55. }
  56.  
  57. func contextDidSave(notification: NSNotification){
  58. let sender = notification.object as NSManagedObjectContext
  59. if sender !== self.managedObjectContext!{
  60. println("Save Detected Outside Thread Main")
  61. self.managedObjectContext!.mergeChangesFromContextDidSaveNotification(notification)
  62. }
  63.  
  64. }
  65.  
  66. func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
  67. switch(type){
  68. case NSFetchedResultsChangeType.Insert:
  69. self.tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
  70. case NSFetchedResultsChangeType.Delete:
  71. self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
  72. case NSFetchedResultsChangeType.Update:
  73. if self.tableView.cellForRowAtIndexPath(indexPath!) != nil{
  74. self.configureCell(self.tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
  75. }
  76.  
  77. case NSFetchedResultsChangeType.Move:
  78. self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
  79. self.tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
  80. }
  81. }
  82.  
  83. let childContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement