当我执行以下代码时,我的 iOS 应用程序崩溃:
- (UIImage *)getImageAndSaveInDBWithKey:(NSString *)imageKey withKeyPath:(NSString *)keyPath inBucket:(NSString *)bucketName scanhistoryRecord:(ScanHistory *) scanHistoryRecord
{
[self setImageExceptionTitle:@""];
[self setImageExceptionName:@""];
[self setImageExceptionReason:@""];
[self setImageDownloadErrorCode:@""];
[self setImageDownloadErrorDomain:@""];
[self setImageDownloadErrorDescription:@""];
NSString *downloadingFilePath = [NSTemporaryDirectory()stringByAppendingPathComponent:imageKey];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];
__block UIImage *downloadedImage = nil;
AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
downloadRequest.bucket = bucketName;
downloadRequest.key = [NSString stringWithFormat:@"%@%@", keyPath, imageKey];
downloadRequest.downloadingFileURL = downloadingFileURL;
@try {
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[[[transferManager download:downloadRequest] continueWithBlock:^id(AWSTask *task) {
if (task.error)
在崩溃日志中我可以看到这一行抛出了异常:
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
这是崩溃日志:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: SIGNAL 6 Abort trap: 6
Terminating Process: iTracker [32153]
Triggered by Thread: 6
Last Exception Backtrace:
0 CoreFoundation 0x1ba17ccb4 __exceptionPreprocess + 164 (NSException.m:202)
1 libobjc.A.dylib 0x1b32183d0 objc_exception_throw + 60 (objc-exception.mm:356)
2 AWSS3 0x1036e9b38 __48+[AWSS3TransferManager defaultS3TransferManager]_block_invoke + 440
3 libdispatch.dylib 0x1c1643eac _dispatch_client_callout + 20 (object.m:560)
4 libdispatch.dylib 0x1c16456ec _dispatch_once_callout + 32 (once.c:52)
5 AWSS3 0x1036e9978 +[AWSS3TransferManager defaultS3TransferManager] + 56
6 iTracker 0x102a0a79c -[AWSManager getImageAndSaveInDBWithKey:withKeyPath:inBucket:scanhistoryRecord:] + 284 (AWSManager.m:98)
7 iTracker 0x102a50554 closure #1 in ResultView.showSuccess() + 316 (ResultView.swift:587)
8 iTracker 0x102a7a63c thunk for @escaping @callee_guaranteed () -> () + 28 (<compiler-generated>:0)
9 libdispatch.dylib 0x1c1642320 _dispatch_call_block_and_release + 32 (init.c:1518)
10 libdispatch.dylib 0x1c1643eac _dispatch_client_callout + 20 (object.m:560)
11 libdispatch.dylib 0x1c1655a64 _dispatch_root_queue_drain + 684 (queue.c:7051)
12 libdispatch.dylib 0x1c1656158 _dispatch_worker_thread2 + 164 (queue.c:7119)
13 libsystem_pthread.dylib 0x219fadda0 _pthread_wqthread + 228 (pthread.c:2631)
14 libsystem_pthread.dylib 0x219fadb7c start_wqthread + 8
我已将此代码包装在 try catch 块中。但 catch 块被忽略,应用程序崩溃了。我可以做什么来解决这个问题?
我发现 [AWSServiceManager defaultServiceManager].defaultServiceConfiguration 为 nil,并添加了一个 if 语句来处理 nil 时的情况。
AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
downloadRequest.bucket = bucketName;
downloadRequest.key = [NSString stringWithFormat:@"%@%@", keyPath, imageKey];
downloadRequest.downloadingFileURL = downloadingFileURL;
if([AWSServiceManager defaultServiceManager].defaultServiceConfiguration == nil) {
NSString *accessPointId = [[NSUserDefaults standardUserDefaults] stringForKey:@"ap_id_1"];
NSString *vendorId = [[NSUserDefaults standardUserDefaults] stringForKey:@"vendor_id"];
NSString *deviceId = [[NSUserDefaults standardUserDefaults] stringForKey:@"device_id"];
NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"];
NSString *message = [NSString stringWithFormat:@"AWS not connected. Image not downloaded. accesspoint: %@ vendor: %@ device: %@ user: %@", accessPointId, vendorId, deviceId, username];
[self setImageDownloadErrorDescription: message];
NSLog(@"%@",message);
return downloadedImage;
}
这解决了我的问题!