Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Adam Peruta
4,919 Pointsvalue not being retained in my method - help!
I have a struct containing a method that queries data from parse. I can add the info to my array, but if I try and print the array before it is retuned, it comes back as empty. I can print out in the for loop and see it incrementally, but not at the end. What is the problem?
func getAllTraditions() -> AnyObject{
var traditionsDict = [String: AnyObject]()
var traditionsDictArray: [Dictionary<String, AnyObject>] = []
let query: PFQuery = PFQuery(className: "Tradition")
query.orderByAscending("objectId")
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
traditionsDict["title"] = (object["traditionName"]) // add to the dictionary
traditionsDict["description"] = (object["traditionDescription"]) // add to the dictionary
traditionsDictArray.append(traditionsDict) // add to the array
}
}
})
print(traditionsDictArray) // this shows me an empty array!
return traditionsDictArray
}
4 Answers
Jhoan Arango
14,575 PointsHello :
Try putting your dictionary instance outside of the function. IF I am not mistaken, and I will do a deeper research on this, the store properties inside a function do not retain its information after the function was executed.
Try these two things:
- Move your print line inside the closure execution, see if that prints.
- if that does not work move your dictionary outside of the function, and into your struct, this way we can make sure the the dictionary instance retains it's data.
Please let me know if any of these solutions work.
Adam Peruta
4,919 PointsThank you for the response! You are correct: when I move the print line inside the closure, it prints out perfectly.
When I move the dictionary outside the function AND move the print line inside the closure, it works. But as soon as I move that print line outside the closure, it prints an empty array. So my question goes back to, how do I make sure it retains the data? Some googling brought me to this, but I don't understand what the solution is: http://stackoverflow.com/questions/27766843/getting-data-out-of-a-closure-in-swift
It is driving me nuts.
Jhoan Arango
14,575 PointsOk first things first:
Let's find something out..
Do this
// Your struct
struct YourStruct {
var traditionsDict = [String: AnyObject]()
var traditionsDictArray: [Dictionary<String, AnyObject>] = []
func getAllTraditions() -> AnyObject {
let query: PFQuery = PFQuery(className: "Tradition")
query.orderByAscending("objectId")
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
traditionsDict["title"] = (object["traditionName"]) // add to the dictionary
traditionsDict["description"] = (object["traditionDescription"]) // add to the dictionary
traditionsDictArray.append(traditionsDict) // add to the array
printDictionary()
}
}
})
return traditionsDictArray
}
// testing to see if it will print a value.
func printDictionary(){
print(traditionsDictArray) // this shows me an empty array!
}
}
Let me know what happens..
Adam Peruta
4,919 PointsThanks again for all your help. So I added this and it prints off the array every time it loops through. So the first time there is 1 thing in the array, then 2, then 3... and the last time all the items are there. But when I call this from my main view controller: let fullList = YourStruct().getAllTraditions() it is still an empty object. I posted this in another help forum and here is what someone says:
This is normal behavior because the findObjectsInBackgroundWithBlock is not complete by the time the last print is performed thus the empty array at that point. If you put the print at the very end of the block then the array is populated and this is where you would refresh a view or tableView.
I just don't know how to refresh the tableView I have based on this.
Jhoan Arango
14,575 PointsYes that is normal behavior, because you won't see an immediate result until the block finishes it its execution. So, if you are working with a tableView, then it has a method called reloadData().
That's how you will refresh the tableviews data. Try it.
Adam Peruta
4,919 PointsYep that works! I am still wondering if there is way to have the function return an object that isn't empty. I'll keep plugging along.
Jhoan Arango
14,575 PointsJust make sure you get both your array and dictionary out of the function, because every time you call that function if you still have the array and the dictionary inside of it, it will create a new instance of those arrays, and will return an empty object.