Search code examples
iosswiftparse-platformwatchkitwatchos-2

Using parse for preparing rows in WatchOS


In my app I'm using Parse SDK to get list of medicines and amount from the database and pass it through the iPhone to the watch. I implemented on the watch two separate sendMessages in WillActivate() :

let iNeedMedicine = ["Value": "Query"]

    session.sendMessage(iNeedMedicine, replyHandler: { (content:[String : AnyObject]) -> Void in

        if let medicines = content["medicines"]  as? [String] {
            print(medicines)
            self.table.setNumberOfRows(medicines.count, withRowType: "tableRowController")
            for (index, medicine) in medicines.enumerate() {


                let row = self.table.rowControllerAtIndex(index) as? tableRowController
                if let row1 = row {

                        row1.medicineLabel.setText(medicine)
                }
            }
        }
        }, errorHandler: {  (error ) -> Void in
            print("We got an error from our watch device : " + error.domain)
    })

Second:

    let iNeedAmount = ["Value" : "Amount"]
    session.sendMessage(iNeedAmount, replyHandler: { (content:[String : AnyObject]) -> Void in

        if let quantity = content["quantity"]  as? [String] {
            print(quantity)
            self.table.setNumberOfRows(quantity.count, withRowType: "tableRowController")
            for (index, quant) in quantity.enumerate() {


                let row = self.table.rowControllerAtIndex(index) as? tableRowController
                row!.amountLabel.setText(quant)
            }
        }
        }, errorHandler: {  (error ) -> Void in
            print("We got an error from our watch device : " + error.domain)
    })

What i get is this: Problem. Is it because of two different messages ?


Solution

  • To display the medicine and the amount in the same table you could do the following:

    1. Create a property let medicines = [(String, String?)]()
    2. When the medicines arrive populate that array with the medicines. So that after this medicines looks like this [("Medicine1", nil), ("Medicine2", nil),...]
    3. When the quantities arrive iterate over medicines and add the quantities to the array, so that it looks like this after that: [("Medicine1", "Quantity1"), ("Medicine2", "Quantity2"),...]
    4. Use the medicines array to populate your table. Create a method that reloads the table:

    Like this:

    func reloadTable() {
        self.table.setNumberOfRows(medicines.count, withRowType: "tableRowController")
        var rowIndex = 0
        for item in medicines {
            if let row = self.table.rowControllerAtIndex(rowIndex) as? tableRowController {
                row.medicineLabel.setText(item.0)
                if let quantity = item.1 {
                    row.quantityLabel.setText(quantity)
                }
                rowIndex++
            }
        }
    }
    
    1. Call reloadTable() whenever you receive a message with quantity or data.

    This is just a raw example to explain the idea. You have to be careful to keep the medicines and the quantities in sync. Especially when you load more data when the user scrolls down.

    To fill the data from the messages into your array you can define two functions:

    func addMedicines(medicineNames: [String]) {
        for name in medicineNames {
            medicines.append((name, nil))
        }
    }
    
    func addQuantities(quantities: [String]) {
        guard medicines.count == quantities.count else { return }
        for i in 0..<medicines.count {
            medicines[i].1 = quantities[i]
        }
    }