Refresh data in the Watch app from an appgroup cached on iPhone

To pass data from my IOS app to the My Watch app, I cache the data in NSUserDefaults (in an app group) in my App App App App. (I call this method both in didFinishLaunching and willTerminate.)

Then I feed this data to the Watch application via the application group and everything works well.

The problem is that when I change the data on my iPhone app, I want to update the data in the Watch app too, but it doesn't update unless I uninstall and reinstall the Watch app.

I tried my pick from the app groups method in awakeWithContext and willActivate, but the data is still outdated. How can I update the data on the Watch so that it does not delete / reinstall?

* edit: In other words, the Apple Watch app seems to keep running on the watch ... while I would prefer it to be killed, so that when the user changes data in the iPhone app then clicks on the watch app it opens up with new data instead of just waking up with outdated data.

class InterfaceController: WKInterfaceController {

    var nameList: [String]?
    var currentUserName: String?

    var peopleDict: [String : [String : [String : String]]]?


    @IBOutlet weak var personTable: WKInterfaceTable!

    private func loadTableData() {

        if let list = nameList {

            personTable.setNumberOfRows(list.count, withRowType: "PersonTableRowController")

            for (index, personName) in enumerate(list) {
                if let row = personTable.rowControllerAtIndex(index) as? PersonTableRowController {
                    row.personLabel.setText(personName)

                } else { println("Could not cast as PersonTableRowController") }
            }
        }

    }


    func fetchFromAppGroup() {

        let defaults = NSUserDefaults(suiteName: "group.com.myndarc.thredz2")
        defaults?.synchronize()
        if let fetchedPeopleDict: AnyObject = defaults?.valueForKey("peopleDict") {


            //save peopleDict locally
            peopleDict = fetchedPeopleDict as? [String : [String : [String : String]]]


            if let dict = fetchedPeopleDict as? NSDictionary {
                nameList = dict.allKeys as? [String]
            }

        }
            if let user: AnyObject = defaults?.valueForKey("currentUser") {

                currentUserName = user as? String



        }
    }


    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)



    }

    override func willActivate() {

        super.willActivate()
        fetchFromAppGroup()

        loadTableData()


    }

      

+3


source to share





All Articles