Populate a collection view with firebase data - ios

I'm trying to recreate a new firebase project where you populate a table view with data from firebase realtime database that contain links to images in firebase storage.
I can populate the tutorial project which is a table view with firebase data. But with my current project it is a collection view inside an extension.
I've narrowed down the issue to my variables
var ref: FIRDatabaseReference!
var messages: [FIRDataSnapshot]! = []
var msglength: NSNumber = 10
private var _refHandle: FIRDatabaseHandle!
specifically
var messages: [FIRDataSnapshot]! = []
Which I think is an array of my data I get from firebase
I then call a function that should populate that array in my viewdidload()
func loadPosts(){
self.messages.removeAll()
// Listen for new messages in the Firebase database
_refHandle = self.ref.child("messages").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
//print("1")
self.messages.append(snapshot)
//print(self.messages.count)
})
}
The issue happens when I try to populate my collections view since I want horizontal scrolling I use an extension. In the extension I find that my array of values is always 0, but in my loadPosts() function the count of my >array is the same value as the amount of posts I have in firebase.
extension HomeViewController : UICollectionViewDataSource
{
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
print(messages.count)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StoryBoard.CellIdentifier, forIndexPath: indexPath) as! InterestCollectionViewCell
// Unpack message from Firebase DataSnapshot
let messageSnapshot: FIRDataSnapshot! = self.messages[indexPath.row]
let message = messageSnapshot.value as! Dictionary<String, String>
let name = message[Constants.MessageFields.name] as String!
if let imageUrl = message[Constants.MessageFields.imageUrl] {
if imageUrl.hasPrefix("gs://") {
FIRStorage.storage().referenceForURL(imageUrl).dataWithMaxSize(INT64_MAX){ (data, error) in
if let error = error {
print("Error downloading: \(error)")
return
}
cell.featuredImageView?.image = UIImage.init(data: data!)
}
} else if let url = NSURL(string:imageUrl), data = NSData(contentsOfURL: url) {
cell.featuredImageView?.image = UIImage.init(data: data)
}
cell.interestTitleLabel?.text = "sent by: \(name)"
}
return cell
}
}
Should I not be using FIRDataSnapshot? If so which is the correct one to use? Or should I approach the project in another form not using extensions?

You are correctly inserting the items into your array within the completion block, but you are missing a call to reload your collectionView.

Related

What am I doing wrong while populating this UITableView in Swift?

