search about user orders and sort by date (newest first) with firebase











up vote
0
down vote

favorite












i'm have an order table in firebase like this
enter image description here



and in my iOS app, i want to show the orders for the user which is active and want to sort them by (CreateDate) to show the newest first



i'm using this code , it's only show the user orders without sorting by date



 if   (Auth.auth().currentUser != nil) {
let user = Auth.auth().currentUser
if let user = user {
uid = user.uid

print("uid = (uid)")


// get order details
let ordersRef = rootRef.child("orders")
ordersRef.queryOrdered(byChild: "clientuid_status").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in

let snabshotvalue = snapshot.value as? [String : AnyObject]
let dateorder = snabshotvalue!["orderDate"]
let tecname = snabshotvalue!["tecname"]
let theproblem = snabshotvalue!["theproblem"]
let homeadress = snabshotvalue!["homeadress"]
let clintlat = snabshotvalue!["clintlat"]
let clintlong = snabshotvalue!["clintlong"]
let clintuid = snabshotvalue!["clintuid"]
let tecuid = snabshotvalue!["tecuid"]
let tecToken = snabshotvalue!["tecToken"]
let status = snabshotvalue!["status"]
let servicetable = snabshotvalue!["servicetable"]

let snashotkey = snapshot.key as String
print("firstname: (tecname) ")

self.dateArray.append(dateorder as? String)
self.tecnameArray.append(tecname as? String)
self.problemArray.append(theproblem as? String)
self.homeadressArray.append(homeadress as? String)
self.clintlatArray.append(clintlat as? String)
self.clintlongArray.append(clintlong as? String)
self.tecuidArray.append(tecuid as? String)
self.tecTokenArray.append(tecToken as? String)
self.orderuidArray.append(snashotkey )
self.clintidArray.append(clintuid as? String)
self.statusArray.append(status as? String)
self.servicetableArray.append(servicetable as? String)

self.getTechRatin(teckUid: tecuid as! String)

self.tableView.reloadData()
})


}


i'm sent a ticket to firebase support, they told me to use this code :



    let orderOrdersByCreatedDate= (ref.child("orders").child(getUid())).queryOrdered(byChild: "createDate").queryLimited(toLast: 1)


when i try to use your code, i get this error :
Use of unresolved identifier 'getUid'



i don't know how use getUid()










share|improve this question
























  • Do you want to get user id from firebase?
    – DionizB
    Nov 22 at 14:23










  • I can get it by let user = Auth.auth().currentUser uid = user.uid
    – aqeel
    Nov 22 at 14:37















up vote
0
down vote

favorite












i'm have an order table in firebase like this
enter image description here



and in my iOS app, i want to show the orders for the user which is active and want to sort them by (CreateDate) to show the newest first



i'm using this code , it's only show the user orders without sorting by date



 if   (Auth.auth().currentUser != nil) {
let user = Auth.auth().currentUser
if let user = user {
uid = user.uid

print("uid = (uid)")


// get order details
let ordersRef = rootRef.child("orders")
ordersRef.queryOrdered(byChild: "clientuid_status").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in

let snabshotvalue = snapshot.value as? [String : AnyObject]
let dateorder = snabshotvalue!["orderDate"]
let tecname = snabshotvalue!["tecname"]
let theproblem = snabshotvalue!["theproblem"]
let homeadress = snabshotvalue!["homeadress"]
let clintlat = snabshotvalue!["clintlat"]
let clintlong = snabshotvalue!["clintlong"]
let clintuid = snabshotvalue!["clintuid"]
let tecuid = snabshotvalue!["tecuid"]
let tecToken = snabshotvalue!["tecToken"]
let status = snabshotvalue!["status"]
let servicetable = snabshotvalue!["servicetable"]

let snashotkey = snapshot.key as String
print("firstname: (tecname) ")

self.dateArray.append(dateorder as? String)
self.tecnameArray.append(tecname as? String)
self.problemArray.append(theproblem as? String)
self.homeadressArray.append(homeadress as? String)
self.clintlatArray.append(clintlat as? String)
self.clintlongArray.append(clintlong as? String)
self.tecuidArray.append(tecuid as? String)
self.tecTokenArray.append(tecToken as? String)
self.orderuidArray.append(snashotkey )
self.clintidArray.append(clintuid as? String)
self.statusArray.append(status as? String)
self.servicetableArray.append(servicetable as? String)

self.getTechRatin(teckUid: tecuid as! String)

self.tableView.reloadData()
})


}


