我是Realm
的新手,我发现它非常有趣并且已经设法保存并检索数据。在我保存的数据中,我还保存了NSDate()
,因此我可以使用逻辑LIFO
对结果进行排序,这意味着保存的最后一个应该首先进入表格视图。这是我的代码!
override func viewDidLoad() {
super.viewDidLoad()
let realmaki = try! Realm()
var eventodos = realmaki.objects(eventsNewSaved2)
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 288.0
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let realm = try! Realm()
let eventodoscount = realm.objects(eventsNewSaved2).sorted("realmgettime", ascending: true).count
return eventodoscount
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("savedCell", forIndexPath: indexPath) as! locationEventsTableViewCell
let realm = try! Realm()
let eventakiaSaved = realm.objects(eventsNewSaved2)
eventakiaSaved.sorted("realmgettime", ascending: true)
let task = eventakiaSaved[indexPath.row] as eventsNewSaved2
cell.eventTitle?.text = "\(task.realmtitle)"
cell.eventDate.text = "\(task.realmdate)"
cell.eventAttends.text = "\(task.realmattends)"
let imageDef : UIImage = UIImage(named: "noimage")!
if task.realmeventCover == "No Image Available" {
cell.eventImage.image = imageDef
} else {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let realm = try! Realm()
let eventakiaSaved = realm.objects(eventsNewSaved2)
eventakiaSaved.sorted("realmgettime", ascending: true)
let task = eventakiaSaved[indexPath.row] as eventsNewSaved2
let url = NSURL(string: task.realmeventCover)
if url != nil {
let data = NSData(contentsOfURL: url!)
dispatch_async(dispatch_get_main_queue()) {
cell.eventImage.image = UIImage(data: data!)
}
} else {
cell.eventImage.image = imageDef
}
}
}
return cell
}
变量realmgettime
是我在用户在另一个视图中按下保存按钮时保存的日期时间对象。
即使我将升序设置为true或false,表视图仍保持不变。
知道为什么不呢??
答案 0 :(得分:1)
sorted(_:, ascending:)
不会修改Results<T>
。您尝试在tableView(tableView:, cellForRowAtIndexPath:)
中对结果进行排序,但没有必要这样做。因为你忽略了返回值。
let eventakiaSaved = realm.objects(eventsNewSaved2)
eventakiaSaved.sorted("realmgettime", ascending: true)
以上代码应如下所示:
var eventakiaSaved = realm.objects(eventsNewSaved2)
var eventakiaSaved = eventakiaSaved.sorted("realmgettime", ascending: true)
更好的方法是链接方法。
let eventakiaSaved = realm.objects(eventsNewSaved2)
.sorted("realmgettime", ascending: true)
此外,Results
个实例是对基础数据的实时自动更新视图,这意味着永远不必重新获取结果。
https://realm.io/docs/swift/latest/#auto-updating-results
因此,您无需每次都重新获取相同的查询。保持结果一次,只要在同一个线程中使用它就到处都是。 推荐的方式如下:
...
var eventodos: Results<eventsNewSaved2>? // Keep results as a model
...
override func viewDidLoad() {
super.viewDidLoad()
let realm = try! Realm()
eventodos = realm
.objects(eventsNewSaved2)
.sorted("realmgettime", ascending: true) // Query only once
}
...
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventodoscount?.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("savedCell", forIndexPath: indexPath) as! locationEventsTableViewCell
let task = eventodos[indexPath.row]
cell.eventTitle?.text = "\(task.realmtitle)"
cell.eventDate.text = "\(task.realmdate)"
cell.eventAttends.text = "\(task.realmattends)"
let imageDef : UIImage = UIImage(named: "noimage")!
if task.realmeventCover == "No Image Available" {
cell.eventImage.image = imageDef
} else {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let realm = try! Realm()
// Need re-fetch because this within sub-thread
let eventakiaSaved = realm
.objects(eventsNewSaved2)
.eventakiaSaved.sorted("realmgettime", ascending: true)
let task = eventakiaSaved[indexPath.row]
let url = NSURL(string: task.realmeventCover)
if url != nil {
let data = NSData(contentsOfURL: url!)
dispatch_async(dispatch_get_main_queue()) {
cell.eventImage.image = UIImage(data: data!)
}
} else {
cell.eventImage.image = imageDef
}
}
}
return cell
}