UICollectionView founds nil when trigger again reloadData() - ios

When I what to remove all the actuall data from the collection view and add new data, pass the error Unexpectedly found nil while unwrapping an Optional value when trigger again reloadData(). Here's the code where pops up.
func getAllProducts(){
print("All products before getting again from the func: \(productsAll.count)")
productsAll.removeAll()
refProducts.observeSingleEvent(of: .value) { (snapshot) in
guard let productsDic = snapshot.value as? [String:AnyObject] else {return}
for(_, productSelected) in productsDic{
guard let name = productSelected["name"] as? String else {return}
guard let imageURLString = productSelected["image"] as? String else {return}
guard let price = productSelected["price"] as? String else {return}
guard let description = productSelected["description"] as? String else {return}
guard let imgURL = URL(string: imageURLString) else {return}
guard let data = try? Data(contentsOf: imgURL) else {return}
guard let img = UIImage(data: data) else {return}
let productData = Product(productImage: img, productName: name, productPrice: price, productDescription: description, productRecommended: false)
self.productsAll.append(productData)
print("We have \(self.productsAll.count) products in All products")
}
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}

Related

want to fetch data from firebase

I can fetch all data but don't know how to go inside the nodes
and fetch values. Here is the structure of my database want to fetch all data
func fetchData(){
ref = Database.database().reference()
let userid = Auth.auth().currentUser?.uid
ref.child(Constants.NODE_MAINTENANCE).child(userid!).child(Constants.NODE_MAINTENANCE_DATE).child(self.lastMaintenanceDateLbl.text ?? "").observe(DataEventType.value) { (snap) in
guard snap.exists()
else {
print("no data found at this date")
AppUtils.showAlert(title: "Alert", message: "No data found at this date!", viewController: self)
return}
// let maintenanceType = snapshot.value as? [String] ?? [""]
// print(maintenanceType)
if let snapshot = snap.children.allObjects as? [DataSnapshot]{
for snap in snapshot{
let maintenanceType = snap.value as? [String:Any]
for type in (maintenanceType?.values)!{
print(type)
}
}
}
}

Chat messages does not load properly

i have tried to create a chat message system but the old messages seems to still remain when the new messages are called.
Anyone can help? Furthermore, sometimes when a new user is created and he chat with another user.The messages from the other user is not reflected in the new user chat.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationItem.title = "Chat"
DispatchQueue.global(qos:.userInteractive).async {
DispatchQueue.main.async {
self.loadPosts()
self.loadPostsReceivedMessage()
}
}
}
//Get Message sent
func loadPosts() {
let senderIDNumber = Auth.auth().currentUser?.uid
let chatsRef = db.collection("chats").order(by: "timestamp", descending: false)
chatsRef.whereField("senderID", isEqualTo: senderIDNumber!).whereField("receiverID", isEqualTo: receiverIDNumber)
.addSnapshotListener { querySnapshot, error in
guard let documents = querySnapshot?.documents else {
print("Error fetching documents: \(error!)")
return
}
for document in documents {
let messageText = document.data()["message"] as? String
let senderIDNumber = document.data()["senderID"] as? String
let receiverIDNumber = document.data()["receiverID"] as? String
let timestamp = document.data()["timestamp"] as? String
guard let sender = document.data()["sender"] as? String else {return}
// let conversationsCounter = document.data()["conversationsCounter"] as? Int
guard let profileUrl = document.data()["profileUrl"] as? String else { return}
let chat = Chat(messageTextString: messageText!, senderIDNumber: senderIDNumber!, receiverIDNumber: receiverIDNumber!, timeStampString: timestamp!, profileImageUrl: profileUrl, senderString: sender)
self.chats.append(chat)
print(self.chats)
self.collectionView.reloadData()
}
}
}
//Get message received
func loadPostsReceivedMessage() {
/* let uid = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if let dic = snapshot.value as? [String: AnyObject]{
let currentUser = dic["username"] as? String
let senderIDNumber = Auth.auth().currentUser?.uid
} */
let chatsRef = db.collection("chats").order(by: "timestamp", descending: false)
print("thecurrentreceiver"+senderString)
print("thecurrentsender"+receiverIDNumber)
chatsRef.whereField("receiverID", isEqualTo: senderString).whereField("sender", isEqualTo: receiverIDNumber)
.addSnapshotListener { querySnapshot, error in
guard let documents = querySnapshot?.documents else {
print("Error fetching documents: \(error!)")
return
}
for document in documents {
let messageText = document.data()["message"] as? String
let senderIDNumber = document.data()["senderID"] as? String
let receiverIDNumber = document.data()["receiverID"] as? String
let timestamp = document.data()["timestamp"] as? String
// let conversationsCounter = document.data()["conversationsCounter"] as? Int
guard let profileUrl = document.data()["profileUrl"] as? String else { return}
guard let sender = document.data()["sender"] as? String else {return}
let chat = Chat(messageTextString: messageText!, senderIDNumber: senderIDNumber!, receiverIDNumber: receiverIDNumber!, timeStampString: timestamp!,profileImageUrl: profileUrl, senderString: sender)
print("whatisthemessage"+messageText!)
self.chats.append(chat)
print(self.chats)
self.chats.sort{$0.timestamp < $1.timestamp}
self.collectionView.reloadData()
}
}
}
You're using addSnapshotListener, which means that your callback gets called every time something relevant in the database changes. And when that happens, you loop over all the documents that match your query and add them to your view. This means that if there are multiple changes, you're adding most messages multiple times.
There are two common solutions:
Clear the view every time your callback gets called.
Only modify the view for the changes each time your callback gets called.
We'll use #2 below, since it is more efficient. Note that I'm only handling new messages to keep things simple. As you make your app more complete, you will also need to handle other types of changes, e.g. when a user deletes or updates a chat message.
let chatsRef = db.collection ("chats").order (by: "timestamp", descending: false)
chatsRef.whereField ("senderID", isEqualTo: senderIDNumber!)
.whereField ("receiverID", isEqualTo: receiverIDNumber)
.addSnapshotListener {
querySnapshot,
error in guard let documentChanges = querySnapshot?.documentChanges else {
print ("Error fetching documents: \(error!)")
return
}
for documentChange in documentChanges {
if (documentChange.type == .added) {
let data = documentChange.document.data ()
let messageText = data["message"] as? String
let senderIDNumber = data["senderID"] as? String
let receiverIDNumber = data["receiverID"] as? String
let timestamp = data["timestamp"] as? String
...
let chat = Chat (
messageTextString : messageText!,
senderIDNumber : senderIDNumber!,
receiverIDNumber : receiverIDNumber!,
timeStampString : timestamp!,
profileImageUrl : profileUrl,
senderString : sender
)
self.chats.append (chat)
print (self.chats)
self.collectionView.reloadData ()
}
}
}
For some more on this, have a look at responding to changes in the Firebase documentation.

In the mentioned url i need to get only first dictionary from the url?

In this order detail array i am having 10 dictionaries but i need to display only first dictionary can any one help me how to implement this ?
http://www.json-generator.com/api/json/get/bUKEESvnvS?indent=2
here is my code shown below
func downloadJsonWithURL() {
let url = NSURL(string: self.url)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
self.orderdetailsArray = (jsonObj!.value(forKey: "Orders detail") as? [[String: AnyObject]])!
for array in self.orderdetailsArray {
let key = "OrderId"
let value = "#1000501"
for (key,value) in array{
if let addressDict = array as? NSDictionary{
if let orderid = addressDict.value(forKey: "OrderId"){
self.orderid.append(orderid as! String)
}
if let orderdate = addressDict.value(forKey: "OrderDate"){
self.orderdate.append(orderdate as! String)
}
if let subtotal = addressDict.value(forKey: "SubTotal"){
self.subTotal.append(subtotal as! Int)
}
if let Shipping = addressDict.value(forKey: "Shipping"){
self.shippingPrice.append(Shipping as! Int)
}
if let tax = addressDict.value(forKey: "Tax"){
self.tax.append(tax as! Int)
}
if let grandtotal = addressDict.value(forKey: "GrandTotal"){
self.grandTotal.append(grandtotal as! Int)
}
if let shippingAddress = addressDict.value(forKey: "ShippingAddress"){
self.shippingAddress.append(shippingAddress as AnyObject)
}
if let shippingMethod = addressDict.value(forKey: "ShippingMethod"){
self.shippingMethod.append(shippingMethod as AnyObject)
}
if let billingAddress = addressDict.value(forKey: "BillingAddress"){
self.billingAddress.append(billingAddress as AnyObject)
}
if let paymentMethod = addressDict.value(forKey: "PayMentMethod"){
self.paymentMethod.append(paymentMethod as AnyObject)
}
self.itemsArray = addressDict.value(forKey: "Items detail") as! [[String : AnyObject]]
}
}
}
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
})
}
}).resume()
}
Do this. :
let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
guard let Ordersdetail = jsonObj["Orders detail"] as? [NSDictionary] else {
print("Cannot find key 'Orderdetails' in \(jsonObj)")
return
}
To access the contents of the first dictionary do this:
var orderid = Ordersdetail[0]["OrderId"]!
var shippingadress = Ordersdetail[0]["ShippingAddress"]!
var total = Ordersdetail[0]["GrandTotal"]!
var subtotal = Ordersdetail[0]["SubTotal"]!
var tax = Ordersdetail[0]["Tax"]!
var shipping = Ordersdetail[0]["Shipping"]!
Hi if you want first dictionary of that
self.orderdetailsArray
then
if let firstDictInfo = self.orderdetailsArray.first as? [String:Any] {
// Do your stuff here
print(firstDictInfo["OrderId"])
}
Instead of looping through the whole dictionary is dictionaries, you should just take the first dictionary and only parse that. There was also quite a few other conceptual problems with your code. In Swift, don't use NSDictionary, but use the native Swift version, Dictionary, which keeps the type information of its contents. Also, use conditional casting to make sure your program doesn't crash even if the received data is wrong/unexpected and don't use force unwrapping of optionals.
Also, when parsing a JSON response in Swift, in general it is not necessary and not a good idea to iterate through the key-value pairs of the dictionaries in the response. You should know what data structure you expect, otherwise you can't parse it properly and since you can directly access dictionary values in Swift if you know the key it corresponds to, there's no need to iterate through the dictionary in a loop.
func downloadJsonWithURL() {
let url = URL(string: self.url)
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = (try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)) as? [String:Any] {
guard let self.orderdetailsArray = jsonObj["Orders detail"] as? [[String: AnyObject]] else {return}
guard let firstOrderDetails = self.orderdetailsArray.first else {return}
let key = "OrderId"
let value = "#1000501"
if let ordered = firstOrderDetails["OrderId] as? String {
self.orderid.append(orderid)
}
if let orderdate = firstOrderDetails["OrderDate"] as? String{
self.orderdate.append(orderdate)
}
if let subtotal = firstOrderDetails["SubTotal"] as? Int{
self.subTotal.append(subtotal)
}
if let shipping = firstOrderDetails["Shipping"] as? Int{
self.shippingPrice.append(shipping)
}
if let tax = firstOrderDetails["Tax"] as? Int{
self.tax.append(tax)
}
if let grandtotal = firstOrderDetails["GrandTotal"] as? Int{
self.grandTotal.append(grandtotal)
}
if let shippingAddress = firstOrderDetails[ "ShippingAddress"] as? AnyObject{ //why don't you store it as a String?
self.shippingAddress.append(shippingAddress)
}
if let shippingMethod = firstOrderDetails[ "ShippingMethod"] as? AnyObject{
self.shippingMethod.append(shippingMethod)
}
if let billingAddress = firstOrderDetails[ "BillingAddress"] as? AnyObject {
self.billingAddress.append(billingAddress)
}
if let paymentMethod = firstOrderDetails ["PayMentMethod"] as? AnyObject{
self.paymentMethod.append(paymentMethod)
}
guard let itemDetails = firstOrderDetails["Items detail"] as? [[String : AnyObject]] else {return}
self.itemsArray = itemDetails
}
}
}
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
})
}
}).resume()
}
I haven't compiled and run the code, so make sure you check for any typos/inconsistencies. Also, make sure you change the types of the objects you store are AnyObjects to specific types.

