我使用领域结果对象Result<AnObject>作为uitableview的数据源。在不同的视图控制器中有一些后期API调用,它们加载了更多的AnObject对象。我想要的是在更新我的数据源以更新表视图时得到通知。我做了一些搜索,并且知道我需要使用KVO,但是我找不到如何在realm.My代码中使用它的任何示例如下:
class myViewController: UIViewController, UITableViewDatasource {
let realm = try! Realm()
override func viewDidLoad() {
super.ViewDidLoad()
var datasource = realm.objects(AnObject.self)
// I need to some how observe the change of datasource
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasource.count
}
...
}Update I在运行之前尝试使用realm.addNotificationBlock(在@joern应答中使用相同的方式),但是问题是,当更新的任何领域对象不仅是datasouce类型时,块都会运行。重新加载表的次数太多了。
更新2 My有一个CalendarViewController,它在上半部分包含一个FSCalenar视图,在下半部分包含一个容器,链接到具有事件表视图的EventsViewController。我有这么多事件要从API中得到很长一段时间。所以我对API进行了大约20个调用,每个调用都会得到一些事件。并在NSOperationQueue中添加所有调用操作,然后根据我首先需要加载的内容设置操作优先级。因此,每个API调用都会在完成时更新数据源对象。我需要活动台面来放松一下。API调用发生在由APIManager调用的CalendarViewController类方法中。
发布于 2015-10-29 10:58:39
你不用用KVO。您可以使用王国的通知功能:
每当提交写事务时,领域实例都会向其他线程上的其他实例发送通知。可以通过注册一个块来观察这些通知:
let token = realm.addNotificationBlock { notification, realm in
let realm = try! Realm()
self.datasource = realm.objects(AnObject.self)
self.tableView.reloadData()
}只要您保持对此令牌的强烈引用,您就会得到通知。
更新
如果不想使用王国的通知,可以在API调用返回结果时发布自己的通知,然后相应地重新加载表视图:
将EventViewController添加到默认的NSNotificationCenter中,并添加一个操作:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("didReceiveReloadNotification:"), name: "RELOAD_NOTIFICATION", object: nil)
...
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func didReceiveReloadNotification(notification: NSNotification) {
let realm = try! Realm()
datasource = realm.objects(AnObject.self)
tableView.reloadData()
}然后,每当您的API请求操作完成后发布通知:
dispatch_async(dispatch_get_main_queue()) { () -> Void in
NSNotificationCenter.defaultCenter().postNotificationName("RELOAD_NOTIFICATION", object: nil)
}发布于 2019-10-02 06:50:31
这里我更新了Swift 4+的答案,以便在结果更新时得到通知
第1点:声明以下属性
var notificationToken: NotificationToken? = nil
let results = realmManager.realm.objects(MemberHistory.self)要点2:您可以在viewWillAppear的下面部分添加或添加一个方法并在ViewWillAppear上调用该方法
{
notificationToken = results?.observe {(changes: RealmCollectionChange) in
switch changes {
case .initial(let data):
print("initials count - \(data.count)")
self.chatTableView.reloadData()
case .update(_, let deletions, let insertions, let modifications):
if(insertions.count == self.arrMessages?.count)
{
//This section will execute when the user's first load of messages comes from API, I had used this for my internal purpose
}
else
{
self.chatTableView.beginUpdates()
self.chatTableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
with: .none)
self.chatTableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }), with: .none)
self.chatTableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
with: .none)
self.chatTableView.endUpdates()
self.chatTableView.scrollToBottom()
}
case .error(let error):
Toast(text: "Error", duration: Delay.short).show()
fatalError("\(error)")
}
}
}https://stackoverflow.com/questions/33412078
复制相似问题