I am trying to populate a UITableView using an array and I am unable to do so. Here is what I have so far. This code is for retrieving data and storing it in the array that I am using to populate the UITableView:
func prepareForRetrieval() {
Database.database().reference().child("UserCart").child(Auth.auth().currentUser!.uid).observe(.value, with: {
(snapshot) in
for snap in snapshot.children.allObjects {
let id = snap as! DataSnapshot
self.keyArray.append(id.key)
}
self.updateCart()
})
}
func updateCart() {
for key in keyArray {
Database.database().reference().child("UserCart").child(Auth.auth().currentUser!.uid).child(key).observeSingleEvent(of: .value, with: {
(snapshot) in
let value = snapshot.value as? NSDictionary
let itemName = value?["Item Name"] as! String
let itemPrice = value?["Item Price"] as! Float
let itemQuantity = value?["Item Quantity"] as! Int
self.cartArray.append(CartData(itemName: itemName, itemQuantity: itemQuantity, itemPriceNumber: itemPrice))
print(self.cartArray.count)
})
}
}
The data is properly appending into the array and when I print the count of the array, it prints the correct count. This means that the data is there. However, when I try to populate a UITableView, it doesn't detect any data. I have the following code to make sure that there is data in the array before trying to populate the UITableView:
override func viewDidLoad() {
super.viewDidLoad()
cartBrain.prepareForRetrieval()
if cartBrain.cartArray.isEmpty == false{
tableViewOutlet.dataSource = self
tableViewOutlet.reloadData()
}
else {
tableViewOutlet.isHidden = true
tableViewOutlet.isUserInteractionEnabled = false
purchaseButtonOutlet.isEnabled = false
cartEmptyLabel.text = "Your cart is empty. Please add items and check back later."
}
}
When I open the View Controller, the TableView is disabled because it doesn't detect any data. I have already set the data source to self and the thing is that when the count of the array is printed, it again prints the correct amount. I have already set the data source to self for the UITableView. Here is my code for the UITableView:
extension CartViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cartBrain.cartArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cartcustomcell", for: indexPath)
cell.textLabel?.text = cartBrain.cartArray[indexPath.row].itemName
cell.detailTextLabel?.text = String(cartBrain.cartArray[indexPath.row].itemQuantity)
return cell
}
}
I don't understand why the count of the array prints the correct amount meaning that there is data stored in it but when the View Controller is loaded, it detects that the array is empty. Thanks for the help and I'm sorry if the question is a bit unclear.
After appending data to cartArray in updateCart you should reloadData(), like this:
weak var tableViewOutlet: UITableView?
func updateCart() {
for key in keyArray {
Database.database().reference().child("UserCart").child(Auth.auth().currentUser!.uid).child(key).observeSingleEvent(of: .value, with: {
(snapshot) in
let value = snapshot.value as? NSDictionary
let itemName = value?["Item Name"] as! String
let itemPrice = value?["Item Price"] as! Float
let itemQuantity = value?["Item Quantity"] as! Int
self.cartArray.append(CartData(itemName: itemName, itemQuantity: itemQuantity, itemPriceNumber: itemPrice))
DispatchQueue.main.async {
self.tableViewOutlet.reloadData()
}
})
}
}
The updateCart doesn't seem to have any connection to the tableViewOutlet so you need to pass in a reference to it in your viewDidLoad like this:
override func viewDidLoad() {
super.viewDidLoad()
cartBrain.tableViewOutlet = tableViewOutlet
cartBrain.prepareForRetrieval()
Note: Since you're using a for loop to trigger the async call multiple times you can use the array count to check if all the items are appended to do the reload to avoid multiple reloads.

Retrieving image from Firebase Storage placing in tableView not working,

I was able to display the image properly when i have just an imageView in a ViewController. I used this code:
islandRef.getData(maxSize: 1 * 1024 * 1024) { [weak self] data, error in
if let error = error {
print((error.localizedDescription))
// Uh-oh, an error occurred!
}
if let data = data{
self?.imageItem.image = UIImage(data: data)
}
}
}
I try a similar approach for my tableView, but its a little more complicated. I have two arrays that pull a string and an integer from my firebase document. I append these to the arrays items and prices. I am able to show these values in my tableView. I try the same thing when my pictues array. I am able to append an image to it. I then check if the pictures array has a count. It has a count of 1, but when i try to access it in tableview. It says the error: Thread 1: Fatal error: Index out of range. I don't understand why my other arrays have values that are usable, but this array does. I don't think there is a problem with the imageView because i can replaces pictures[indexPath.row] with my PlaceholderImage and it will properly show my placeholder image.
class ProfileViewController: UITableViewController {
var item = [String]()
var prices = [Int]()
var pricePicture = [Any]()
var db:Firestore!
var pictures = [UIImage]()
let storage = Storage.storage()
var placeholderImage = UIImage(named: "placeholder.jpg")
var imageMenu:UIImage?
override func viewDidLoad() {
getData()
let db = Firestore.firestore()
super.viewDidLoad()
}
func getData(){
let db = Firestore.firestore()
let docRef = db.collection("wine").document("pinot-noir-2017")
let storage = Storage.storage()
docRef.getDocument(source: .server) { (document, error) in
if let document = document {
let keys = document.data()?.keys
for key in keys!{
self.item.append(key)
self.pricePicture = document.data()![key] as! [Any]
self.prices.append(self.pricePicture[0] as! Int)
let stor = storage.reference()
let islandRef = stor.child("carbanet.jpg")
islandRef.getData(maxSize: 1 * 1024 * 1024) { [weak self] data, error in
if let error = error {
print((error.localizedDescription))
}
if let data = data{
self?.pictures.append(UIImage(data: data)!)
//this will print 1
print(self?.pictures.count ?? 0)
}
}
self.tableView.reloadData()
}
} else {
//print("Document does not exist in cache")
}
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return item.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
cell.textLabel?.text = item[indexPath.row] + " $" + String(prices[indexPath.row])
cell.imageView?.image = pictures[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let detailViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.detailViewController) as? DetailViewController{
show(detailViewController, sender: .none)
}
}
}
If someone could show me the error I am making that would be great. I'm pretty new to swift, so any help you can give me would be appreciated. I have read through the firebase documentation and watched their videos, but I can't figure out why it works in one scenario and not the other.
I think it was because i was trying to do it inside of trying to access a firebase document. I made another function getImage() and this time it worked.
func getImage(){
let stor = storage.reference()
let islandRef = stor.child("carbanet.jpg")
islandRef.getData(maxSize: 1 * 1024 * 1024) { [weak self] data, error in
if let error = error {
print((error.localizedDescription))
// Uh-oh, an error occurred!
}
if let data = data{
self?.pictures.append(UIImage(data: data as Data)!)
//this will print 1
print(self?.pictures.count ?? 0)
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}

ios Swift Firebase sending irregular data

I have a collectionView and ı am getting firebase database data. My collectionView create send firebase indexpath.row. This row 0 , 1, 2, 3, ... but firebase response irregular data and ı see debug mod indexpath.row 16 , 3, 7 , 11...
what is the problem ?
I share my sample code.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print(indexPath.row) // 1, 2, 3, 4
self.ref.child(userID!).child(self.islemString).child(String(indexPath.row + 1)).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let asd = value?["xxx"] as? Int ?? 0
print(indexPath.row) // 16, 3, 7 ,9
if(asd == 0) {
cell.imageKilit.image = UIImage(named: "kilit")
}
else{
cell.imageKilit.image = UIImage(named: "")
}
}
)
}
In response to the comment from the OP asking for example code:
Here's one example but I don't know what's stored in your Firebase so we will just use a messages structure as an example
messages
msg_0
msg: "Hello!"
msg_1
msg: "Another msg
and when the app starts we will have this code in the viewDidLoad to initially populate an array which will be used as a datasource for our collectionView, tableView etc.
var messagesArray = [String]()
var ref = //set to your firebase database
func viewDidLoad() {
let messagesRef = self.ref.child("messages")
messagesRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let msgDict = child.value as! [String: AnyObject]
let msg = msgDict["msg"] as! String
self.messagesArray.append(msg)
}
self.myCollectionView.reloadData()
})
}
then, in the collection view delegate methods
func collectionView(_ collectionView: UICollectionView, cellForItemAt in...
let msg = self.messagesArray[indexPath.row]
//do something with the msg
}
As you can see, as your scroll through the tableView, it's only pulling data from the dataSource array and not constantly hitting Firebase for each row of data. In this case we are using a simple string but it could be an array of message objects which could contain the message, the name of the sender and even their image.

