ios - 根据要求将应用程序数据与 iCloud 同步

标签 ios objective-c icloud

我想将我的一些应用程序数据存储在 iCloud 中,并且我希望在应用程序进入后台或关闭时保存这些数据。

我有一个MyDocument类,是按照this Apple tutorial编写的.事实上,我这样做:

// this function called from applicationDidEnterBackground
void SaveICloudData( const char *rawData )
{
    MyDocument *doc = AppDelegate.getICloudDocument;     // MyDocument is subclass of UIDocument, like in tutorial

    NSString *str = [[NSString alloc] initWithUTF8String:rawData];
    [doc setDocumentText:str];
}

我的应用程序进入后台,只有在我再次打开它时才会同步。但我想手动将我的数据与 iCloud 同步。我该怎么做?

最佳答案

退出前,UIDocument 检查文档是否hasUnsavedChanges。如果 YES,将调用保存。
您正在 applicationDidEnterBackground 中设置文档文本委托(delegate)方法,这可能为时已晚。

来自方法文档:

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

关于ios - 根据要求将应用程序数据与 iCloud 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14318301/

相关文章:

ios - 访问 RestKit 请求体

ios - 从 NSMutableDictionary 获取所有值

objective-c - requestAVAssetForVideo 不显示完成 block

ios - 如何使用 IOS 8 XCode 6 beta 从 Popover 中的按钮关闭 UIPopover

ios - 如何修改文本字段数组中选定的 UITexFiled 值

ios - AFNetworking 更改基本 url

ios - 如何启动并运行带有 MagicalRecord(核心数据)的 iCloud?

ios - 核心数据 - 重新加载 TableVIew

iOS KeyChain secItemAdd 崩溃

iOS11 : Can't create folder again in iCloud drive once deleted manually