WatchKit应用程序在后台运行后丢失数据-Swift [英] WatchKit app losing data after going background - Swift

查看:76
本文介绍了WatchKit应用程序在后台运行后丢失数据-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用(watchOS 2)applicationContext方法将字典从iPhone传递到手表.

在iPhone应用程序内部:

func giveMeInfo(){
    var lastStringUsed = porkee288.storyPoints.last!
    do {
        let resultDict = ["dict": myDict]
        try WCSession.defaultSession().updateApplicationContext(resultDict)
    }  
    catch {
        print("Something wrong happened")
    }
}

在手表应用中:

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {

    dispatch_async(dispatch_get_main_queue()) { () -> Void in

        if let retrievedDict = applicationContext["dict"] as? [String : String] {

                self.dictInsideWatch = retrievedDict     
        }
    }
}

数据很好地由watchKit中的tableview提取,但是,每次应用程序进入后台时,数据都会自动丢失,这很奇怪,因为在iPhone应用程序中词典具有一定的持久性(至少直到被暂停).

您建议采取什么措施来解决此问题并防止数据消失?

解决方案

您正在描述的问题是,一旦您返回手表应用程序,该表就不会显示任何数据.尽管您没有显示该特定代码,但这可能是由于下次打开该应用程序时该词典为空白.

由于仅收到一次应用程序上下文,因此,您可能用于重新加载表的任何属性观察器或方法仅在新到达数据时触发,而在恢复应用程序时不会./p>

当字典为空白时,您可以使用

Inside the watch app:

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {

    dispatch_async(dispatch_get_main_queue()) { () -> Void in

        if let retrievedDict = applicationContext["dict"] as? [String : String] {

                self.dictInsideWatch = retrievedDict     
        }
    }
}

The data is fetched by the tableview in the watchKit nicely, however, each time the app goes to the background, the data is automatically lost, which is strange because in iPhone apps dictionaries have some persistence (at least until becoming suspended).

What would you recommend to counteract this issue and keep the data from disappearing?

解决方案

The problem you're describing is that the table doesn't show any data once you return to the watch app. Although you didn't show that specific code, it's likely due to the dictionary being blank the next time the app is opened.

Since the application context is only received once, any property observer or method you might be using to reload the table would only fire when the data is newly arrived, not when the app is resumed.

When your dictionary is blank, you can fall back on the receivedApplicationContext property to access the most recent received data for your table.

A dictionary containing the last update data received from a paired and active device. (read-only)

Use this method to access the most recently received update dictionary. The session object also sends a newly arrived dictionary to the session:didReceiveApplicationContext: method of its delegate.

You can also persist the dictionary in NSUserDefaults to handle the case when your app has been terminated while suspended.

You didn't show how you called loadTable() once you had the data. You definitely want to do that, once you've (received new data or) retrieved persisted data.

if !session.receivedApplicationContext.keys.isEmpty {
    // Use most recently received dictionary
    dictInsideWatch = receivedApplicationContext["dict"]
} else {
    // Use persisted dictionary
    dictInsideWatch = NSUserDefaults.standardUserDefaults().dictionaryForKey("dict") ?? [:]
}
loadTable()

If you adopt this approach, make sure to persist the data (either immediately after it's received, or at the point when the app is about to move to an inactive state).

这篇关于WatchKit应用程序在后台运行后丢失数据-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