i'm sent a ticket to firebase support, they told me to use this code :



    let orderOrdersByCreatedDate= (ref.child("orders").child(getUid())).queryOrdered(byChild: "createDate").queryLimited(toLast: 1)


when i try to use your code, i get this error :
Use of unresolved identifier 'getUid'



i don't know how use getUid()










share|improve this question
























  • Do you want to get user id from firebase?
    – DionizB
    Nov 22 at 14:23










  • I can get it by let user = Auth.auth().currentUser uid = user.uid
    – aqeel
    Nov 22 at 14:37













up vote
0
down vote

favorite









up vote
0
down vote

favorite











i'm have an order table in firebase like this
enter image description here



and in my iOS app, i want to show the orders for the user which is active and want to sort them by (CreateDate) to show the newest first



i'm using this code , it's only show the user orders without sorting by date



 if   (Auth.auth().currentUser != nil) {
let user = Auth.auth().currentUser
if let user = user {
uid = user.uid

print("uid = (uid)")


// get order details
let ordersRef = rootRef.child("orders")
ordersRef.queryOrdered(byChild: "clientuid_status").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in

let snabshotvalue = snapshot.value as? [String : AnyObject]
let dateorder = snabshotvalue!["orderDate"]
let tecname = snabshotvalue!["tecname"]
let theproblem = snabshotvalue!["theproblem"]
let homeadress = snabshotvalue!["homeadress"]
let clintlat = snabshotvalue!["clintlat"]
let clintlong = snabshotvalue!["clintlong"]
let clintuid = snabshotvalue!["clintuid"]
let tecuid = snabshotvalue!["tecuid"]
let tecToken = snabshotvalue!["tecToken"]
let status = snabshotvalue!["status"]
let servicetable = snabshotvalue!["servicetable"]

let snashotkey = snapshot.key as String
print("firstname: (tecname) ")

self.dateArray.append(dateorder as? String)
self.tecnameArray.append(tecname as? String)
self.problemArray.append(theproblem as? String)
self.homeadressArray.append(homeadress as? String)
self.clintlatArray.append(clintlat as? String)
self.clintlongArray.append(clintlong as? String)
self.tecuidArray.append(tecuid as? String)
self.tecTokenArray.append(tecToken as? String)
self.orderuidArray.append(snashotkey )
self.clintidArray.append(clintuid as? String)
self.statusArray.append(status as? String)
self.servicetableArray.append(servicetable as? String)

self.getTechRatin(teckUid: tecuid as! String)

self.tableView.reloadData()
})


}


i'm sent a ticket to firebase support, they told me to use this code :



    let orderOrdersByCreatedDate= (ref.child("orders").child(getUid())).queryOrdered(byChild: "createDate").queryLimited(toLast: 1)


when i try to use your code, i get this error :
Use of unresolved identifier 'getUid'



i don't know how use getUid()










share|improve this question















i'm have an order table in firebase like this
enter image description here



and in my iOS app, i want to show the orders for the user which is active and want to sort them by (CreateDate) to show the newest first



i'm using this code , it's only show the user orders without sorting by date



 if   (Auth.auth().currentUser != nil) {
let user = Auth.auth().currentUser
if let user = user {
uid = user.uid

print("uid = (uid)")


// get order details
let ordersRef = rootRef.child("orders")
ordersRef.queryOrdered(byChild: "clientuid_status").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in

let snabshotvalue = snapshot.value as? [String : AnyObject]
let dateorder = snabshotvalue!["orderDate"]
let tecname = snabshotvalue!["tecname"]
let theproblem = snabshotvalue!["theproblem"]
let homeadress = snabshotvalue!["homeadress"]
let clintlat = snabshotvalue!["clintlat"]
let clintlong = snabshotvalue!["clintlong"]
let clintuid = snabshotvalue!["clintuid"]
let tecuid = snabshotvalue!["tecuid"]
let tecToken = snabshotvalue!["tecToken"]
let status = snabshotvalue!["status"]
let servicetable = snabshotvalue!["servicetable"]

let snashotkey = snapshot.key as String
print("firstname: (tecname) ")

self.dateArray.append(dateorder as? String)
self.tecnameArray.append(tecname as? String)
self.problemArray.append(theproblem as? String)
self.homeadressArray.append(homeadress as? String)
self.clintlatArray.append(clintlat as? String)
self.clintlongArray.append(clintlong as? String)
self.tecuidArray.append(tecuid as? String)
self.tecTokenArray.append(tecToken as? String)
self.orderuidArray.append(snashotkey )
self.clintidArray.append(clintuid as? String)
self.statusArray.append(status as? String)
self.servicetableArray.append(servicetable as? String)

self.getTechRatin(teckUid: tecuid as! String)

self.tableView.reloadData()
})


}