How to import data from Firebase only once at login

My code is kind of buggy lately and I wonder if the methods I am using to retrieve data from Firebase are the correct methods to use. In short, I am retrieving data from firebase and than storing it inside an SQLite database.
This is my code:
FirebaseStore.rootRef.childByAppendingPath("users/"+FirebaseStore.rootRef.authData.uid+"/forums").observeSingleEventOfType(.Value, withBlock:{
snapshot in
guard let firebaseData = snapshot.value as? NSDictionary else {return}
guard let uids = firebaseData.allKeys as? [String] else {return}
importContext.performBlock{
for uid in uids{
guard let forum = NSEntityDescription.insertNewObjectForEntityForName("Forum", inManagedObjectContext: importContext) as? Forum else {return}
FirebaseStore.rootRef.childByAppendingPath("forums/"+uid+"/posts").queryOrderedByKey().observeSingleEventOfType(.Value, withBlock: {
snapshot in
// Saving the chat's messages
guard let data = snapshot.value as? NSDictionary else {return}
importContext.performBlock{
guard let posts = NSEntityDescription.insertNewObjectForEntityForName("Post", inManagedObjectContext: importContext) as? Post else {return}
do{
try importContext.save()
}catch let error{
// Error
}
}
})
}
}
})
}
I am not sure if I have to call this observeSingleEventOfType.

Crashing with observeForSingleEventOfType

I've got this method:
func fetchImageWithKey(key: String, completion: UIImage -> ()) {
imagesEndPoint.childByAppendingPath(key).observeSingleEventOfType(.Value, withBlock: { snapshot in
guard let imageString = snapshot.value["imageString"] as? String else { return }
guard let imageData = NSData(base64EncodedString: imageString, options: .IgnoreUnknownCharacters), image = UIImage(data: imageData) else { fatalError() }
completion(image)
})
}
Which is called each time a cell is dequeued in cellForRowAtIndexPath. For some reason, whilst scrolling through the tableView, this line guard let imageString = snapshot.value["imageString"] as? String else { return } will hit the else block.
I made sure that the ref does indeed have the key "imageString" and a value of type String in the end. I'm thinking it has something to do with the tableView cell dequeueing, but I'm not sure how I might approach this problem.
Any advice?
Are you sure: snapshot.value is dictionary.
You should check :
if let dic = snapshot.value as? NSDictionary{
guard let imageString = dic["imageString"] as? String else { return }
guard let imageData = NSData(base64EncodedString: imageString, options: .IgnoreUnknownCharacters), image = UIImage(data: imageData) else { fatalError() }
completion(image)
}else{
//
}

Resources