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

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

问题描述

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

I'm passing a dictionary from my iPhone to the watch using the (watchOS 2) applicationContext method.

在 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抓取的很好,但是每次app进入后台,数据自动丢失,奇怪,在 iPhone 应用中,字典有一些持久性(至少在被暂停之前).

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.

当你的字典是空白的,你可以依靠 receivedApplicationContext 属性来访问最近收到的表数据.

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)

使用此方法访问最近收到的更新字典.会话对象还向其委托的 session:didReceiveApplicationContext: 方法发送一个新到达的字典.

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.

你也可以在 NSUserDefaults 中持久化字典来处理你的应用程序在暂停时被终止的情况.

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

您没有展示在获得数据后如何调用 loadTable().一旦您(接收到新数据或)检索到持久数据,您肯定想这样做.

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天全站免登陆