i'm sent a ticket to firebase support, they told me to use this code :



    let orderOrdersByCreatedDate= (ref.child("orders").child(getUid())).queryOrdered(byChild: "createDate").queryLimited(toLast: 1)


when i try to use your code, i get this error :
Use of unresolved identifier 'getUid'



i don't know how use getUid()







ios swift firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 14:22









Doug Stevenson

67.3k87998




67.3k87998










asked Nov 22 at 14:15









aqeel

5018




5018












  • Do you want to get user id from firebase?
    – DionizB
    Nov 22 at 14:23










  • I can get it by let user = Auth.auth().currentUser uid = user.uid
    – aqeel
    Nov 22 at 14:37


















  • Do you want to get user id from firebase?
    – DionizB
    Nov 22 at 14:23










  • I can get it by let user = Auth.auth().currentUser uid = user.uid
    – aqeel
    Nov 22 at 14:37
















Do you want to get user id from firebase?
– DionizB
Nov 22 at 14:23




Do you want to get user id from firebase?
– DionizB
Nov 22 at 14:23












I can get it by let user = Auth.auth().currentUser uid = user.uid
– aqeel
Nov 22 at 14:37




I can get it by let user = Auth.auth().currentUser uid = user.uid
– aqeel
Nov 22 at 14:37












1 Answer
1






active

oldest

votes

















up vote
0
down vote













That last snippet of code is wrong. To sort the orders by creadeDate you need:



