Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reload table in WatchKit

We have a hierarchical watch app.

The root controller is a table of menu items. That list of items is controlled by a server. The data is retrieved and stored in core data. The menu is populated the first time going into the app.

But I want this table to stay current. My thought was to add code to willActivate to check if there was changes, and reload the table. In my reload logic I call the same function I called the first time, which sets the menuTable.setNumberOfRows and creates each row. Looking at what I'm putting in the logs, it is going through this logic with a different count of rows and new labels. But the app on the watch shows the table with the old data.

How can I get this table to reload with the new information?

like image 853
Jason Hocker Avatar asked May 16 '15 06:05

Jason Hocker


2 Answers

I've had this problem too and as rmp says, it still seems to be a bug in watchOS 1.0.1. The problem appears when you try to reload your tableView after run willActivate() and nothing will happen.

In my case, I reload the tableView after receive a reply from a delegate and then I reload all the content just if it's necessary. To achieve this, I remove all rows from a NSIndexSet and load again.

if isNecessary {
    self.table.removeRowsAtIndexes(NSIndexSet(indexesInRange: NSMakeRange(0, maxItems)))
    isNecessary = false
}

I've tried a lot of tricks but none has worked for me:

  • Force to reload rows by table.setNumberOfRows(0, withRowType: "data")
  • Setting parameters to empty text before assign new values

One thing you could do is to hide tableView before removing rows, and avoid the remove animation.

like image 138
j_gonfer Avatar answered Oct 05 '22 22:10

j_gonfer


It is a bug in WatchKit. Seems like Apple doesn't handle the repetitive interface object correctly.

The general principle here is: Only insert or remove necessary rows after a table is created. Do not reload the whole table like what we usually do in iOS. It just doesn't work (or trigger the bug).

So specifically, you have to:

  • Do this in willActivated method. This is correct.
  • If this is the first load, before the table is even created, do what you are now doing – load all table rows.
  • For all following times, don't reload the table, fetch the new data and check the desired number of rows.
  • Compare with the current number of rows in the table, insert to or remove from the bottom of the existing table.
  • Now simply re-assign the new data to all existing rows. Again, do not reload.

It should work if you follow the above steps.

like image 45
Richard Bao Avatar answered Oct 05 '22 22:10

Richard Bao