0

Through Firebase I'm calling many url to convert it to UIImage but aren't displayed in a corrected order. The print func stamp to the consolle the current index:

0 2 1 3 4

The JSON is like:

{
    "evento_1" = "http://.altervista.org/evento1.jpeg";
    "evento_2" = "http://.altervista.org/evento2.jpeg";
    "evento_3" = "http://.altervista.org/evento3.jpeg";
    "evento_4" = "http://.altervista.org/evento4.jpeg";
    "evento_5" = "http://.altervista.org/evento5.jpeg";
}

Function to get the data:

ref.observeEventType(.Value, withBlock: { snapshot in

            let nsDictionary = snapshot.value as? NSDictionary


            for (key, value) in nsDictionary! {
                dict[key as! String] = value
            }

            var index = 0


            for (_, url ) in dict {

                self.loadImage(url as! String, i: index)
                index++

            }

            }, withCancelBlock: { error in
                print(error.description)
        })

Image Loader func:

func loadImage(urlString:String,i:Int)
{      
    let url = NSURL(string: urlString)
    let data = NSData(contentsOfURL: url!)

    self.eventi[i].image = UIImage(data: data!)
 }
6
  • You're making asynchronous calls, so the result will always appear in undefined order (by definition, because it's async). What you can do is sort the values once you've downloaded everything and before displaying them. Commented Feb 29, 2016 at 15:26
  • Question edited to a Synchronous, same problem. Commented Feb 29, 2016 at 15:39
  • 3
    And now you've changed your code to synchronous calls... Which one are you actually using?! Anyway: a dictionary has no order, it is not an array. So looping over a dictionary will always give results unordered, even whith synchronous calls. Commented Feb 29, 2016 at 15:39
  • Ah ok. So how could I can sort the Dictionary? Commented Feb 29, 2016 at 15:43
  • Why not just put it on an array of images within the right order? Commented Feb 29, 2016 at 16:32

2 Answers 2

1

Put your Firebase snapshot keys into an array and the key:value pairs into a dictionary. Then sort the key array:

arrayOfKeys = Array(dict.keys) //swift
arrayOfKeys.sort {
  return $0 < $1
}

then you can iterate over the array to get the event name (the key), which corresponds to the objects in your dictionary (access it by key) which returns it's value.

or (and I like this better)

Just take every .value dictionary and put each into an array then sort the array by the eventName (assuming that's the key)

eventArray.sort({ $0.eventName > $1.eventName })
Sign up to request clarification or add additional context in comments.

Comments

0

As Jay and Eric mention: dictionaries have no defined order. The FDataSnapshot does have a defined order, but once you convert it to a dictionary, you're throwing that away.

So instead of first converting to a dictionary and then looping, you should simply loop over the children of the snapshot with:

for child in snapshot.children {
   let key = child.key as String

   print(key);
}

Where I print the key, you can do whatever you need with the child snapshot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.