daily pastebin goal
74%
SHARE
TWEET

Untitled

a guest Feb 14th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DispatchQueue.global(qos: .userInitiated).async {
  2. let realm = try! Realm()
  3.  
  4. let cachedWords = CashedWords()
  5. cachedWords.words.removeAll()
  6.  
  7. let predicate = NSPredicate(format: "enabled == 1")
  8. let results = realm.objects(Word.self).filter(predicate).distinct(by: ["title"])
  9.  
  10. let azer = Locale(identifier: "az")
  11. let alphabeticallySortedResults = Array(results).sorted {
  12.     $0.title.compare($1.title, locale: azer) == .orderedAscending
  13. }
  14.  
  15. cachedWords.words.append(objectsIn: alphabeticallySortedResults)
  16. try! realm.write {
  17.     realm.add(cachedWords)
  18. }
  19. let wordsRef = ThreadSafeReference(to: cachedWords)
  20.  
  21. DispatchQueue.main.async {
  22.     let realm = try! Realm()
  23.     guard let wordsResult = realm.resolve(wordsRef) else {
  24.         return // object was deleted
  25.     }
  26.  
  27.     self.words = Array(wordsResult.words)
  28.     self.reloadTableInMain()
  29. }
RAW Paste Data
Top