简体   繁体   中英

Call kit identification

First we are receiving 160K data from the server Then we are trying to insert this 160k data directly to CallKit but we are getting this error:

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

Then we tried 30K data to insert and this time it was successful.

Our problem is we can not insert more than 30K, for this reason, we can not use the remaining 120K.

How can we solve this issue?

This is my extension code:

@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

I would suggest that you break you load up into blocks with the @autoreleasepool to reduce your memory consumption; extensions have a lower memory quota than apps and will be terminated if they exceed it.

Something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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