Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. extension Realm {
  2.  
  3. public var queue: NSOperationQueue {
  4. get {
  5. if let queue = objc_getAssociatedObject(self, &StoredPropaties.queue) as? NSOperationQueue {
  6. return queue
  7. }
  8.  
  9. let queue = self.createDefaultQueue()
  10. self.queue = queue
  11. return queue
  12. }
  13. set {
  14. objc_setAssociatedObject(self, &StoredPropaties.queue, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
  15. }
  16. }
  17.  
  18. public func writeBackground(block: (store: Realm) -> Void, completion: () -> Void) {
  19.  
  20. self.queue.addOperationWithBlock {
  21. autoreleasepool {
  22. do {
  23. let realm = try Realm(configuration: self.configuration)
  24. try realm.write {
  25. block(store: realm)
  26. }
  27. } catch {
  28. // FIXME: Error Handling
  29. }
  30. NSOperationQueue.mainQueue().addOperationWithBlock {
  31. completion()
  32. }
  33. }
  34. }
  35. }
  36.  
  37. private enum StoredPropaties {
  38. static var queue: Void?
  39. }
  40.  
  41. private func createDefaultQueue() -> NSOperationQueue {
  42. let queue = NSOperationQueue()
  43. queue.maxConcurrentOperationCount = 1
  44. queue.qualityOfService = NSQualityOfService.Default
  45. return queue
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement