CallKit标识

问题描述 投票:0回答:1

我们正在从服务器接收160K的数据,我们正尝试将其直接插入CallKit,但出现此错误:

com.apple.CallKit.error.calldirectorymanager Code=2
CXErrorCodeCallDirectoryManagerErrorLoadingInterrupted

我们已经尝试插入30K数据,但是这次成功了。

我们的问题是我们不能插入超过30K,因此,我们不能使用剩余的120K。

我们如何解决这个问题?

这是我的扩展代码:

@interface CallDirectoryHandler () <CXCallDirectoryExtensionContextDelegate>
@end

@implementation CallDirectoryHandler

- (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context {
    context.delegate = self;

    if (![self addIdentificationPhoneNumbersToContext:context]) {
        NSError *error = [NSError errorWithDomain:@"CallDirectoryHandler" code:2 userInfo:nil];
        [context cancelRequestWithError:error];
        return;
    }

    [context completeRequestWithCompletionHandler:nil];
 }

- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context {

    NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:SharedUserDefaultsSuiteName];
    containerURL = [containerURL URLByAppendingPathComponent:callDirectoryPathComponent];

    NSURL *containerURLForCallerIDFounded = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:SharedUserDefaultsSuiteName];
    containerURLForCallerIDFounded = [containerURLForCallerIDFounded URLByAppendingPathComponent:callDirectorySolvedCallerIDPathComponent];

    NSMutableDictionary *dictFromFile1 = [NSMutableDictionary dictionaryWithContentsOfFile:containerURL.path];
    NSMutableDictionary *dictFromFile2 = [NSMutableDictionary dictionaryWithContentsOfFile:containerURLForCallerIDFounded.path];
    if (dictFromFile2.count > 0) {
        [dictFromFile1 addEntriesFromDictionary:dictFromFile2];
    }
    if (dictFromFile1.count == 0) {
        return YES;
    }

    NSArray *sortedArray = [[dictFromFile1 allKeys] sortedArrayUsingComparator: ^NSComparisonResult(
                                                                                       NSString *string1,
                                                                                       NSString *string2) {
        return [string1 compare: string2 options: NSNumericSearch];
    }];

    for (int i=0; i<sortedArray.count; i++) {
        @autoreleasepool {
            NSString *number = [sortedArray objectAtIndex:i];
            NSString *name = dictFromFile1[number];
            if (number && [number isKindOfClass:[NSString class]] &&
                name && [name isKindOfClass:[NSString class]]) {
                CXCallDirectoryPhoneNumber phoneNumber = [number longLongValue];
                [context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNumber label:name];
            }
            number = nil;
            name = nil;
        }
    }
    return YES;
}

#pragma mark - CXCallDirectoryExtensionContextDelegate

- (void)requestFailedForExtensionContext:(CXCallDirectoryExtensionContext *)extensionContext withError:(NSError *)error {
}

@end
ios callkit
1个回答
0
投票

我建议您使用@autoreleasepool将负载分成多个块,以减少内存消耗;扩展名的内存配额少于应用程序,如果超过该配额,则将被终止。

类似:

int offset = 0

int blockSize = 100

while offset < sortedArray.count {
    @autoreleasepool {
        for (int i=offset; i < min(sortedArray.count, offset+blockSize); i++) {
            NSString *number = [sortedArray objectAtIndex:i];
            NSString *name = dictFromFile1[number];
            if (number && [number isKindOfClass:[NSString class]] &&
            name && [name isKindOfClass:[NSString class]]) {
                CXCallDirectoryPhoneNumber phoneNumber = [number longLongValue];
                [context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNumber label:name];
            }
            number = nil;
            name = nil;
        }
        offset += blockSize
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.