基本的iCloud Key-Value存储测试失败

时间:2016-06-05 00:21:40

标签: ios swift icloud

我想测试iCloud键值存储。以下是我采取的步骤:

1)购买苹果开发者账户约100美元,等待激活

2)在开发者区域,我创建了一个App ID,一个iCloud容器,一个配置文件(iOS Development),并确保它知道我的个人设备。

3)在XCode中创建了一个新的单视图swift应用程序

4)在应用程序中添加了以下代码:didFinishLaunchingWithOptions:method:

    let keyStore = NSUbiquitousKeyValueStore.defaultStore()

    #if (arch(i386) || arch(x86_64)) && os(iOS)
        //let DEVICE_IS_SIMULATOR = true
        keyStore.setString("testValue2", forKey: "testKey2")
        let didSync = keyStore.synchronize()
        print("synched: \(didSync)")
    #else
        //let DEVICE_IS_SIMULATOR = false
        let didSync = keyStore.synchronize()
        print("synched: \(didSync)")
        if let theString = keyStore.stringForKey("testKey2") {
            print("the string: \(theString)")
        }
        else {
            print("did not find string with specified key")
        }
    #endif

5)在5s模拟器上启动应用程序,确认keyStore.synchronize()返回true。

6)等了10秒

7)在iPhone 6+上启动应用程序,确认keyStore.synchronize()返回true。

8)遗憾的是,它打印出“未找到带指定键的字符串”

我做错了什么?

1 个答案:

答案 0 :(得分:1)

在这种情况下,您不应该致电synchronize。来自documentation

  

在内存和磁盘同步期间,此方法使用先前从iCloud接收的更改来更新内存中的键和值集合

由于您正在写入内存然后立即调用synchronize,因此内存中的值将被缓存的值覆盖,在全新的应用程序中,这些值为空。系统尚未有机会使用您刚写入的值更新缓存。

您应该在synchronize中包含applicationWillEnterForeground的来电:

  

调用此方法的唯一建议时间是在应用启动时,或在返回到前台时,以确保内存中键值存储表示是最新的。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let keyStore = NSUbiquitousKeyValueStore.defaultStore()
    keyStore.synchronize()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.iCloudChangedNotification(_:)), name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification, object: nil)

    keyStore.setString("testValue2", forKey: "testKey2")
    if let theString = keyStore.stringForKey("testKey2") {
        print("the string: \(theString)")
    }
    else {
        print("did not find string with specified key")
    }  
    return true
}

func iCloudChangedNotification(notification: NSNotification) {
    print("iCloud changed")
    if let userInfo = notification.userInfo {
        if let changeReason = userInfo[NSUbiquitousKeyValueStoreChangeReasonKey] as? NSNumber {
            print("Change reason = \(changeReason)")
        }
        if let changedKeys = userInfo[NSUbiquitousKeyValueStoreChangedKeysKey] as? [String] {
            print("ChangedKeys = \(changedKeys)")
        }          
    }

    let keyStore = NSUbiquitousKeyValueStore.defaultStore()

    if let theString = keyStore.stringForKey("testKey2") {
        print("the string: \(theString)")
    }
    else {
        print("did not find string with specified key")
    }
}

func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    let keyStore = NSUbiquitousKeyValueStore.defaultStore()
    keyStore.synchronize()            
}