2

I am new to programming and currently trying things with firebase. I am building a newsfeed like app, which displays custom type cells in my tableView. I currently have the problem that after an app restart the separation is like the one in firebase. But I want the newest be first and stay there. I thought about saving the array in core data, but I don't like this option as much. So my question is: how do I sort the posts from newest to oldest?

var ref: DatabaseReference!

var posts = [String]()
var timeRef = [String]()
var userRef = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    addSlideMenuButton()

    ref = Database.database().reference()

    ref.child("posts").observe( .childAdded, with: { snapshot in

        let newPost = snapshot.value as? NSDictionary
        let post_title:String = newPost!["post_title"] as? String ?? "error"

        let newPostTime = snapshot.value as? NSDictionary
        let post_time:String = newPostTime!["post_time"] as? String ?? "error"

        let newPostUser = snapshot.value as? NSDictionary
        let post_user:String = newPostUser!["post_user"] as? String ?? "Team"

        self.posts.insert(post_title, at: 0)
        self.timeRef.insert(post_time, at: 0)
        self.userRef.insert(post_user, at: 0)

        self.newsfeedTableView.reloadData()
    })
}

(Tell me if you need more code)

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Roman H
  • 251
  • 2
  • 10
  • You can `sort` array by `post_time` once you received it from firebase then reload your `tableView`. – Dharmesh Kheni Dec 07 '18 at 06:44
  • But I need to use Calendar, right? So I can use the date for separation? – Roman H Dec 07 '18 at 06:47
  • Yes and `post_time` should return a date or time interval of post creation time. – Dharmesh Kheni Dec 07 '18 at 06:48
  • can u give me an example, please. Because I once watched a tutorial and didn't understand a thing D: – Roman H Dec 07 '18 at 06:53
  • When you are creating a post and posting it to firebase you just need to give current time interval to `post_time` property. and here is how you can get current time interval https://stackoverflow.com/questions/358207/iphone-how-to-get-current-milliseconds. – Dharmesh Kheni Dec 07 '18 at 06:55
  • oh i am not sending anything to firebase. I am just gettin Data. I type in the time manual – Roman H Dec 07 '18 at 06:58
  • So you are creating post manually from firebase? – Dharmesh Kheni Dec 07 '18 at 06:58
  • Yes, that's what I do. I am a beginner and not much into Databases – Roman H Dec 07 '18 at 07:00
  • 1
    then you can create a date with time with correct formate then convert that string to `Date` once you received it into your project and sort it – Dharmesh Kheni Dec 07 '18 at 07:03
  • Basically **never** use multiple arrays as table data source. Sorting three arrays to keep the same order is very expensive. Use a custom struct or class. What string format is `post_time`? – vadian Dec 07 '18 at 09:39

1 Answers1

1

I'm not familiar with firebase but it might have something built in that might help you with that task as it's a common one.

Anyway, this is how you may do it with your method:

let isoFormatter = ISO8601DateFormatter()

// dateStrings will be fetched from your database
let dateStrings = [
    "2018-12-07T09:21:42Z",
    "2018-12-07T09:19:42Z",
    "2018-12-07T09:20:42Z"
]

// here we're converting the strings to an actual Date types
let dates = dateStrings.compactMap { isoFormatter.date(from: $0) }

// sorting in descending order
let sortedDates = dates.sorted { $0 > $1 }

print(sortedDates)
/*
 prints-

 [
    2018-12-07 09:21:42 +0000,
    2018-12-07 09:20:42 +0000,
    2018-12-07 09:19:42 +0000
 ]
 */

You can use isoFormatter.string(from: Date) to upload a date to your database

Niv
  • 517
  • 5
  • 18