Dynamically populating an iOS table view with Swift from an API

I'm currently in the process of creating an app to display the latest football scores. I've connected to an API through a URL and pulled back the team names for the english premier league into an array of strings.
The problem seems to come from populating the iOS table view that I intend to display the list of teams with. The data appears to be pulled from the API fine, but for some reason the TableView method which creates a cell and returns it doesn't seem to be called. The only time I can get the method to be called is when I actually hard code a value into the array of team names.
Here is my code:
class Main: UIViewController {
var names = [String]()
override func viewDidLoad() {
super.viewDidLoad()
let URL_String = "https://football-api.com/api/?Action=standings&APIKey=[API_KEY_REMOVED]&comp_id=1204"
let url = NSURL(string: URL_String)
let urlRequest = NSURLRequest(URL: url!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
(data, response, error) in
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let teams = json["teams"] as? [[String : AnyObject]] {
for team in teams {
if let name = team["stand_team_name"] as? String {
self.names.append(name)
}
}
}
} catch {
print("error serializing JSON: \(error)")
}
})
task.resume()
}
// Number of Sections In Table
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
// Number of Rows in each Section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
// Sets the content of each cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = names[indexPath.row]
return cell
}
}
Just wondering if anyone can point me in the right direction here. This code doesn't crash or throw any errors, it just refuses to load a table view. The only reason I can possibly think of is that the array of team names is empty after completing a request to the API. However I've set breakpoints throughout and checked the values of local variables and the desired information is being pulled from the API as intended...
you are in the correct way , just refresh the table using reloadData once you got the new data from API
if let teams = json["teams"] as? [[String : AnyObject]] {
for team in teams {
if let name = team["stand_team_name"] as? String {
self.names.append(name)
}
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.yourtableViewname.reloadData()
})
}

