I'm new to Realm and i find it very interesting and already have managed to save and retrieve data. Among with my data that i save, i also save an NSDate() so i can sort my results with the logic LIFO, that means the last that is saved should be first out to the table view. This is my code!
override func viewDidLoad() {
super.viewDidLoad()
let realmaki = try! Realm()
var eventodos = realmaki.objects(eventsNewSaved2)
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
retu 288.0
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
retu 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let realm = try! Realm()
let eventodoscount = realm.objects(eventsNewSaved2).sorted("realmgettime", ascending: true).count
retu 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
}
}
}
retu cell
}
The variable realmgettime is the Date Time object that i save while the user pushes save button in another view. The table view remains the same even if i set ascending to true or false. Any idea why not??
