过滤领域

时间:2017-09-17 14:41:43

标签: swift realm

设置

Swift 3应用程序,RealmSwift 2.10.1,带有一个按钮,tableview和一个不确定的旋转进度指示器(旋转圆圈)

目标

当用户单击该按钮时,查询大型域数据库(数百万条记录)并在等待结果时显示进度指示器。

问题

直到过滤器完成后,才会显示进度指示器。

代码

//called from button click
func performFilterAction() {  
     self.filterProgressIndicator.isHidden = false
     self.filterProgressIndicator.startAnimation(nil)
     let predicate = NSPredicate(format: "location = %@", "US")
     let realm = try! Realm()
     self.results = realm.objects(MyClass.self).filter(predicate)
     self.tableView.reloadData()
     self.filterProgressIndicator.isHidden = true
}

上面的代码过滤了美国境内所有位置的Realm,并填充了一个数组,该数组用作tableView的数据源。

其他

另一个问题是即使进度指示器隐藏在代码的末尾,它也不会隐藏在UI中。

我尝试在后台线程上实现过滤代码:

func performFilterAction() {
     self.filterProgressIndicator.isHidden = false
     self.filterProgressIndicator.startAnimation(nil)

     DispatchQueue.global(qos: .background).async {  
          let predicate = NSPredicate(format: "location = %@", "US")
          let realm = try! Realm()
          self.results = realm.objects(MyClass.self).filter(predicate)
          DispatchQueue.main.async {
              self.tableView.reloadData()
              self.filterProgressIndicator.isHidden = true
          }
     }
}

在过滤之前显示progressIndicator但在tableView重新加载时崩溃并出现以下错误

  

此应用程序正在从后台修改autolayout引擎   从主线程访问引擎后的线程。这个可以   导致引擎损坏和奇怪的崩溃。

另一种尝试:

根据提供的答案,我实施了像这样的收集通知

func performFilterAction1() {
    self.filterProgressIndicator.isHidden = false
    self.filterProgressIndicator.startAnimation(nil)
    let predicate = NSPredicate(format: "location = %@", "US")
    let realm = try! Realm()
    self.results = realm.objects(MyClass.self).filter(predicate)

    self.notificationToken = self.results.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
        guard let tableView = self?.filterTableView else { return }
        switch changes {
        case .initial:
            // Results are now populated and can be accessed without blocking the UI
            tableView.reloadData()
            self?.filterProgressIndicator.isHidden = true
            break
        case .update(_, let deletions, let insertions, let modifications):
            break
        case .error(let error):
            // An error occurred while opening the Realm file on the background worker thread
            fatalError("\(error)")
            break
        }
    }
}

结果:进度指示器直到过滤器完成后才显示,并且只是“闪烁”'暂时。

根据请求:回溯

772.00 ms   58.7%   0 s      Main Thread  0x72995
742.00 ms   56.5%   0 s       start
742.00 ms   56.5%   0 s        main
742.00 ms   56.5%   0 s         NSApplicationMain
625.00 ms   47.6%   1.00 ms          -[NSApplication run]
377.00 ms   28.7%   0 s           -[NSApplication(NSEvent) sendEvent:]
342.00 ms   26.0%   0 s            -[NSWindow(NSEventRouting) sendEvent:]
342.00 ms   26.0%   0 s             -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:]
342.00 ms   26.0%   0 s              -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:]
331.00 ms   25.2%   0 s               -[NSControl mouseDown:]
330.00 ms   25.1%   0 s                -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:]
330.00 ms   25.1%   0 s                 -[NSCell trackMouse:inRect:ofView:untilMouseUp:]
319.00 ms   24.2%   0 s                  _os_activity_initiate_impl
319.00 ms   24.2%   0 s                   -[NSButtonCell _sendActionFrom:]
319.00 ms   24.2%   0 s                    -[NSCell _sendActionFrom:]
319.00 ms   24.2%   0 s                     _os_activity_initiate_impl
319.00 ms   24.2%   0 s                      __26-[NSCell _sendActionFrom:]_block_invoke
319.00 ms   24.2%   0 s                       -[NSControl sendAction:to:]
319.00 ms   24.2%   0 s                        -[NSApplication(NSResponder) sendAction:to:from:]
318.00 ms   24.2%   0 s                         _os_activity_initiate_impl
259.00 ms   19.7%   0 s                          @objc FilterVC.filterAction(Any) -> ()
259.00 ms   19.7%   0 s                           FilterVC.filterAction(Any) -> ()
257.00 ms   19.5%   0 s                            FilterVC.test() -> ()
238.00 ms   18.1%   0 s                             Results.count.getter
238.00 ms   18.1%   0 s                              -[RLMResults count]
238.00 ms   18.1%   0 s                               -[RLMResults count]::$_1::operator()() const
238.00 ms   18.1%   0 s                                realm::Results::size()
238.00 ms   18.1%   0 s                                 realm::Query::count(unsigned long, unsigned long, unsigned long) const
238.00 ms   18.1%   0 s                                  realm::Query::aggregate_internal(realm::Action, realm::DataType, bool, realm::ParentNode*, realm::QueryStateBase*, unsigned long, unsigned long, realm::SequentialGetterBase*) const

