首页 > 解决方案 > 在应用程序进入后台时获取 LPLinkMetadata 的多个实例

问题描述

我必须在应用程序进入后台时执行此任务以保留用户体验,因此我必须使用此方法而不是新的 iOS 13 后台任务(因为它们可能在应用程序进入后一段时间内无法运行的背景)。获取 LPLinkMetadata 会实例化 WKWebView,因此 Apple 规定必须在主线程上调用它。我知道这段代码有各种各样的代码气味,我不知道如何修复它。是的,我仍然使用objective-c(它是一个旧应用程序)。

当应用程序进入后台时,我不确定是否可以在主线程上运行它。这甚至允许吗?

我的完成是错误的,因为在调用它时没有返回所有链接的元数据,但我不知道如何解决这个问题。线程和操作还不是我的强项。

我遇到了可怕的错误,包括以下所有错误。我将不胜感激任何帮助或指导。

编辑:这是我最新的代码,这些是我最新的错误。错误仍然几乎完全一样,如果不完全一样的话。

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    __block BOOL changedMetadata = NO;
    self.linkMetadataFetchers = [[NSMutableArray alloc] init];
    
    self.downloadLPMetadataBgTask = [UIApplication.sharedApplication beginBackgroundTaskWithName:@"DownloadLPMetadata" expirationHandler:^{

        for (LPMetadataProvider *provider in self.linkMetadataFetchers.reverseObjectEnumerator) {
            NSLog(@"provider:%@",provider);
            [provider cancel];
            [self.linkMetadataFetchers removeObject:provider];
        }
        self.linkMetadataFetchers = nil;
        
        if (changedMetadata) {
            [[NSNotificationCenter defaultCenter] postNotificationName:kLinkMetadataChanged object:nil];
        }
        
        [UIApplication.sharedApplication endBackgroundTask:self.downloadLPMetadataBgTask];
        self.downloadLPMetadataBgTask = UIBackgroundTaskInvalid;
    }];

    dispatch_group_t prepareWaitingGroup = dispatch_group_create();

    for (Link *link in _allLinks) {
        if ([Link metadataForURLString:link.linkURL] == nil) {
          
            dispatch_group_enter(prepareWaitingGroup);
            
            __block LPMetadataProvider *lpMetadataProvider = [[LPMetadataProvider alloc] init];
            
            [self.linkMetadataFetchers addObject:lpMetadataProvider];
           
            NSURL *theLinkURL = [NSURL URLWithString:link.linkURL];
            [lpMetadataProvider startFetchingMetadataForURL:theLinkURL completionHandler:^(LPLinkMetadata *fetchedLinkMetadata, NSError * error) {
               
                //called here because this returns on a background thread, even though called on the main thread
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [self.linkMetadataFetchers removeObject:lpMetadataProvider];
                
                    if (fetchedLinkMetadata != nil) {
                    
                        fetchedLinkMetadata.remoteVideoURL = nil;
                        fetchedLinkMetadata.videoProvider = nil;

                        [Link storeMetadata:fetchedLinkMetadata forURLString:link.linkURL];
                    
                        changedMetadata = YES;
                    }

                    dispatch_group_leave(prepareWaitingGroup);
                });
             }];
        }
    }

    dispatch_group_notify(prepareWaitingGroup, dispatch_get_main_queue(), ^{
        
        if (changedMetadata) {
            [[NSNotificationCenter defaultCenter] postNotificationName:kLinkMetadataChanged object:nil];
        }
        
        [UIApplication.sharedApplication endBackgroundTask:self.downloadLPMetadataBgTask];
        self.downloadLPMetadataBgTask = UIBackgroundTaskInvalid;
    });
}

这个错误最让我担心:

2020-06-27 20:08:42.020212-0400 MyApp 90433:1221213] [BackgroundTask] 调用过期处理程序后后台任务仍未结束:< UIBackgroundTaskInfo:0x600000f87fc0>:taskID = 14,taskName = com.apple.WebKit.ProcessAssertion ,创建时间 = 44122(经过 = 45)。此应用程序可能会被系统终止。调用 UIApplication.endBackgroundTask( :) 来避免这种情况。

所有错误:

