Query within query not appending with Swift/Parse - swift

I hope someone can help because this is probably a simple problem, but my Parse query within a query prints the object I am looking for, but cannot append it to the array I need it in to retrieve it in a UITableView.
The error is "fatal error: index out of range" when I dequeue the results of the query in a cell.
Here's the code:
import UIKit
import Parse
import Bolts
class MessagesTableVC: UITableViewController {
var usernames = [String]()
var sentDate = [NSDate]()
var details = [String]()
var userImage = [PFFile]()
#IBAction func backToProfile(sender: AnyObject) {
self.performSegueWithIdentifier("messagesToProfile", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
let messagesQuery = PFQuery(className: "Messages")
messagesQuery.whereKey("recipientId", equalTo: PFUser.currentUser()!.objectId!)
messagesQuery.includeKey("senderId")
messagesQuery.orderByDescending("createdAt")
messagesQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error != nil {
print(error)
}
if error == nil {
if let objects = objects {
self.usernames.removeAll(keepCapacity: true)
self.sentDate.removeAll(keepCapacity: true)
self.details.removeAll(keepCapacity: true)
self.userImage.removeAll(keepCapacity: true)
for object in objects {
self.sentDate.append(object.createdAt! as NSDate)
if (object["item"] != nil) {
self.details.append(object["item"] as! String)
} else {
self.details.append(object["request"] as! String)
}
let senderObject = (object["senderId"] as! PFUser)
let senderId = (senderObject.objectId! as String)
print(senderId)
// Query for sender info
let userQuery = PFUser.query()
userQuery?.whereKey("objectId", equalTo: senderId)
userQuery?.getFirstObjectInBackgroundWithBlock({ (object, error) in
self.usernames.append((object!["username"] as! String))
//self.userImage.append(object!["profilePicture"] as! PFFile)
})
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
//self.search.resignFirstResponder()
}
}
}
})
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return sentDate.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("messageCell", forIndexPath: indexPath) as! MessageTableViewCell
//print(usernames[indexPath.row])
cell.senderUsername.text = usernames[indexPath.row]
cell.itemOrPreview.text = details[indexPath.row]
let date = sentDate[indexPath.row]
let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.timeStyle = .ShortStyle
let dateString = formatter.stringFromDate(date)
cell.sentDate.text = dateString
//userImage[indexPath.row].getDataInBackgroundWithBlock { (data, error) in
// if let downloadedItemImage = UIImage(data: data!) {
// cell.senderImage?.image = downloadedItemImage
//}
//}
return cell
}
override func viewWillAppear(animated: Bool) {
tableView.reloadData()
}
}

As I may guess, you might get inconsistent number of elements in sentDate and usernames because you append to usernames via asynchronous getFirstObjectInBackgroundWithBlock method, so, by the time you call reloadData on tableView, all user names might not be yet added to usernames. You might have less items in usernames then in sentDate by the time your tableView callback fires and in numberOfItems you return number of items in sentDate.
In order to fix that you need at first refactor your code, it has a lot of places things might go wrong. I won't give any specific advise, but, seems, you might want to wait before you get all the data before you reload your tableView.

Related

CoreData gets added to Table View AGAIN Every Time I Show View Controller (duplicates data)