2 个答案:

答案 0 :(得分:1)

这是Realm's collection notifications的完美用例。这看起来像是:

var notificationToken: NotificationToken? = nil
var results: Results<MyClass>? = nil

func performFilterAction() {
  filterProgressIndicator.isHidden = false
  filterProgressIndicator.startAnimation(nil)

  let realm = try! Realm()
  results = realm.objects(MyClass.self).filter("location = %@", "US")
  notificationToken = results.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
    guard let tableView = self?.tableView else { return }
    switch changes {
    case .initial:
      // Results are now populated and can be accessed without blocking the UI
      tableView.reloadData()
      self.filterProgressIndicator.isHidden = true
      break
    case .update(_, let deletions, let insertions, let modifications):
      // Query results have changed, so apply them to the UITableView
      tableView.beginUpdates()
      tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
                         with: .automatic)
      tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
                         with: .automatic)
      tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
                         with: .automatic)
      tableView.endUpdates()
      break
    case .error(let error):
      // An error occurred while opening the Realm file on the background worker thread
      fatalError("\(error)")
      break
    }
  }
}

deinit {
  notificationToken?.stop()
}

这使您可以在执行初始过滤时显示进度指示器,还可以在对集合中的数据进行更改时提供动画更新。如果您不关心后者,您也可以在reloadData()案例中致电.update

答案 1 :(得分:0)

当self.result发生更改时(例如在didSet中),您是否有任何视觉效果?如果你这样做,那么这可能是导致第二个解决方案出错的原因。

你可以做的是将加载过程发送到主线程,而不是后台线程(即第一种和第二种方法的混合),以便进度指示器有机会出现:

func performFilterAction() {
     self.filterProgressIndicator.isHidden = false
     self.filterProgressIndicator.startAnimation(nil)

     DispatchQueue.main.async {  
          let predicate = NSPredicate(format: "location = %@", "US")
          let realm = try! Realm()
          self.results = realm.objects(MyClass.self).filter(predicate)
          self.tableView.reloadData()
          self.filterProgressIndicator.isHidden = true
     }
}

请注意,尽管这可能有效,但在主线程上运行冗长的进程并不是一个好习惯。另一种方法可能是在后台线程中加载数据,但是当它完全就绪时,将它放在从主线程分配给结果的单独变量中。这将像你的第二种方法一样工作,除了self.result不会在后台分配,而是在主线程中分配。

func performFilterAction() {
     self.filterProgressIndicator.isHidden = false
     self.filterProgressIndicator.startAnimation(nil)

     DispatchQueue.global(qos: .background).async {  
          let predicate = NSPredicate(format: "location = %@", "US")
          let realm = try! Realm()
          let newResults = realm.objects(MyClass.self).filter(predicate)
          DispatchQueue.main.async {
              self.results = newResults
              self.tableView.reloadData()
              self.filterProgressIndicator.isHidden = true
          }
     }
}