繁体   English   中英

呼叫套件识别

[英]Call kit identification

首先我们从服务器接收 160K 数据然后我们尝试将这 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

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

就像是:

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM