Answer the question
In order to leave comments, you need to log in
How to create an id?
Tell me plz. How to create an iteration of id in the idPosts array, but at the same time, after the application exited, the value was saved, and not reset.
Structure:
users: {
"asd2123123adsa": [
name: "Ivan",
lastname: "Ivanov"
idPosts: [
id_1: "123123213",
id_2: "543547643",
...
id_n: "785621232"
]
],
"dg43531234234": [
name: "Petr",
lastname: "Petrov"
idPosts: [
id_1: "223123213",
id_2: "343547643",
...
id_n: "185621232"
]
]
}
Answer the question
In order to leave comments, you need to log in
To add a new element to idPosts:
let currentUserID = // Setup user id
let idPostsRef = FIRDatabase.database().reference().child("users/\(key)/idPosts")
// Getting last postID key
idPostsRef.observeSingleEvent(of: .value, with: { snapshot in
if let dictionary = snapshot.value as? [String: AnyObject] {
let lastPostId = dictionary.keys.sorted()[dictionary.count - 1]
let substringStartIndex = lastPostId.index(lastPostId.startIndex, offsetBy: +2)
guard let lastPostNumber = Int(lastPostId.substring(from: substringStartIndex)) else { return }
let index = lastPostNumber + 1
idPostsRef.child("id\(index)").setValue(post.id)
} else {
idPostsRef.child("id0").setValue(post.id)
}
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question