Parse, iOS, includeKey query does not retrieve attribute of pointer object

I'm quite new to working with Parse and I'm building a todo list as part of a CRM. Each task in the table view shows the description, due date, and client name. The description and due date are in my Task class, as well as a pointer to the Deal class. Client is a string in the Deal class. I'm able to query the description and due date properly, but I am not able to retrieve the client attribute from within the Deal object by using includeKey. I followed the Parse documentation for includeKey.
The description and due date show up properly in the resulting table view, but not the client. The log shows client label: nil and the printed task details include <Deal: 0x7ff033d1ed40, objectId: HffKOiJrTq>, but nothing about the client attribute. How can I retrieve and assign the pointer object's attribute (client) to my label within the table view? My relevant code is below. Thank you in advance.
Edit: I've updated my code with func fetchClients() based on this SO answer, but I'm still not sure whether my function is complete or where to call it.
class TasksVC: UITableViewController {
var taskObjects:NSMutableArray! = NSMutableArray()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
println("\(PFUser.currentUser())")
self.fetchAllObjects()
self.fetchClients()
}
func fetchAllObjects() {
var query:PFQuery = PFQuery(className: "Task")
query.whereKey("username", equalTo: PFUser.currentUser()!)
query.orderByAscending("dueDate")
query.addAscendingOrder("desc")
query.includeKey("deal")
query.findObjectsInBackgroundWithBlock { (tasks: [AnyObject]!, error:NSError!) -> Void in
if (error == nil) {
var temp:NSArray = tasks! as NSArray
self.taskObjects = temp.mutableCopy() as NSMutableArray
println(tasks)
self.tableView.reloadData()
} else {
println(error?.userInfo)
}
}
}
func fetchClients() {
var task:PFObject = PFObject(className: "Task")
var deal:PFObject = task["deal"] as PFObject
deal.fetchIfNeededInBackgroundWithBlock {
(deal: PFObject!, error: NSError!) -> Void in
let client = deal["client"] as NSString
}
}
//MARK: - Tasks table view
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.taskObjects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as TaskCell
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "M/dd/yy"
var task:PFObject = self.taskObjects.objectAtIndex(indexPath.row) as PFObject
cell.desc_Lbl?.text = task["desc"] as? String
cell.date_Lbl.text = dateFormatter.stringFromDate(task["dueDate"] as NSDate)
cell.client_Lbl?.text = task["client"] as? String
var clientLabel = cell.client_Lbl?.text
println("client label: \(clientLabel)")
return cell
}
}
If the deal column is a pointer then includeKey("deal") will get that object and populate it's properties for you. There is no need to perform a fetch of any type on top of that.
You really should be using Optionals properly though:
if let deal = task["deal"] as? PFObject {
// deal column has data
if let client = deal["client"] as? String {
// client has data
cell.client_Lbl?.text = client
}
}
Alternatively you can replace the last if let with a line like this, which handles empty values and uses a default:
cell.client_Lbl?.text = (deal["client"] as? String) ?? ""
In your posted cellForRowAtIndexPath code you are trying to read client from the task instead of from the deal: task["client"] as? String.

Resources