简体   繁体   English

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

[英]Sync app data with iCloud on request

I want to store some of my application data in iCloud, and I want it to be saved when the application is going to the background or is shut down. 我想将一些应用程序数据存储在iCloud中,并且希望在应用程序进入后台或关闭时将其保存。

I have a MyDocument class, which is written in accordance with this Apple tutorial . 我有一个MyDocument类,它是按照此Apple教程编写的。 In fact, I do this: 实际上,我这样做:

// 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];
}

My app goes to the background and is synchronized only when I turn it on again. 我的应用程序进入后台,仅在再次打开时才同步。 But I want to synchronize my data with iCloud manually. 但是我想手动将数据与iCloud同步。 How can I do this? 我怎样才能做到这一点?

Before exiting, UIDocument checks if the document hasUnsavedChanges . 在退出之前, UIDocument检查文档是否具有hasUnsavedChanges If YES , saving is invoked. 如果YES ,则调用保存。
You are setting the document text in the applicationDidEnterBackground delegate method, which is probably too late. 您正在applicationDidEnterBackground委托方法中设置文档文本,这可能为时已晚。

From the method docs: 从方法文档:

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. 因为在applicationDidEnterBackground:中启动的任何后台任务很可能要等到该方法退出后才会运行,因此在启动这些任务之前,您应该请求额外的后台执行时间。 In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread. 换句话说,首先调用beginBackgroundTaskWithExpirationHandler :,然后在调度队列或辅助线程上运行任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM