Objective c - 当应用程序进入后台时保存完成块
Objective c - Save completion block when app enter in background
在我的应用程序中,用户需要将一些信息保存到我的数据库中。
[myUser saveInBackground:^(BOOL success){
if(success){
// Do smthing
}
else{
// Alert the user
}
}];
现在它工作正常,当保存过程中发生错误时,我可以提醒用户仅当应用程序仍然打开并处于活动状态时。
假设对我的数据库的请求持续了大约 10 秒(例如网络不好)并且用户决定离开应用程序,我的完成块没有被触发。
当用户已经离开应用程序时,如何提醒用户保存失败?
谢谢
看看Background Modes。此示例取自提到的文档:
- (void)yourSavingMethod {
UIApplication *application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Unfortunately, timeout..
// Clean up any unfinished task.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Save your data.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
真正需要的时候可以拨打-beginBackgroundTaskWithExpirationHandler:
。但是不要忘记结束任务。浏览文档以获取更多详细信息。
在我的应用程序中,用户需要将一些信息保存到我的数据库中。
[myUser saveInBackground:^(BOOL success){
if(success){
// Do smthing
}
else{
// Alert the user
}
}];
现在它工作正常,当保存过程中发生错误时,我可以提醒用户仅当应用程序仍然打开并处于活动状态时。
假设对我的数据库的请求持续了大约 10 秒(例如网络不好)并且用户决定离开应用程序,我的完成块没有被触发。
当用户已经离开应用程序时,如何提醒用户保存失败?
谢谢
看看Background Modes。此示例取自提到的文档:
- (void)yourSavingMethod {
UIApplication *application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Unfortunately, timeout..
// Clean up any unfinished task.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Save your data.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
真正需要的时候可以拨打-beginBackgroundTaskWithExpirationHandler:
。但是不要忘记结束任务。浏览文档以获取更多详细信息。