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
Jamie Barton
14,498 PointsFun FactBook App and Parse
Hi
I wanted to take the FactBook application using Swift a little further and integrate Parse.
I have successfully integrated Parse via their doc instructions and created a bridging header file but I'm not too sure on the process of integrating the quotes.
Would you recommend when the app is launched it downloads all of the Facts and stores them on the phone locally and then on each refresh the app calls from that array or it makes a new network call each time?
I'm not too sure which road is the best to go down... Memory Vs. Network.
Moving onto the actual code. If anyone has done anything similar it would be much appreciated for those wanting to take the Fun FactBook a little further, a brief overview of the code required to getting something to work!
Thanks :)
1 Answer
Stone Preston
42,016 Pointsit kind of depends on the number of facts you have. if you have a lot of facts it might take more time than you think is acceptable to download them all at once. if you have 20 or so getting them all when the app starts is probably fine.
I think I would get a new one from the backend each time.
Jamie Barton
14,498 PointsJamie Barton
14,498 PointsThanks. I have well over 200 quotes. I thought that would be far too much to store in memory but didn't know if the network calls would become a poor UX.
Stone Preston
42,016 PointsStone Preston
42,016 Pointsan array of 200 text quotes is not that big honestly. you should just try out both ways of doing it and see what you think is best. use a loading indicator whenever you get data from the backend to let the user know work is being done. both ways have disadvantages and advantages
Jamie Barton
14,498 PointsJamie Barton
14,498 PointsThanks. Do you have any experience using Parse?
I'm in the middle of adding it to my project and I'm doing something like this to add the quotes to an array.
I'm running that method inside
viewDidLoad()and thefactsDatais aNSMutableArraybut once I've got that. I'm unsure how I should be referencing to it within my other views and getting a random quote from there.self.quotesCount = self.quotesData.countappears to be 0 when I print it to the console. So either the query isn't working or at the time that code is ran, the array is ZERO because thefindObjectsInBackgroundWithBlockhasn't ran yet. I'm not sure when it has completed I can set thequotesCountThanks
Jamie Barton
14,498 PointsJamie Barton
14,498 PointsUpdate: I tried adding
But that also returns 0. The query I would imagine is running because the status bar displays a spinner but it appears it doesn't return anything.
Stone Preston
42,016 PointsStone Preston
42,016 PointsI would probably just use a regular array. looping over the objects and adding them one by one takes time compared to just setting an array property using the objects array.
also yes since the findObjects method runs in the background quotesCount is getting set before the quotesData. wait you have factsData and quotesData, what is the difference between those two?
if you have done the fun facts app you should have a model class. that is where this code needs to go.
Jamie Barton
14,498 PointsJamie Barton
14,498 PointsIgnore the two variable arrays. I tried creating another one for a moment using another name to see if it worked using another query but same result. I got rid of the model for now to try getting this to work in the controller. I'll try adding it to the model created during the course.
Stone Preston
42,016 PointsStone Preston
42,016 Pointsyeah you can get it to work in view did load. try moving the log into the findObjects function completion block after you add all the objects
Jamie Barton
14,498 PointsJamie Barton
14,498 PointsI think I'm thinking this too much here. I am not getting an error
NSArray does not have a member named 'addObject'Stone Preston
42,016 PointsStone Preston
42,016 Pointssorry, did you change your mutable array property to a regular array? are you using NSArray or a swift array? if you are using a swift array try
you may have to do some casting
self.quotesData = objects as [PFObject]Jamie Barton
14,498 PointsJamie Barton
14,498 PointsSorry! I noticed I had changed it from a
NSMutableArrayto aNSArray. Nothing prints out however in the console even when the above code is inside the viewDidLoad() method.Very strange!
Stone Preston
42,016 PointsStone Preston
42,016 Pointsoh i see whats wrong. your if condition is incorrect. you have
if error != nil {but you need
if error == nil {Jamie Barton
14,498 PointsJamie Barton
14,498 PointsOk. It's giving me an error back
unexpectedly found nil while unwrapping an Optional valueand that'll be because some of the facts have author fields and some don't. Is there a way to make this nil if it doesn't exist within the array?Edit: I got the query to work it seems but now pulling a random one from the array and choosing the
factLabel?.text = fact["fact"]doesn't seem to work.It's complaining about AnyObject does not have member named "subscript"