iOS后台进程
我需要在应用程序运行时与服务器进行同步。 有时,此同步可能需要几分钟。 在iPhone上,如果用户按下home(主页)按钮或设备自动锁定,此任务将中断 我试过这样的方法:iOS后台进程,ios,iphone,multithreading,background,Ios,Iphone,Multithreading,Background,我需要在应用程序运行时与服务器进行同步。 有时,此同步可能需要几分钟。 在iPhone上,如果用户按下home(主页)按钮或设备自动锁定,此任务将中断 我试过这样的方法: backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:b
backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
backgroundTaskID = UIBackgroundTaskInvalid;
}];
[self Synchronization];
funcao sincronização:
-(void)Synchronization{
object=nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[TheAppDelegate setSync:YES];
send = NO;
responseObj = [[ConteinerInicial alloc] init];
object = [self fillObjectContainer:1];
error = NO;
[[objectManager HTTPClient] setDefaultHeader:@"Token" value:TheAppDelegate.token.token];
[[objectManager HTTPClient] setDefaultHeader:@"Username" value:TheAppDelegate.token.userName];
[[objectManager HTTPClient] setDefaultHeader:@"IDDevice" value:TheAppDelegate.token.IDDevice];
[objectManager postObject:object path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result){
NSArray *response = [result array];
NSLog(@"Loading mapping result: %@", result);
NSDictionary *headerDictionary = [operation.HTTPRequestOperation.response allHeaderFields];
NSString *authenticationStatus = [headerDictionary objectForKey:@"AuthenticationStatus"];
// if server returns "NotAuthorized", user must login again
if ([authenticationStatus isEqualToString:@"NotAuthorized"]) {
[AppDelegate showErrorAlert:@"Login expired!"];
[TheAppDelegate clearLoginToken];
error = YES;
return;
}
responseObj = [response objectAtIndex:0];
if(responseObj.Package.count>0)
{
[self savePackage:responseObj tipo:1];
[self Synchronization];
}else
{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
backgroundTaskID = UIBackgroundTaskInvalid;
}
} failure: ^(RKObjectRequestOperation *operation, NSError *reportError){
RKLogError(@"Operation failed with error: %@", reportError);
[AppDelegate showErrorAlert:@"There is some problem with the connection or the server! Please, check your connection or the address of the server and try again."];
send = YES;
[TheAppDelegate setSync:NO];
error = YES;
}];
});
}
在同步结束时,已准备好:
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
backgroundTaskID = UIBackgroundTaskInvalid;
但是这个代码不起作用,有人知道我如何克服这个挑战吗
同步的方法是请求数据,直到不再存在。但使用此代码,它只会在进入后台时接收的background对象中结束您的代码正在调用
[self synchronization]代码>在过期处理程序中。只有当应用程序在后台运行的时间不足时,才会调用该块。因此,只有在没有时间进行同步时,您才尝试开始同步
调用[自同步]代码>块外部。该块应包含清理/暂停代码并结束后台任务
另外,请查看使用NSURLSession
以获得良好的iOS 7兼容性。现在只需下载当前软件包即可。我需要下载的文件大约有8个,存储在核心数据中,需要5到10分钟。有什么建议吗?你的意思是你需要下载8个回复,而不仅仅是1个。您可以同时运行大约4次下载(使用不同的线程)。您的主要问题是,您无法确定您将被授予多少后台时间(url会话有助于这一点)。我看到您正在使用RestKit,它还不支持url会话。您可以查看设置对象管理器操作队列的并发操作计数,并同时创建所有必需的下载。