0

Looking to retrieve value of custom class from a snap in swift like i do in java , i use Firebasedecoder . Works fine but i need the following structure

{

username = uiii;
email = test@rom.com

..}

If i make ordered requests like .queryOrdered(ByCHild:email).queryEqual("uiii"), i get the resquest with a previous node :

{

"hjhj"= {
username = uiii;
email = test@rom.com

..} }

Looking for a way to either remove the uneccessary values or to have the correct snap structure.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
it75
  • 125
  • 10

3 Answers3

0

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

To get to the individual node(s) in the result, you need to loop over snapshot.children, as shown in the Firebase documentation on listening for value events on a list of children.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I have only one result as planned. The issue is the structure of the result i can not handle . I tried to loop like this https://stackoverflow.com/questions/50847960/ios-firebase-database-get-key-of-value but i get errors saying NSEnumerator.element has no value for ... This is more the data structure that i don t know how to handle than the result itself – it75 Mar 15 '21 at 00:35
  • It doesn't matter how many results you have planned, if you're using a query the result is a list and you'll need to loop over the children. If you are having a hard time making the code from the links work, edit your question (there's a link right under it) to show the [minimal, complete/standalone code that anyone can run to reproduce where you go stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Mar 15 '21 at 01:04
0

In short, if you have extra data at the same level and that makes decodeFirebase crash, you still can use it:

let value = snapshot.value 
let modifiedValue:NSMutableDictionary = (value as AnyObject).mutableCopy() as! MutableDictionary

You then can remove elements by key: modifiedValue.removeObject(forKey: test) and then apply decode.

E_net4
  • 27,810
  • 13
  • 101
  • 139
it75
  • 125
  • 10
  • There is no reason at all to use `NSMutable...` collection types in Swift. – vadian Mar 22 '21 at 15:12
  • @ vadaian : I woulkd even say : there s no reason at all not to use NSMutable... – it75 Mar 23 '21 at 23:13
  • one more comment for the upvoter( or censor as you see it i vevn forgot to thank you for censoring me , my bad – it75 Apr 02 '21 at 00:56
-1

custom class USER with all values in the pictures

import Foundation import SwiftUI import Firebase import CodableFirebase

//knowing the userid , clean beautiful result with Firebasedecoder

func cleanResultWithCodebableFirebase(){

      ref.child("3oleg").observeSingleEvent(of: .value, with: { snapshot in
           guard let value = snapshot.value else { return }
           do {
               let user = try FirebaseDecoder().decode(User.self, from: value)
               print(user.getUser_id())
               
           } catch let error {
               print(error)
           }
       })

}

not knowing userID dirty result

func customwithdirtylists(){

let query = ref.queryOrdered(byChild: Strings.field_username).queryEqual(toValue: "uiiii") query.observeSingleEvent( of: .value, with: { (snapshot) -> Void in

   for child in snapshot.children {

     let childSnapshot = snapshot.childSnapshot(forPath: (child as AnyObject).key)

     for grandchild in childSnapshot.children{
         let grandchildSnapshot = childSnapshot.childSnapshot(forPath: (grandchild as AnyObject).key)
         //possible from here to get the key and values of each element of the custom class

         
     }
     

   }

})

}

This is the code i use in both cases, direct request or when ordered . No list visible when direct with the help of firebase decode .Ugly way to rebuild custom class thru looping . I m sure there are more elegant ways to do it especially when all i need is just remove one value of the direct result to have a clean result

it75
  • 125
  • 10