Search code examples
arraysswiftdictionaryparse-platform

Load dictionary with objectID and data from Parse in Swift


I'm pulling data from Parse into an array but need a way to reference that specific object directly for making changes to the data on the server (ex: deleting a specific entry off the app and having it delete on the server). Previously I was using an array filled with a PFQuery, which worked for pulling data down but not for making changes back up. I think creating a dictionary with [objectID : string of data needed] would work, so each data set currently in the array would always be paired directly to it's identifier. My issue is pulling both sets of data down (objectID and string of data) and matching them up in the dictionary. Any advice or help?

I changed the original array to a dictionary in the cellContent variable but otherwise the code is still set up for an array.

Thanks!

import UIKit
import Parse

var segueWorker = ""

class MyWorkersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var cellContent = [String: String]()


    @IBAction func backButton(_ sender: Any) {

        navigationController?.popViewController(animated: true)

        performSegue(withIdentifier: "workersToMyFarm", sender: self)

    }

    @IBOutlet weak var tableView: UITableView!


    internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return cellContent.count

    }




    internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")

        cell.textLabel?.text = cellContent[indexPath.row]

        return cell

    }

    override func viewDidAppear(_ animated: Bool) {


        // Do any additional setup after loading the view.

        self.cellContent.removeAll()

        let query = PFQuery(className: "Workers")

        query.findObjectsInBackground(block: { (objects1, error) in

            if error != nil {

                print(error!)

            } else {

                query.whereKey("username", equalTo: PFUser.current()?.username)

                query.findObjectsInBackground(block: { (objects2, error) in

                    for object in objects2! {

                        self.cellContent.append(object["workerName"] as! String)

                        self.cellContent.sort()

                        self.tableView.reloadData()

                        }

                    }

                )}

            }

        )}

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == UITableViewCellEditingStyle.delete {

            cellContent.remove(at: indexPath.row)

            tableView.reloadData()


        }




    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let indexPath = tableView.indexPathForSelectedRow

        let currentCell = tableView.cellForRow(at: indexPath!)!

        segueWorker = (currentCell.textLabel!.text!)

        performSegue(withIdentifier: "toAddWorkers", sender: self)

    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Solution

  • Took some tinkering but finally got it to work by spreading out the variables and finally building the dictionary once I had all variables created. Also, had a lot of trouble with getting the objectId from Parse but seems to be working by simply calling object.objectId.

    Code below for reference:

    self.cellContent.removeAll()
    
    let query = PFQuery(className: "Workers")
    
    query.findObjectsInBackground(block: { (objects1, error) in
    
        if error != nil {
    
            print(error!)
    
        } else {
    
                    query.whereKey("username", equalTo: PFUser.current()?.username)
    
                    query.findObjectsInBackground(block: { (objects2, error) in
    
                        for object in objects2! {
    
                            self.cellContent.append(object["workerName"] as! String)
    
    
                            // Needs to be objectId
    
                            let objectKey = object.objectId
    
                            let objectValue = object["workerName"] as! String
    
                            self.cellContentDict[objectKey!] = objectValue
    
                            print(self.cellContentDict)
    
                            self.cellContent.sort()
    
                    self.tableView.reloadData()