985055-0400 MyApp [9189:1289719] [ProcessSuspension] 0x11a1a5240 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:53:47.337484-0400 MyApp 9189:1289719] [assertion] 获取断言时出错:<NSBBd: <NSError: <NSError 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:47.390441-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x6000008dc480; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:48.240831-0400 MyApp[9189:1289719] WF: _userSettingsForUser : (null) 2020-06-27 20:53:48.282299-0400 MyApp[ 9189:1289719] WF:_WebFilterIsActive 返回:否 2020-06-27 20:53:48.677632-0400 MyApp [9189:1289719] WF:_userSettingsForUser : (null) 2020-06-27 20:53:48。478790-0400 MyApp[9189:1289719] WF:_WebFilterIsActive 返回:NO 2020-06-27 20:53:56.727273-0400 MyApp[9189:1289719] [ProcessSuspension] 0x11a1a5000 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 :53:56.738099-0400 MyApp[9189:1289719] [ProcessSuspension] 0x11a10dd80 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:53:56.831405-0400 MyApp[9189:1289719] WF2: 020-UserSettings: (null) 06-27 20:53:56.838785-0400 MyApp[9189:1289719] WF: _WebFilterIsActive 返回:NO 2020-06-27 20:53:56.882649-0400 MyApp[9189:1289719] WF: _userSettingsForUser : (null) 2020-06 -27 20:53:56.883591-0400 MyApp [9189:1289719] WF: _WebFilterIsActive 返回:NO 2020-06-27 20:53:56.928754-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 05;6000009d5 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:56.934225-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x6000009d29d0; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:56.974920-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x6000008aa700; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:56.988064-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x60000083a340; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:57.339247-0400 MyApp[9189:1289719] WF: _userSettingsForUser : (null) 2020-06-27 20:53:57。379947-0400 MyApp[9189:1289719] WF: _WebFilterIsActive 返回:NO 2020-06-27 20:53:57.531548-0400 MyApp[9189:1289719] WF: _userSettingsForUser : (null) 2020-06-27 20:53:57.7226 -0400 MyApp [9189:1289719] WF:_WebFilterIsActive 返回:NO 2020-06-27 20:53:57.783396-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x6000009f0ab0; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:57.825150-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x6000009761f0; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:58.133422-0400 MyApp[9189:1289719] [ProcessSuspension] 0x11a1a5180 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:53:58 . 138869-0400 myApp [9189:1289719] [processSusPension] 0x11A3B95A0-ProcessAsserTion :: ProcessAssertionWasinValidated()2020-06-27 20:53:53:53:53:58.474841-0400 myApp [9189:1289:12897719]] -06-27 20:53:58.532229-0400 MyApp [9189:1289719] [ProcessSuspension] 0x11a10d2d0 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:53:58.588643-0400 MyApp 9189:128971 获取错误] [assertion]断言:<NSError:0x60000090a520;域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:58.628739-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x6000008d8e40; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:53:58.725068-0400 MyApp [9189:1289719] [ProcessSusPension] 0x1136DB720-ProcessAsserTion :: ProcessAssertionWasinValidated()2020-06-27 20:53:53:53:53:58.727406-0400 MyApp [9189:128971.128971] -06-27 20:54:00.498097-0400 MyApp[9189:1289719] [ProcessSuspension] 0x11a1a5690 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:54:00.501250-0400 MyApp[9189:12089171]a[1a56pensfion]0x171 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:54:00.503221-0400 MyApp[9189:1289719] [ProcessSuspension] 0x11a30ab10 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:54:00.579[MyApp] 9189:1289719] [ProcessSuspension] 后台任务在持有 WebKit ProcessAssertion (isMainThread?1) 时过期。2020-06-27 20:54:01。UIBackgroundTaskInfo:0x600001223780>:taskID = 13,taskName = com.apple.WebKit.ProcessAssertion,creationTime = 46844(经过 = 42)。此应用程序可能会被系统终止。调用 UIApplication.endBackgroundTask(:) 避免这种情况。2020-06-27 20:54:01.684869-0400 MyApp [9189:1289719] [ProcessSuspension] 0x11a30ab70 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:54:01.690090-040719 [9189:128] MyApp [9189:12] 0x11A1A5390-ProcessAsserTion :: ProcessAssertionWasInvalidated()2020-06-27 20:54:01.690290-0400 MyApp [9189:1289719] [processSuspension] [processSuspension] 0x11A30A30A360-CrocessAspertion:7220202020202020202020202020202020202020202020202020202020202020202020号2020202020202020202020202020202020202. [9189:1289719] [ProcessSuspension] 0x11a1a52d0 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:54:01.692379-0400 MyApp [9189:1289719] [ProcessSuspension] 0x11a30a5a0 - ProcessAssertion():2020-06-27 20:54:01.692379-0400 20:54:01.695908-0400 MyApp [9189:1289719] [ProcessSuspension] 0x113654f60 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:54:01。1289719] [ProcessSuspension] 0x11a1a5210 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:54:01.738068-0400 MyApp 9189:1289719] [assertion] 获取断言时出错:<NSError:0x6000008d9290; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:54:01.759941-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x6000008dbfc0; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:54:01.769570-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x6000008c14a0; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:54:01.783856-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x60000083ed00; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:54:01.784982-0400 MyApp 9189:1289719] [断言] 获取断言时出错:<NSError: 0x60000083e6d0; 域:RBSAssertionErrorDomain;代码:2;原因:“指定的目标进程不存在”> 2020-06-27 20:54:01.788174-0400 MyApp[9189:1289719] [ProcessSuspension] 0x11a10d1b0 - ProcessAssertion::processAssertionWasInvalidated() 2020-06-27 20:54:01.832853 -0400 myApp [9189:1289719] [processSusPension] 0x11A10D270- crocessAssertion :: ProcessAsserTionWasinValidated()2020-06-27 20:54:54:02.139156-0400 06-27 20:54:02。

标签: iosbackground-processios13background-task

解决方案


推荐阅读