首先,我们从服务器接收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
答案 0 :(得分: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
}
}