2

Im trying to retrieve all the rows from a Parse server table that contains strings and images. Each one of those will then be mapped to a swift array and then used within the application. When I run this code, my append to array updates the count within the scope of the query block but once outside this block of code - I have an empty array as was initialized.


var langArray = [String]()

override func viewDidLoad() {
        super.viewDidLoad()

        let query = PFQuery(className:"Languages")
        query.findObjectsInBackground { (objects, error) -> Void in
          if error == nil {
            for object in objects! {
                self.langArray.append(object["name"] as! String)
                print("inside this loop: \(self.langArray.count)")
            }
          } else {
            print(error!)
          }
        print("outside the foreach loop: \(langArray.count)")
        }


As per the code, Im running the query in the viewDidLoad() so it would be the first thing being retrieved.

4
  • Can you post the code you are using to access the array. I suspect you are having an issue with asynchronous data. You are probably calling the array before the server returns the data. Commented Oct 25, 2019 at 18:06
  • so the array is set as a variable that I am appending the returned data to. its in the snippet provided as the first line of code. Commented Oct 27, 2019 at 19:55
  • Yeah I can see that, my question is where you said "once outside this block of code - I have an empty array as was initialized." Can you post your code showing this discovery? Commented Oct 28, 2019 at 16:57
  • Added the print statements to see what i was getting as far as data being populated into the array Commented Oct 28, 2019 at 18:23

1 Answer 1

1

So the problem you are running into is asynchronous calls. You are correct the viewDidLoad is the first thing called. The problem is that your query.findinbackgroundwithblock is a background task. This means the task will execute and finish whenever the network allows. The without the background task the code would usually run one line after another. This specific calls tells the device not to wait for it to finish before moving on. This is causing your "outside the for loop" println to be called before your loop even begins. When the data is returned from parseserver the code inside the query block will begin to execute. This allows you to print out the data in the array once it is populated. If you are trying to do something else with this code aside from just print you will need more of a code snip it. My suggestion is to create a method outside of viewDidLoad and then do some logic that causes the for loop to call the function when it hits the end of the loop.

Sign up to request clarification or add additional context in comments.

2 Comments

ah yes! the 'inbackground'! I thinking the data Im trying to retrieve is small enough so that it shouldn't lag for the end user! thanks so much for catching that!
Glad to help, if you get stuck on further issues just post and new question

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.