Slow CloudKit table scrolling - altering existing code? - swift

Below I have my existing query download and cell for table row code...
publicDB.perform(query, inZoneWith: nil)
{
(results, error) -> Void in
if (error != nil)
{
self.present(alert, animated: true, completion: nil)
}
else
{
for result in results!
{
self.restaurantArray.append(result)
}
OperationQueue.main.addOperation( { () -> Void in
self.tableView.reloadData()
}) } }}
downloadRestaurants()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell
let restaurant: CKRecord = restaurantArray[indexPath.row]
cell?.name?.text = restaurant.value(forKey: "Name") as? String
let asset = restaurant.value(forKey: "Picture") as! CKAsset
let data = try! Data(contentsOf: asset.fileURL)
_ = UIImage(data: data)
cell?.picture?.image = UIImage(data: data)
return cell!
}
When I run this code, the app remains functional but scrolling through the 10 or so table cells is incredibly choppy. I am unsure what is causing this - all records, each containing an image, are downloaded during the query download portion of the top function. However, a problem or concept I'm missing is ever present during runtime. What am I missing here? Lazy loading? cache? something else? Unsure at this point, so any help would be incredibly helpful.
Update 1:
I've updated my code with a large thank you going to Pierce. I've had to update my code ever so slightly from his answer to maintain a ckrecord array to segue over to another controller via - restaurantArray but also create a new array for the NSObject class - tablerestaurantarray to be displayed in the current table controller.
var restaurantArray: Array<CKRecord> = []
var tablerestaurantarray: [Restaurant] = []
for result in results!
{
let tablerestaurant = Restaurant()
if let name = result.value(forKey: "Name") as! String? {
tablerestaurant.name = name
}
// Do same for image
if let imageAsset = result.object(forKey: "Picture") as! CKAsset? {
if let data = try? Data(contentsOf: imageAsset.fileURL) {
tablerestaurant.image = UIImage(data: data)
}
}
self.tablerestaurantarray.append(tablerestaurant)
self.restaurantArray.append(result)
}
OperationQueue.main.addOperation( { () -> Void in
self.tableView.reloadData()
})
}
}
}
downloadRestaurants()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return restaurantArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell
let restaurant: Restaurant = tablerestaurantarray[indexPath.row]
cell?.name?.text = restaurant.name
cell?.picture?.image = restaurant.image
return cell!
}

The way your code is setup, whenever you scroll in your UITableView, your program is converting a CKAsset into Data, and then converting that into a UIImage, and that's within every cell! That's a rather inefficient process, so try creating an NSObject called something like Restaurant that has an image property, and when you go through all the records returned from your CKQuery, parse each record into a new Restaurant object. To create a new NSObject, go to File -> New -> File -> select 'Swift File' and add something like this:
import UIKit
class Restaurant: NSObject {
// Create a UIImage property
var image: UIImage?
// Add any other properties, i.e. name, address, etc.
var name: String = ""
}
Now for your query:
// Create an empty array of Restaurant objects
var restaurantArray: [Restaurant] = []
publicDB.perform(query, inZoneWith: nil) { (results, error) -> Void in
if (error != nil) {
self.present(alert, animated: true, completion: nil)
} else {
for result in results! {
// Create a new instance of Restaurant
let restaurant = Restaurant()
// Use optional binding to check if value exists
if let name = result.value(forKey: "Name") as! String? {
restaurant.name = name
}
// Do same for image
if let imageAsset = result.object(forKey: "Picture") as! CKAsset? {
if let data = try? Data(contentsOf: imageAsset.fileURL) {
restaurant.image = UIImage(data: data)
}
}
// Append the new Restaurant to the Restaurants array (which is now an array of Restaurant objects, NOT CKRecords)
self.restaurantArray.append(restaurant)
}
OperationQueue.main.addOperation( { () -> Void in
self.tableView.reloadData()
})
}
}
Now your cell setup is much simpler:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell
let restaurant: Restaurant = restaurantArray[indexPath.row]
cell?.name?.text = restaurant.name
cell?.picture?.image = restaurant.image
return cell!
}

You should use CKQueryOperation in order to implements pagination for your UITableView.
You have to set the resultLimit property to a number equals to the cell quantity visiable at one time on you table plus 3 or 4
Set recordFetchedBlock property where you have to implement the code that will apply to one CKRecord
Set queryCompletionBlock property. This is the most important part on your pagination code because this closure receive an Optional CKQueryCursor parameter.
If this CKQueryCursor is nil then you have reach the last record available for you query but if it's a non nil value, then you have more records to fetch using this CKQueryCursor as indicator to your next fetch.
When user scroll on your TableView and reach the last element you should perform another fetch with CKQueryCursor.
Other performance advice is CKAssets should be treated on separated execution queues.

