我想将我的一些应用程序数据存储在 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/