I followed this tutorial
https://www.youtube.com/watch?v=35mKM4IkHS8&lc=UgztyK4XjUuAOrKk0XJ4AaABAg.9LtwRc_M0Gv9Nt8GIlAzDo
Basically I made a NotePad App that has a core data save function.
I made this app on another view controller
So There is MainViewController > NoteViewViewController
The first time I click the notepad section it loads core data perfectly well, but if I close out the NoteView and reopen it -- it duplicates all the saved Notes in Core Data
Here is the. Note ViewController
import UIKit
import CoreData
var noteList = [Note]()
class NoteTableView: UITableViewController
{
func nonDeletedNotes() -> [Note]
{
var noDeleteNoteList = [Note]()
for note in noteList
{
if(note.deletedDate == nil)
{
noDeleteNoteList.append(note)
}
}
return noDeleteNoteList
}
var firstLoad = true
override func viewDidLoad() {
if(firstLoad == true)
{
firstLoad = false
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Note")
do {
let results:NSArray = try context.fetch(request) as NSArray
for result in results
{
let note = result as! Note
noteList.append(note)
}
}
catch
{
print("Fetch Failed")
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell
{
let noteCell = tableView.dequeueReusableCell(withIdentifier: "noteCellID", for: indexPath) as! NoteCell
let thisNote: Note!
thisNote = nonDeletedNotes()[indexPath.row]
noteCell.titleLabel.text = thisNote.title
noteCell.descLabel.text = thisNote.desc1
return noteCell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return nonDeletedNotes().count
}
override func viewDidAppear(_ animated: Bool) {
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
self.performSegue(withIdentifier: "editNote", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if(segue.identifier == "editNote")
{
let indexPath = tableView.indexPathForSelectedRow!
let noteDetail = segue.destination as? FocusWheelViewController
let selectedNote : Note!
selectedNote = nonDeletedNotes()[indexPath.row]
noteDetail!.selectedNote = selectedNote
tableView.deselectRow(at: indexPath, animated: true)
}
}
}
I'm sure there is a common solution but I'm not sure what it is and wasn't able to follow the posts asking similar questions as my code was different and I truthfully don't understand the mechanics well enough to apply other answers to this
I found the easiest solution was to just add these two lines so the table view refreshed every-time, then loaded the data
noteList.removeAll()
tableView.reloadData()
So the code looks something like this:
var firstLoad = true
override func viewDidLoad() {
if(firstLoad == true)
{
noteList.removeAll() //NEWCODE
tableView.reloadData() //NEWCODE
firstLoad = false
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Note")
do {
let results:NSArray = try context.fetch(request) as NSArray
for result in results
{
let note = result as! Note
noteList.append(note)
}
}
catch
{
print("Fetch Failed")
}
}
}
The problem is var firstLoad = true. Because every time the controller start, firtLoad always true and the app will get data from Coredata and append to noteList.
The solution is UserDefaults. The first time when you run app, firstLoad always true. So you need to save the value bool of firstLoad to UserDefaults
// Set
UserDefaults.standard.setValue(true, forKey: "firstLoad")
// Get
UserDefaults.standard.bool(forKey: "firstLoad")
import UIKit
import CoreData
class NoteTableView: UITableViewController{
var noteList = [Note]()
func nonDeletedNotes() -> [Note]{
var noDeleteNoteList = [Note]()
for note in noteList {
if(note.deletedDate == nil) {
noDeleteNoteList.append(note)
}
}
return noDeleteNoteList
}
override func viewDidLoad() {
if noteList.count == 0 {
if(UserDefaults.standard.bool(forKey: "firstLoad") == true){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Note")
do {
let results:NSArray = try context.fetch(request) as NSArray
for result in results
{
let note = result as! Note
noteList.append(note)
UserDefaults.standard.setValue(false, forKey: "firstLoad")
}
}
catch {
print("Fetch Failed")
}
}
} {
else {
UserDefaults.standard.setValue(false, forKey: "firstLoad")
}
}
}
}
And maybe you need to check duplicate value when get data from CoreData.

Definition conflicts with previous value - also, when is override deleted?

So I am attempting to make a tableview out of an array of cloudkit downloaded items. However, I'm having this simple little "definition conflicts with pervious value" error. This error occurs for the cellForRowAtIndexPath function - which supposedly conflicts with the numberOfRowsInSection function. I have attempted moving/deleting brackets of the latter function and by placing a question mark/optional at the end... "-> UITableViewCell?" to no avail. Where could the error be coming from?
Also, as it stands, xcode deleted the override portion of each tableview function. Why does it sometimes stay and when is it deleted?
import UIKit
import CloudKit
class DiningTable: UITableViewController {
var categories: Array<CKRecord> = []
override func viewDidLoad() {
super.viewDidLoad()
func getRecords()
{
categories = []
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "DiningTypes", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["Name", "Address", "Picture"]
queryOperation.qualityOfService = .UserInteractive
queryOperation.recordFetchedBlock = { (record:CKRecord) -> Void in
let categoryRecord = record
self.categories.append(categoryRecord)
}
queryOperation.queryCompletionBlock = { (cursor:CKQueryCursor?, error: NSError?) -> Void in
if (error != nil) {
print("Failed to get data from iCloud - \(error!.localizedDescription)")
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}
publicDatabase.addOperation(queryOperation
}
func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int {
return self.categories.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("dining") as! DiningTableCell
let restaurant: CKRecord = categories[indexPath.row]
cell.RestaurantName?.text = restaurant.valueForKey("Name") as? String
let img = restaurant.objectForKey("Picture") as! CKAsset
cell.RestaurantPhoto.image = UIImage(contentsOfFile: img.fileURL.path!)
return cell
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue1" {
if let destViewController = segue.destinationViewController as? RestaurantTable {
let indexPath = self.tableView.indexPathForSelectedRow!
destViewController.indexpath1 = indexPath
}
}
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
}
Your entire class definition is nested inside your
override func viewDidLoad() {
function. Put a closing brace in after the call to super.viewDidLoad()
Are you sure that's your code or could you have accidentally removed some lines?
Because, as it stands, your table view functions are actually embedded in your viewDidLoad function... They need to be object level functions.
Note the indentation level of your code when you have Xcode indent the code for you (Right-click on the screen and select Structure->Re-indent.
import UIKit
import CloudKit
class DiningTable: UITableViewController {
var categories: Array<CKRecord> = []
override func viewDidLoad() {
super.viewDidLoad()
} // <--- you are missing this close brace here.
func getRecords()
{
categories = []
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "DiningTypes", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["Name", "Address", "Picture"]
queryOperation.qualityOfService = .UserInteractive
queryOperation.recordFetchedBlock = { (record:CKRecord) -> Void in
let categoryRecord = record
self.categories.append(categoryRecord)
}
queryOperation.queryCompletionBlock = { (cursor:CKQueryCursor?, error: NSError?) -> Void in
if (error != nil) {
print("Failed to get data from iCloud - \(error!.localizedDescription)")
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}
publicDatabase.addOperation(queryOperation)
}
override func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int {
return self.categories.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("dining") as! DiningTableCell
let restaurant: CKRecord = categories[indexPath.row]
cell.RestaurantName?.text = restaurant.valueForKey("Name") as? String
let img = restaurant.objectForKey("Picture") as! CKAsset
cell.RestaurantPhoto.image = UIImage(contentsOfFile: img.fileURL.path!)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue1" {
if let destViewController = segue.destinationViewController as? RestaurantTable {
let indexPath = self.tableView.indexPathForSelectedRow!
destViewController.indexpath1 = indexPath
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

CloudKit - Fetching 5 records with one field type... takes 3-4 seconds. why is it so slow?

I have a recordtype - "DiningTypes". "DiningTypes only has one field type, which is a string. I have 5 records... and it takes 3-4 second to load a table. How is that this is so slow? Do I need to begin the fetching process in a previous controller to have quicker UI response times?
import UIKit
import CloudKit
class table1: UITableViewController {
var categories: Array<CKRecord> = []
var fetchedcategories: Array<CKRecord> = []
override func viewDidLoad() {
super.viewDidLoad()
func fetchdiningtypes()
{
let container = CKContainer.defaultContainer()
let publicDatabase = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "DiningTypes", predicate: predicate)
publicDatabase.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
if (error != nil)
{
print("Error" + (error?.localizedDescription)!)
}
else
{
for result in results!
{
self.categories.append(result)
}
}
}
}
fetchdiningtypes()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return categories.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("dining") as! table1cell
let restaurant: CKRecord = categories[indexPath.row]
cell.Name.text = restaurant.valueForKey("Name") as? String
return cell
}
}
The default quality of service for CloudKit operations is NSQualityOfServiceUtility. If your application isn't in the foreground when the request runs you may see operations take longer than usual to run.
Try using CKQueryOperation and setting its qualityOfService to NSOperationQualityOfServiceUserInitiated.

Tutorial in retrieving, mutating and saving array from Parse.com in Swift with UITableView

import UIKit
class FeedTableViewController: UITableViewController {
var navBar:UINavigationBar=UINavigationBar()
let font = UIFont(name: "Baskerville", size: 15)
var feedData:NSMutableArray = NSMutableArray()
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
#IBAction func likeButton(sender: AnyObject) {
if var votes:Int? = quote!.objectForKey("votes") as? Int {
votes!++
}
}
#IBAction func loadData(sender: AnyObject?) {
feedData.removeAllObjects()
var findFeedData:PFQuery = PFQuery(className: "userQuotes")
findFeedData.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]?, error:NSError?)->Void in
if error == nil{
if let objs = objects{
for object in objs{
let quote:PFObject = object as! PFObject
self.feedData.addObject(quote)
// let user:PFUser = (object as! NSArray).lastObject as! PFUser
}
//println(self.feedData)
let array:NSArray = self.feedData.reverseObjectEnumerator().allObjects
self.feedData = NSMutableArray(array: array)
NSOperationQueue.mainQueue().addOperationWithBlock({
self.tableView.reloadData()
})
}
}
}
}
override func viewDidAppear(animated: Bool) {
self.loadData( nil )
}
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Quotezilla"
// 3
//self.navigationItem.setRightBarButtonItem(rightSearchBarButtonItem, animated: true)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return feedData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:QuoteTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! QuoteTableViewCell
let quote:PFObject = self.feedData.objectAtIndex(indexPath.row) as! PFObject
cell.contentTextView!.font = font
cell.timeStampLabel!.font = font
cell.publisherLabel!.font = font
cell.contentTextView.alpha = 0
cell.timeStampLabel.alpha = 0
cell.publisherLabel.alpha = 0
cell.contentTextView.text = quote.objectForKey("content") as! String
//cell.publisherLabel.text = quote.objectForKey("publisher") as? String
/* func loadLikes(){
if var votes:Int? = quote.objectForKey("votes") as? Int {
votes!++
}
}*/
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, h:mm a"
cell.timeStampLabel.text = dateFormatter.stringFromDate(quote.createdAt!)
var votes:Int? = quote["votes"] as? Int
if votes == nil {
votes = 0
}
cell.likesLabel?.text = "\(votes!)"
var myObject = quote["publisher"] as? PFObject
myObject?.fetchIfNeeded()
if let foundUser = myObject as? PFUser{
cell.publisherLabel.text = foundUser.username
UIView.animateWithDuration(0.7, animations: {
cell.contentTextView.alpha = 1
cell.timeStampLabel.alpha = 1
cell.publisherLabel.alpha = 1
})
}
return cell
}
So what I am essentially attempting to do is create a likes or votes button. As you see in the code I have a likeButton action that is supposed to auto-increment the likes section in parse. I display the current likes that I have filled into the rows in Parse itself in the cellForRowAtIndexPath function. The problem is that I cannot call quote.objectForKey("votes"), because I initialize it later. I have been poring over this problem and cannot find a way to make the votes update in parse through the likeButton action.
You must live with life on the network. That means your table won't have certain data available when the App starts. Handle a missing object or missing key within a particular cell gracefully and just use some kind of placeholder value. When the parse callback executes, you are already correctly forcing a refresh.
OK So BIG EDIT
This class needed a lot of work. I'm not even going to spell out every change here, but it's basically a complete Parse.com tutorial at this point.
This code compiles cleanly but I can't be sure of everything in your context. In particular do you have a 'likesButton' on every table row as part of your custom table cell view? I'm assuming that.
class FeedTableViewController: UITableViewController {
var navBar = UINavigationBar()
let font = UIFont(name: "Baskerville", size: 15)
var feedData = [PFObject]()
static let cellID = "cell"
// NOTE! See how this tag is set below
#IBAction func likeButton(sender: UIButton) {
let quote = feedData[sender.tag]
if let votes = quote.objectForKey("votes") as? Int {
quote.setObject(votes + 1, forKey: "votes")
}
else {
// CHALLENGE FOR YOU: handle the case of no votes attribute
}
// UPDATE the local UI
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: sender.tag, inSection: 0)],
withRowAnimation: .None)
// CHALLENGE FOR YOU: UPDATE Parse...start a new question if necessary
}
#IBAction func loadData(sender: AnyObject?) {
feedData.removeAll()
PFQuery(className: "userQuotes").findObjectsInBackgroundWithBlock {
[unowned self]
(objects: [AnyObject]?, error: NSError?) -> Void in
if let objs = objects {
for object in objs {
self.feedData.append(object as! PFObject)
}
self.feedData = self.feedData.reverse()
}
NSOperationQueue.mainQueue().addOperationWithBlock { self.tableView.reloadData() }
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadData(nil)
self.title = "Quotezilla"
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return feedData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(FeedTableViewController.cellID, forIndexPath: indexPath) as! QuoteTableViewCell
cell.likesButton!.tag = indexPath.row // See how tag works with the above
cell.contentTextView!.font = font
cell.timeStampLabel!.font = font
cell.publisherLabel!.font = font
cell.contentTextView.alpha = 0.0
cell.timeStampLabel.alpha = 0.0
cell.publisherLabel.alpha = 0.0
let q = feedData[indexPath.row]
if let content = q.objectForKey("content") as? String {
cell.contentTextView.text = content
}
else {
cell.contentTextView.text = "Content not found!"
}
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, h:mm a"
cell.timeStampLabel.text = dateFormatter.stringFromDate(q.createdAt!)
let votes = (q.objectForKey("votes") as? Int) ?? 0
cell.likesLabel?.text = "\(votes)"
let myObject = q.objectForKey("publisher") as? PFObject
myObject?.fetchInBackgroundWithBlock {
[unowned self]
(object: PFObject?, error: NSError?) in
NSOperationQueue.mainQueue().addOperationWithBlock {
if let foundUser = object as? PFUser {
cell.publisherLabel.text = foundUser.username
UIView.animateWithDuration(0.7) {
cell.contentTextView.alpha = 1.0
cell.timeStampLabel.alpha = 1.0
cell.publisherLabel.alpha = 1.0
}
}
else {
cell.publisherLabel.text = "Publisher not found!"
}
}
}
return cell
}
}

Parse & Xcode 6.4 problems unwrapping optionals with Swift 1.2

I'm having trouble unwrapping an optional after upgrading to xcode 6.4. Here's the error I'm getting when running this line of code:
query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
I know that with the new update for xcode you have to be more careful unwrapping optionals, but I'm not sure what changes I need to make to make swift cooperate. I've tried many different ways that were suggested on S/O but haven't gotten one to work yet. Here's my full code:
Thanks!!
import UIKit
import Parse
class TableViewController: UITableViewController {
var usernames = [""]
var userids = [""]
var isFollowing = ["":false]
override func viewDidLoad() {
super.viewDidLoad()
var query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let users = objects {
self.usernames.removeAll(keepCapacity: true)
self.userids.removeAll(keepCapacity: true)
self.isFollowing.removeAll(keepCapacity: true)
for object in users {
if let user = object as? PFUser {
if user.objectId! != PFUser.currentUser()?.objectId {
self.usernames.append(user.username!)
self.userids.append(user.objectId!)
var query = PFQuery(className: "followers")
***query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)***
query.whereKey("following", equalTo: user.objectId!)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
if objects.count > 0 {
self.isFollowing[user.objectId!] = true
} else {
self.isFollowing[user.objectId!] = false
}
}
if self.isFollowing.count == self.usernames.count {
self.tableView.reloadData()
}
})
}
}
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return usernames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = usernames[indexPath.row]
let followedObjectId = userids[indexPath.row]
if isFollowing[followedObjectId] == true {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
let followedObjectId = userids[indexPath.row]
if isFollowing[followedObjectId] == false {
isFollowing[followedObjectId] = true
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
var following = PFObject(className: "followers")
following["following"] = userids[indexPath.row]
following["follower"] = PFUser.currentUser()?.objectId
following.saveInBackground()
} else {
isFollowing[followedObjectId] = false
cell.accessoryType = UITableViewCellAccessoryType.None
var query = PFQuery(className: "followers")
query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
query.whereKey("following", equalTo: userids[indexPath.row])
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
object.deleteInBackground()
}
}
})
}
}
}
You can make use of Optional Binding to access the logged in user to make sure that it is not nil before using it as a query parameter.
So you should change the line:
query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
With:
if let userObj = PFUser.currentUser()?.objectId {
query.whereKey("follower", equalTo: userObj)
}
Now this will solve your bug and prevent your app from crashing, but maybe you should restructure your app in a way to confirm that the user has logged in no matter what when reaching this UITableViewController.
In any case you should still use Optional Binding to prevent a potential crash.