Related

display array in a label in swift

I would like two display Object data gotten from Parse in swift. I have tried using label in this way but it only displays the last element in the object. Please how can I make it display all the element in the object in the label. Like one element to one label. Thanks
let query = PFQuery(className: "Questionnaire")
query.findObjectsInBackground { (objects, error) -> Void in
if error == nil {
// There were no errors in the fetch
if let returnedObjects = objects {
// var text = ""
// Objects Array is not nil
// loop through the array to get each object
for object in returnedObjects {
print(object["question"] as! String)
// text.append(object["question"] as! String)
self.Label.text = (object["question"] as! String)
}
}
}
}
You can do in one line like that and join all question with , separator , you can change separator to any (empty, -,...etc)
if let returnedObjects = returnedObjects {
self.Label.text = returnedObjects.map {($0["question"] as? String) ?? nil}.compactMap({$0}).joined(separator: ",")
}
Use tableview for this.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! YouTableViewCell
cell.textLabel.text = yourArray[indexpath.row] as? String ?? ""
return cell
}
If it's important to use UILabel
var concatenatedString = ""
for object in returnedObjects {
concatenatedString += object["question"] as! String
}
self.Label.text = concatenatedString
You are looping through the array and setting each value to Label.text. However, setting Label.text will replace what was on the label before. That's why you only see the last item.
One solution is to display the string representation of the array:
self.Label.text = "\(object)"
Another solution is to display the items in a table view Suganya Marlin has suggested. You would need to conform to UITableViewDatasource and implement the various methods. Here is a guide.

Swift showing Firebase duplicating data (.observeChildAdded) not working

Each time I add a new post to the database, the amount of times the posts show is increased by one. For instance, when I add one new post, the number of times the posts are duplicated is once. When I add another post (the view is reloaded) I see all the posts three times. I assume that the problem is with the function fetchPosts(), as each time the view loads it collects all the data from the firebase and appends it to the array. I have already tried emptying the array in the view did load, but that only makes all the posts show even more times. Also, I have tried using observe(.childAdded) and that results in no posts showing at all.
var ref: DatabaseReference!
var postList = [Post]()
var refHandle : UInt!
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
checkForSignedIn ()
ref = Database.database().reference().child("posts")
fetchPosts()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PostTableViewCell
//set cell content
let contentOfCellPost = postList[indexPath.row]
cell.label?.text = contentOfCellPost.post_words
cell.revealCount.text = contentOfCellPost.Reveals
return cell
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let tableSize = tableView.bounds.height
return tableSize
}
func fetchPosts () {
let query = ref.queryOrdered(byChild: "timestamp").queryLimited(toFirst: 10)
query.observe(.value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
if let value = child.value as? NSDictionary {
let post = Post()
let poster = value["poster"] as? String ?? "Name not found"
let post_content = value["post"] as? String ?? "Content not found"
let post_reveals = value["Reveals"] as? String ?? "Reveals not found"
post.post_words = post_content
post.poster = poster
post.Reveals = post_reveals
post.postID = child.key
self.postList.append(post)
print (post.post_words ?? "none")
DispatchQueue.main.async { self.tableView.reloadData() }
//make this for when child is added but so that it also shows psots already there something like query.observre event type of
}
}
}
}
The console log at first, for instance, will show the correct number of posts :
Thrice
Twice
Once
Tttt
Bloop
Decimal
9
7
3
When I add a new post, it shows this:
Tattoo
Thrice
Twice
Once
Tttt
Bloop
Decimal
9
7
3
Tattoo
Thrice
Twice
Once
Tttt
Bloop
Decimal
9
7
3
Tattoo
Thrice
Twice
Once
Tttt
Bloop
Decimal
9
7
3
Tattoo
Thrice
Twice
Once
Tttt
Bloop
Decimal
9
7
3
You need to clear your model (self.postList) at the beginning of the .observe block like so:
query.observe(.value) { (snapshot) in
self.postList.removeAll() //or however you can clear it
for child in snapshot.children.allObjects as! [DataSnapshot] {
if let value = child.value as? NSDictionary {
let post = Post()
let poster = value["poster"] as? String ?? "Name not found"
let post_content = value["post"] as? String ?? "Content not found"
let post_reveals = value["Reveals"] as? String ?? "Reveals not found"
post.post_words = post_content
post.poster = poster
post.Reveals = post_reveals
post.postID = child.key
self.postList.append(post)
print (post.post_words ?? "none")
DispatchQueue.main.async { self.tableView.reloadData() }
//make this for when child is added but so that it also shows psots already there something like query.observre event type of
}
}
Currently, each time the database is updated with a post, you add all posts to your model once again. Therefore you must clear your model each time you fetch all posts.
The reason why this doesn't work in viewDidLoad is because viewDidLoad is called only once, in the beginning, and not everytime the view appears -- thus the data will not be cleared upon adding a post.
Alternatively, you can use .childAdded -- but then you need to change the way you parse it because each snapshot with .childAdded returns a single post, not all the posts together.

Read the value of an attribute of a Core Data entity

I'm trying to read the value of an attribute (called 'name') of an entity (called 'List'), stored in a Core Data database. The issue that I have is that it says that the value of that attribute is nil, which should be a String.
My code for retrieving all List-entities is as follows:
container?.performBackgroundTask { [weak self] context in
self?.wordLists = try! List.all(in: context)
DispatchQueue.main.async(execute: {
print("Main queue available, reloading tableview.")
self?.wordListSelector.reloadData()
})
}
class func all(in context: NSManagedObjectContext) throws -> [List] {
let listRequest: NSFetchRequest<List> = List.fetchRequest()
do {
let list = try context.fetch(listRequest)
print(list)
return list
} catch {
print("error")
throw error
}
}
This prints:
[<__Words.List: 0x6000000937e0> (entity: List; id: 0xd00000000004000c <x-coredata://999D0158-64BD-44FD-A0B1-AB4EC03B9386/List/p1> ; data: <fault>), <__Words.List: 0x600000093a60> (entity: List; id: 0xd00000000008000c <x-coredata://999D0158-64BD-44FD-A0B1-AB4EC03B9386/List/p2> ; data: <fault>), <__Words.List: 0x600000093ab0> (entity: List; id: 0xd0000000000c000c <x-coredata://999D0158-64BD-44FD-A0B1-AB4EC03B9386/List/p3> ; data: <fault>)]
Which shows me that there should be 3 lists in the database, which is as expected.
I have created a variable like this:
var wordLists: [List] = [] {
didSet {
print("Detected wordList update, waiting for main queue.")
DispatchQueue.main.async(execute: {
print("Main queue available, reloading tableview.")
self.wordListSelector.reloadData()
})
}
}
This variable holds the List-entities that I retrieved by calling that all() function which I mentioned previously.
The following two methods will fill my TableView:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRowsInSection: \(wordLists.count).")
return wordLists.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "CategoryCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
print("cellForRowAt: \(indexPath.row) has name \(wordLists[indexPath.row].name).")
cell.textLabel?.text = wordLists[indexPath.row].name
return cell
}
This prints the following:
Detected wordList update, waiting for main queue.
Main queue available, reloading tableview.
numberOfRowsInSection: 3.
cellForRowAt: 0 has name nil.
cellForRowAt: 1 has name nil.
cellForRowAt: 2 has name nil.
Why is the name nil? Is this because the data is still 'faulted'? By viewing topics online I thought that Core Data automaticly unfaulted its data when you try to access it. What am I doing wrong?
Edit:
If I change the didset to the following:
var wordLists: [List] = [] {
didSet {
print("Wordlist was updated.")
for wordList in wordLists {
print(wordList)
print(wordList.name)
}
}
}
It does print the names (Optional("nameofitem1")). In the cellForRowAt it still prints 'nil'.
What you do looks like a good job for the NSFetchedResultsController. That will also help with the threading you might mix up as suggested in the comments. There is really good documentation on the NSFetchedResultsController

How to make a prototype cell 'disappear' when it is 'empty' (not clickable)

I am making an application where the user can see certain items/users within his x km radius (much like Tinder where you can set the radius of girls/guys you want to see in your area). So in my cellForRowAtIndexPath function I am determining whether a cell can be shown.
If he is in the radius, the event is shown. If the location is too far away, it shouldn't be using a cell.
My current code just hides the cell, but it is still clickable. I want it to NOT use a cell in the first place, but I couldn't find how to do it. Any ideas?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> AllGeneralMeetsTableViewCell {
//get location from backend
let locLati = object?["coordLat"] as? Double
let locLongi = object?["coordLong"] as? Double
let currentLocation:CLLocation = CLLocation(latitude: localLati , longitude: localLongi)
let meetLocation:CLLocation = CLLocation(latitude: locLati! , longitude: locLongi!)
let meters:CLLocationDistance = currentLocation.distanceFromLocation(meetLocation)
// make distance in km
let distInKm = meters/1000
//get distance that user has set in his settings
let cell = tableView.dequeueReusableCellWithIdentifier("GeneralMeetsIdentifier") as! AllGeneralMeetsTableViewCell!
if (distInKm <= settingsKm) {
// Extract values from the PFObject to display in the table cel
if let title = object?["title"] as? String {
cell?.titleCell?.text = title
}
if let message = object?["message"] as? String {
cell?.messageCell?.text = message
}
if let image = object?["image"] as? PFFile {
image.getDataInBackgroundWithBlock({ (imageData: NSData?,error: NSError?) -> Void in
if (error == nil) {
let image1 = UIImage(data:imageData!)
cell.imageCell.image = image1
}
})
}
return cell
}
else {
return cell
}
}
}
The data is being returned by the following query
override func queryForTable() -> PFQuery {
let query = PFQuery(className: self.parseClassName!)
let FBID = myUser.objectForKey("facebookID")!
query.whereKey("facebookID", equalTo: FBID)
query.whereKey("private", equalTo: "false")
return query
}
In your cellForRowAtIndexPath-
if cell is to be displayed {
tableView.rowHeight = 120
//Replace 120 with desired rowHeight
} else {
tableView.rowHeight = 0
}
Hope this helps :-)
You want to make the cell disappear when there is no data. If I am right, then you can use this library called as 'DZNEmptyDataSet' to display some images telling the user that there is no data to load. Use Cocoapods to install it, or just drag the files and create a bridging header. The usage is pretty straightforward as well - just follow the documentation at the GitHub.
The proper solution is to remove the data from your data source so that it won't be displayed in the first place. But sometimes this is a lot of work.
If you just want to hide a cell you can give it a zero height using the tableView(_:heightForRowAtIndexPath:) delegate method:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 1 {
return 0.0
}
return tableView.rowHeight
}
Parse provides a type PFGeopoint and it is even supported in the Query, with Parse framework you can also obtain location the same way you download stuff from database, this way you can download only those posts that are available for the user in his range...
PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil { //asynchronous, U have access to GPS location
// create Parse Query
let query: PFQuery = PFQuery(className: "Post")
// 5km range
query.whereKey("gps", nearGeoPoint: geoPoint, withinKilometers: 5.0)
//rest of query....
} else { // dont have access to GPS or whatever
print("Eror location: \(error)")
}
}

Parse Issue with the display of related relational values

Now I'm doing a normal topic and comment integrated app. Already carried out the functions like posting topic, posting comment, and setting the pointer of my comment to the Topic class.
But now the problem that trouble me now for the past few days, is the comments displayed cannot be classified to each topic, they are all gathering together no matter what different topic they're from.
My PFObjects are Topics and Comment, two classes totally.
Topics has objects:Title, content and user.
Comment has objects: content, parent(pointer to Topics) and user.
Already check out the parse database, seeming like each comment has a same parent key from its own topic's object ID. But still wonder why they cannot be shown accordingly.
Here's my coding. I'd be grateful if ones could lend ones' helping hand.
var timelineCommentData:NSMutableArray = NSMutableArray()
func loadData(){
timelineCommentData.removeAllObjects()
var findCommentData:PFQuery = PFQuery(className: "Comment")
findCommentData.findObjectsInBackgroundWithBlock({
(objects:[AnyObject]!,error:NSError!)->Void in
if (error == nil) {
for object in objects {
self.timelineCommentData.addObject(object)
}
let array:NSArray = self.timelineCommentData.reverseObjectEnumerator().allObjects
self.timelineCommentData = array.mutableCopy() as NSMutableArray
self.tableView.reloadData()
}
})
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return timelineCommentData.count + 1 //+1 is for displaying the topic
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = indexPath.row == 0 ? "storyCell" : "CommentCell"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier) as UITableViewCell
if let storyCell = cell as? TopicTableViewCell{
storyCell.titleLabel.text = topic?.objectForKey("title") as? String
storyCell.contentLabel.text = topic?.objectForKey("content") as? String
storyCell.timestampLabel.text = timeAgoSinceDate(topic!.createdAt, true)
}
storyCell.delegate = self
}
if let CommentCell = cell as? CommentsTableViewCell {
let comment:PFObject = self.timelineCommentData.objectAtIndex(indexPath.row - 1) as PFObject ///// Is anything wrong here??
var query = PFQuery(className: "Comment")
query.whereKey("parent", equalTo:topic)
query.findObjectsInBackgroundWithBlock({
(objects:[AnyObject]!,error:NSError!)->Void in
if (error == nil) {
CommentCell.commentLabel.text = comment.objectForKey("commentContent") as? String /////Or here
CommentCell.timeLabel.text = timeAgoSinceDate(comment.createdAt, true)
}
else{
println("no comment")
}
})
CommentCell.delegate = self
}
return cell
}