My Firebase database looks like this
This is the Rules of database
{
"rules": {
".read": "auth!=true",
".write":"auth!=true",
"users": {
"$uid": {
".indexOn": "lastTxt"
}
}
}
}
Currently there are 11 children in "rjs" nodes. I created a UITableView() to show the "conection", "displayName" & "img_url" of each rj and used this below code to achieve that.
class RJData {
var name: String?
var connection: String?
var url: String?
init(name: String?, connection: String?, url: String?) {
self.name = name
self.connection = connection
self.url = url
}
}
var rjUIDs = [String]()
var rjInfo = [RJData]()
func fetchInfoForAllRj()
{
Database.database().reference().child("rjs").observe(.value, with: { (snapshot) in
print("\n\n\n ----- snapshot = \(snapshot)\n\n\n")
for child in snapshot.children.allObjects as! [DataSnapshot]
{
let rjID = child.key
self.rjUIDs.append(rjID)
if let dictionary = child.value as? [String: AnyObject], let name = dictionary["displayName"] as? String, let connection = dictionary["connection"] as? String, let url = dictionary["img_url"] as? String
{
let data = RJData(name: name, connection: connection, url: url)
self.rjInfo.append(data)
}
}
DispatchQueue.main.async {
self.tableView_RJS.reloadData()
}
}, withCancel: nil)
}
I am caching image using this function
extension UIImageView {
public func imageFromURL(urlString: String) {
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicator.frame = CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
activityIndicator.startAnimating()
if self.image == nil{
self.addSubview(activityIndicator)
}
URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error ?? "No Error")
return
}
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
activityIndicator.removeFromSuperview()
self.image = image
})
}).resume()
}
}
It does my job but takes 30 to 50 secs to load the UITableView()
for the first time. After that it takes nearly 1 sec to load the UITableView()
, I mean it works fine after the first time.
What can I do to reduce the loading time? Any assistance would be appreciated.