【问题标题】:iOS Background processiOS后台进程
【发布时间】:2014-01-16 11:52:33
【问题描述】:

我需要在我的应用程序运行时与服务器进行同步。 有时,这种同步可能需要几分钟。 在 iPhone 上,如果用户按下主页按钮或设备自动锁定,则此任务会中断。

我试过类似的东西:

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;

但是这段代码不起作用,有人知道我该如何克服这个挑战吗?

同步的方式是请求数据直到不再存在。但是使用此代码,它最终会出现在进入后台时正在接收的背景对象中

【问题讨论】:

    标签: ios iphone multithreading background


    【解决方案1】:

    您的代码在到期处理程序中调用[self synchronization];。仅当您的应用程序没有时间在后台运行时才会调用该块。因此,您仅在没有时间进行同步时才尝试开始同步。

    在街区外致电[self synchronization];。该块应包含清理/暂停代码并结束后台任务...

    另外,请考虑使用 NSURLSession 以获得良好的 iOS 7 兼容性。

    【讨论】:

    • 现在只下载当前包。我需要下载大约 8 个并存储在核心数据中,这需要 5 到 10 分钟。有什么建议吗?
    • 您的意思是您需要下载 8 个响应,而不仅仅是 1 个。您可以同时运行大约 4 个下载(使用不同的线程)。您的主要问题是您无法确定您将获得多少后台时间(url 会话对此有所帮助)。
    • 我看到你正在使用 RestKit,它还不支持 url 会话。您可以查看设置对象管理器操作队列的并发操作计数并同时创建所有需要的下载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多