ordersRef.queryOrdered(byChild: "creadeDate").observe(.childAdded, ...


But Firebase Database queries can only order/filter on a single property, so that means that you can no longer filter on clientuid_status as you do now.



In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. You already seem to have done this with clientuid_status, and to allow both filtering on that and ordering on date, you'll expand it to clientuid_status_creadeDate and order/filter on that.



For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase






share|improve this answer





















  • thanks,i don't care to filter by clientuid_status, i only want to retrieve the orders which equal to clientuid_status and sort them by createDate without sort by uid or status (note: i write it wrong creadeDate) , so if i write the code like this why it's not working : rootRef.child("orders").queryOrdered(byChild: "createDate").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in
    – aqeel
    Nov 22 at 22:15










  • Because you're ordering/filtering by createDate, and then pass in a UID. If you just want to sort on createDate drop the queryEqual(toValue: clause: rootRef.child("orders").queryOrdered(byChild: "createDate").observe(.childAdded. Note that unless you updated your data, you'll need creadeDate.
    – Frank van Puffelen
    Nov 22 at 22:51











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432894%2fsearch-about-user-orders-and-sort-by-date-newest-first-with-firebase%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













That last snippet of code is wrong. To sort the orders by creadeDate you need:



ordersRef.queryOrdered(byChild: "creadeDate").observe(.childAdded, ...


But Firebase Database queries can only order/filter on a single property, so that means that you can no longer filter on clientuid_status as you do now.



In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. You already seem to have done this with clientuid_status, and to allow both filtering on that and ordering on date, you'll expand it to clientuid_status_creadeDate and order/filter on that.



For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase






share|improve this answer





















  • thanks,i don't care to filter by clientuid_status, i only want to retrieve the orders which equal to clientuid_status and sort them by createDate without sort by uid or status (note: i write it wrong creadeDate) , so if i write the code like this why it's not working : rootRef.child("orders").queryOrdered(byChild: "createDate").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in
    – aqeel
    Nov 22 at 22:15










  • Because you're ordering/filtering by createDate, and then pass in a UID. If you just want to sort on createDate drop the queryEqual(toValue: clause: rootRef.child("orders").queryOrdered(byChild: "createDate").observe(.childAdded. Note that unless you updated your data, you'll need creadeDate.
    – Frank van Puffelen
    Nov 22 at 22:51















up vote
0
down vote













That last snippet of code is wrong. To sort the orders by creadeDate you need:



ordersRef.queryOrdered(byChild: "creadeDate").observe(.childAdded, ...


But Firebase Database queries can only order/filter on a single property, so that means that you can no longer filter on clientuid_status as you do now.



In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. You already seem to have done this with clientuid_status, and to allow both filtering on that and ordering on date, you'll expand it to clientuid_status_creadeDate and order/filter on that.



For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase






share|improve this answer





















  • thanks,i don't care to filter by clientuid_status, i only want to retrieve the orders which equal to clientuid_status and sort them by createDate without sort by uid or status (note: i write it wrong creadeDate) , so if i write the code like this why it's not working : rootRef.child("orders").queryOrdered(byChild: "createDate").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in
    – aqeel
    Nov 22 at 22:15










  • Because you're ordering/filtering by createDate, and then pass in a UID. If you just want to sort on createDate drop the queryEqual(toValue: clause: rootRef.child("orders").queryOrdered(byChild: "createDate").observe(.childAdded. Note that unless you updated your data, you'll need creadeDate.
    – Frank van Puffelen
    Nov 22 at 22:51













up vote
0
down vote










up vote
0
down vote









That last snippet of code is wrong. To sort the orders by creadeDate you need:



ordersRef.queryOrdered(byChild: "creadeDate").observe(.childAdded, ...


But Firebase Database queries can only order/filter on a single property, so that means that you can no longer filter on clientuid_status as you do now.



In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. You already seem to have done this with clientuid_status, and to allow both filtering on that and ordering on date, you'll expand it to clientuid_status_creadeDate and order/filter on that.



For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase






share|improve this answer












That last snippet of code is wrong. To sort the orders by creadeDate you need:



ordersRef.queryOrdered(byChild: "creadeDate").observe(.childAdded, ...


But Firebase Database queries can only order/filter on a single property, so that means that you can no longer filter on clientuid_status as you do now.



In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. You already seem to have done this with clientuid_status, and to allow both filtering on that and ordering on date, you'll expand it to clientuid_status_creadeDate and order/filter on that.



For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 14:45









Frank van Puffelen

223k26364390




223k26364390












  • thanks,i don't care to filter by clientuid_status, i only want to retrieve the orders which equal to clientuid_status and sort them by createDate without sort by uid or status (note: i write it wrong creadeDate) , so if i write the code like this why it's not working : rootRef.child("orders").queryOrdered(byChild: "createDate").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in
    – aqeel
    Nov 22 at 22:15










  • Because you're ordering/filtering by createDate, and then pass in a UID. If you just want to sort on createDate drop the queryEqual(toValue: clause: rootRef.child("orders").queryOrdered(byChild: "createDate").observe(.childAdded. Note that unless you updated your data, you'll need creadeDate.
    – Frank van Puffelen
    Nov 22 at 22:51


















  • thanks,i don't care to filter by clientuid_status, i only want to retrieve the orders which equal to clientuid_status and sort them by createDate without sort by uid or status (note: i write it wrong creadeDate) , so if i write the code like this why it's not working : rootRef.child("orders").queryOrdered(byChild: "createDate").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in
    – aqeel
    Nov 22 at 22:15










  • Because you're ordering/filtering by createDate, and then pass in a UID. If you just want to sort on createDate drop the queryEqual(toValue: clause: rootRef.child("orders").queryOrdered(byChild: "createDate").observe(.childAdded. Note that unless you updated your data, you'll need creadeDate.
    – Frank van Puffelen
    Nov 22 at 22:51
















thanks,i don't care to filter by clientuid_status, i only want to retrieve the orders which equal to clientuid_status and sort them by createDate without sort by uid or status (note: i write it wrong creadeDate) , so if i write the code like this why it's not working : rootRef.child("orders").queryOrdered(byChild: "createDate").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in
– aqeel
Nov 22 at 22:15




thanks,i don't care to filter by clientuid_status, i only want to retrieve the orders which equal to clientuid_status and sort them by createDate without sort by uid or status (note: i write it wrong creadeDate) , so if i write the code like this why it's not working : rootRef.child("orders").queryOrdered(byChild: "createDate").queryEqual(toValue: "(uid)_0").observe(.childAdded, with: { (snapshot) -> Void in
– aqeel
Nov 22 at 22:15












Because you're ordering/filtering by createDate, and then pass in a UID. If you just want to sort on createDate drop the queryEqual(toValue: clause: rootRef.child("orders").queryOrdered(byChild: "createDate").observe(.childAdded. Note that unless you updated your data, you'll need creadeDate.
– Frank van Puffelen
Nov 22 at 22:51




Because you're ordering/filtering by createDate, and then pass in a UID. If you just want to sort on createDate drop the queryEqual(toValue: clause: rootRef.child("orders").queryOrdered(byChild: "createDate").observe(.childAdded. Note that unless you updated your data, you'll need creadeDate.
– Frank van Puffelen
Nov 22 at 22:51


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432894%2fsearch-about-user-orders-and-sort-by-date-newest-first-with-firebase%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo