0

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 ?

kiuzio2
  • 11
  • 6
  • Yes, the reply of the second message is overwriting the first table. What are you trying to achieve? Displaying medicine and amount in one table? – joern Oct 16 '15 at 15:50

1 Answers1

0

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]
    }
}
joern
  • 27,354
  • 7
  • 90
  • 105
  • Thanks for your help, but i still got a problem. My property array will look like these: "`medicines = [(String, String?)]()`". And from the messages i will get two arrays: "`medicinesNames = ["Zyrtec", "medicine1", "medicine2"]`" and "`amountName = ["2 times a day" , "in the morning", "after lunch"]`" The question is who to append the property with those arrays ? – kiuzio2 Oct 17 '15 at 14:56
  • Please see my updated answer for a suggested way to do this. – joern Oct 17 '15 at 15:50
  • When i try to use `fun reloadTable()` in line: `if let row = self.table.rowControllerAtIndex(index) as? tableRowController` Shows an error: `Cannot convert value of type '(UnsafePointer ,Int32) -> UnsafeMutablePointer' to expected argument type 'Int'` – kiuzio2 Oct 17 '15 at 16:35
  • Oh, yes. I forgot to add set and increment the index. I updated the answer. – joern Oct 17 '15 at 16:40
  • I don't know what the issue is, but it still doesn't show the rows. I put all of the code (sendingMessage, reloadTable etc) in willActivate. Maybe there is a problem in iPhone side with parsing info from Parse SDK ? When i try to print the elements of medicine Array it only shows `[]` in the logs. – kiuzio2 Oct 17 '15 at 17:02
  • Are you calling reloadTable after added the data from the messages? See updated answer. – joern Oct 17 '15 at 17:21
  • It works! Thank you so much for your patience and time spent on this code. It will improve my app a lot. – kiuzio2 Oct 17 '15 at 17:46
  • No problem, glad it helped you! – joern Oct 17 '15 at 17:47