last day (14 days later) » 

2:22 AM
0
A: Images downloaded from firebase not scaling properly and not displaying until scroll on table view cells

PierceThe name of the UIImageView in your custom cell CompanyTableViewCell is companyImage, yet when you set the properties for your cell in cellForRowAt(_:) you are assigning the image for cell.imageView?.image instead of cell.companyImage.image which is your custom imageView that you set the constrai...

 
I tried the setupCell method, but I got "unexpectedly found nil while unwrapping an Optional value". I think it's because firebase is an asynchronous database, so company.image is nil for method calls (such as print and setupCell). Is there a way around this?
 
@15ongm - well you definitely need to do an asynchronous callback inside your Firebase method! You never want to do any UI execution on your background thread! Let me post an example. I will edit my answer
@15ongm - Please check my updated answer. See the recommended Firebase method changes for loadData, and also how I changed the setupCell(image:) method to pass in an optional UIImage.
 
I followed the code above. I tried customCell.companyImage.image = company.image and customCell.setUpImage(image: company.image). For some reason, the issue is worse. After building thee app, at first, I see that the table is empty and then everything is shows up except the company image. I still need to scroll down and then back up for the first cells' company images to show up
Also, I'm not sure if this helps, but when I'm running the app, the memory line continues to move to the right as added in my question.
 
How often are you calling loadData()? Do you call it more than once? And if so, do you ever clear the companies array?
 
No, I don't clear the companies array. loadData() is the last method to be called in viewDidLoad
 
2:24 AM
I'm having a hard time figuring out what the problem is, without being able to see everything, and I also don't have much Firebase experience. Let me ask you this, is this the first ViewController you load in your app? Is there any other functionality that happens before this where you could load the data, that way once this VC is presented, the data would already be loaded?
 
No, there are 4 view controllers (including the company view controller) that are loaded into a tab view controller. I click on the 3rd tab to bring up & load the company view controller. I'm not sure how to. Based on a lot of basic firebase tutorials, they usually load the data in the view controller that has the table view- probably because firebase loads data asynchronously
 
It's kind of a hack, it doesn't really solve this actual problem, but it should solve your problem. You could create a global property named companies thats an array of companies, and then when you first load the app, like in the very first ViewController in viewDidLoad call your loadData method, and delete the part at the end of the method that tells the table view to reload
Then when you eventually get to the part where the user loads the VC with the tableView, all the company data should already be totally loaded and it can just load it because it's all cached. Instead of waiting for all the data
So in the VC that has the table view, you would just setup your tableView and set the UITableViewDataSource to load from the global companies array
Do you know how to create a global property?
 
no, is it global var?
 
No all you have to do is declare it outside of your class, any one of your classes
So like in your first viewController when it says class ViewController: UIViewController {
Just declare it ABOVE that
Inbetween that and the import UIKit and other import statements
So you'd have something like import UIKit ... var companies:[company] = [] .... class ViewController: UIViewController {
Then that property would be accessible inside ANY class within your project
you just can't put self in front of it to access it, if you declare another property inside another class with the same exact name, it might cause conflict so be careful
 
2:39 AM
okay so I should delete DispatchQueue.main.async { self.companyTableView.reloadData() } from loadData() and then leave the rest of the updated code as is?
 
Yes
And then you should be able to load your VC with the tableView, and it'll set all the data from the companies array, because you still call it the same way companies[indexPath.row] ....
 
okay I tried that but it didn't work, I still need to scroll down and then scroll back up for the first cells' images to load
 
And you called loadData() right after the app launched?
Then I'm stumped
Give me a minute let me look at your code again
 
yup loadData() was called in viewDidLoad
 
Omg I feel kind of dumb
I think I know the problem
You're not de-queueing the cell
Okay just a sec
 
2:48 AM
De-queueing? In cellForRowAtIndexPath I did let cell = tableView.dequeueReusableCell(withIdentifier: CompanyTableViewCell.identifier, for: indexPath) and then let customCell = cell as! CompanyTableViewCell
I'm not sure if I included that in my question or not
 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let company = companies[indexPath.row]
let customCell = tableView.dequeueReusableCell(withIdentifier: "CompanyTableViewCell") as! CompanyTableViewCell
customCell.companyImage.image = company.image
return customCell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let company = companies[indexPath.row]
        let customCell = tableView.dequeueReusableCell(withIdentifier: "CompanyTableViewCell") as! CompanyTableViewCell
        customCell.companyImage.image = company.image
        return customCell
    }
Ignore that first one
Then when you create the tableView, wherever you instantiate it, add this tableView.register(CompanyTableViewCell.self, forCellReuseIdentifier: "CompanyTableViewCell")
That will definitely solve the memory problem
 
okay i'll try that rn
 
I don't know if that will solve the problem though if you say you already had a dequeue set up. Your original question didn't show that so I thought that might have been the issue. Still give it a try
 
hmm its still not working
I was looking up the issue and found this: github.com/rs/SDWebImage/issues/9
"With the default imageView of a TableViewCell, you need to use a placeholder image, otherwise the cell will init without the image view (title aligned to the left border with no margin for the image) and wont be refreshed once image is finish downloading."
Is my image view companyImage a custom image view or a default image view of table view cell?
 
I think that's just saying to have like a static image set for the UIImageView across all cells
So originally they could all load an image of a fish just as a placeholder, and then when the data gets refreshed, it'll repopulate the images with the right ones
fish is just an example. placeholder could be anything
Hey one other thing
I forgot you created your cell from a .xib so you need to change where you registered the cell identifier. I told you wrong
You want to say this
tableView.register(UINib(nibName: "<INSERT_XIB_FILE_NAME>", bundle: nil), forCellReuseIdentifier: "CompanyTableViewCell")
 
3:07 AM
oh okay, that helped the memory, its been reduced to 36.3 mb!
yeah the table view not showing the images until scroll is still not working
but maybe that's related to the source i found above?
 
Yeah because I really...I'm lost otherwise
 
oh wait actually as I scroll, the memory increases until it reaches 268 mb...
 
If it's some other reason I'm not seeing it
 
hmm it might be because I'm downloading the images from firebase
i think i'll have to look into it more
 
Are these incredibly large files?
 
3:11 AM
i just wanted to say thank you so much for all the help- its my first time building a much more complex app, so I've been kind of lost the past few days, so I really appreciate it a lot
 
Even if you were loading big files, your memory allocation should go down when the process is done. I've had problems like this before with TableView's
Yeah sure thing. Sorry I couldn't help you solve it better
 
theyre usually between 14-112 kb
*10-150 kb
np honestly your suggestions definitely improve my code/efficiency
 
Yeah that's not large at all
This is a strange problem. Let me think about it for a little while. If I have any other ideas I'll send you a message on here. If not good luck
 
okay that sounds good thank you!
 
3:34 AM
You're welcome
 

last day (14 days later) »