切换到另一个UIViewController时可能出现的错误?
for video in videoFacebook {
if let videoURL = URL(string: video) {
// create your document folder url
let documentsUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
// your destination file url
let destination = documentsUrl.appendingPathComponent(videoURL.lastPathComponent)
pathToTheVideoFile.append(String(describing: destination))
// check if it exists before downloading it
if FileManager().fileExists(atPath: destination.path) {
print("The file already exists at path")
} else {
// if the file doesn't exist
// just download the data from your url
URLSession.shared.downloadTask(with: videoURL, completionHandler: { (location, response, error) in
// after downloading your data you need to save it to your destination url
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("video"),
let location = location, error == nil
else { return }
do {
try FileManager.default.moveItem(at: location, to: destination)
indexVideo += 1
print("Video " + String(indexVideo))
if indexPhoto == countImageLink && indexVideo == countVideoLink {
callback(descriptionFacebookVideo, titleFacebookVideo, CDataArray, countVideoLink, countImageLink, pathToTheVideoFile, sourceLink!)
}
print("file saved")
} catch {
print(error)
}
}).resume()
}
}
}
此处的错误记录:
[ [UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/Keyboard/UIKeyboardTaskQueue.m:432
2017-08-08 18:08:01.777 Customer[23898:946824] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ea92b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010dd72141 objc_exception_throw + 48
2 CoreFoundation 0x000000010ea96cf2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010d90c536 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 UIKit 0x000000010f9f3f6f -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 165
5 UIKit 0x000000010f174515 -[UIKeyboardImpl setDelegate:force:] + 1372
6 UIKit 0x000000010f58a63d -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 992
7 UIKit 0x000000010f593c4b -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 499
8 UIKit 0x000000010f06a35d -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1145
9 UIKit 0x0000000123765d20 -[UIViewControllerAccessibility _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 260
10 UIKit 0x000000010f06bfae -[UIViewController _presentViewController:withAnimationController:completion:] + 4660
11 UIKit 0x000000010f06ee8a -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 532
12 UIKit 0x000000010f06e962 -[UIViewController presentViewController:animated:completion:] + 181
13 UIKit 0x0000000123765638 -[UIViewControllerAccessibility presentViewController:animated:completion:] + 197
14 UIKit 0x000000010f66eccc __74-[UIStoryboardPresentationSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 133
15 UIKit 0x000000010f67e444 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 447
16 UIKit 0x000000010f67e254 -[UIStoryboardSegueTemplate _perform:] + 82
17 UIKit 0x000000010f05be47 -[UIViewController performSegueWithIdentifier:sender:] + 99
18 UIKit 0x000000012376606a -[UIViewControllerAccessibility performSegueWithIdentifier:sender:] + 67
19 Customer 0x000000010d63afb4 _TFC8Customer26AddNewRecipeViewController18goToListOfRecepiesfT_T_ + 116
20 Customer 0x000000010d63f382 _TFFC8Customer26AddNewRecipeViewController13saveNewRecipeFT_T_U2_FTSSSSCSo14NSMutableArraySiSiGSaSS_SS_T_ + 2274
21 Customer 0x000000010d63f56c _TPA__TFFC8Customer26AddNewRecipeViewController13saveNewRecipeFT_T_U2_FTSSSSCSo14NSMutableArraySiSiGSaSS_SS_T_ + 300
22 Customer 0x000000010d62a6bd _TFFFFC8Customer26AddNewRecipeViewController17getFacebookVideosFT8callbackFTSSSSCSo14NSMutableArraySiSiGSaSS_SS_T__T_U_FTGSqV10Foundation4Data_GSqCSo11URLResponse_GSqPs5Error___T_U_FT_T_U_FTGSqVS2_3URL_GSqS4__GSqPS5____T_ + 2893
23 Customer 0x000000010d642183 _TPA__TFFFFC8Customer26AddNewRecipeViewController17getFacebookVideosFT8callbackFTSSSSCSo14NSMutableArraySiSiGSaSS_SS_T__T_U_FTGSqV10Foundation4Data_GSqCSo11URLResponse_GSqPs5Error___T_U_FT_T_U_FTGSqVS2_3URL_GSqS4__GSqPS5____T_ + 611
24 Customer 0x000000010d62aacb _TTRXFo_oGSqV10Foundation3URL_oGSqCSo11URLResponse_oGSqPs5Error____XFdCb_dGSqCSo5NSURL_dGSqS1__dGSqCSo7NSError___ + 203
25 CFNetwork 0x0000000111773e30 __85-[__NSURLSessionLocal downloadTaskForRequest:downloadFilePath:resumeData:completion:]_block_invoke + 19
26 CFNetwork 0x00000001117c509a __50-[__NSCFLocalDownloadTask _private_fileCompletion]_block_invoke.321 + 79
27 Foundation 0x000000010d87c3b7 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
28 Foundation 0x000000010d87c0bb -[NSBlockOperation main] + 101
29 Foundation 0x000000010d87a877 -[__NSOperationInternal _start:] + 627
30 Foundation 0x000000010d8765fc __NSOQSchedule_f + 198
31 libdispatch.dylib 0x000000011221305c _dispatch_client_callout + 8
32 libdispatch.dylib 0x00000001121f194f _dispatch_queue_serial_drain + 221
33 libdispatch.dylib 0x00000001121f2669 _dispatch_queue_invoke + 1084
34 libdispatch.dylib 0x00000001121f4ec4 _dispatch_root_queue_drain + 634
35 libdispatch.dylib 0x00000001121f4bef _dispatch_worker_thread3 + 123
36 libsystem_pthread.dylib 0x00000001125aa5a2 _pthread_wqthread + 1299
37 libsystem_pthread.dylib 0x00000001125aa07d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
过渡功能:
func goToListOfRecepies() {
performSegue(withIdentifier: "backToListOfRecepies", sender: self)
}
let recipe = Recipe(context: self.context)
viewIndicator.isHidden = false
indicator.isHidden = false
indicator.startAnimating()
getFacebookVideos(callback: { (descriptionFacebookVideo, titleFacebookVideo, CDataArray, countVideoLink, countImageLink, pathToTheVideoFile, sourceLink) in
recipe.pathToTheVideoFile = pathToTheVideoFile as NSObject
recipe.countImage = Int16(countImageLink - countVideoLink)
recipe.countVideo = Int16(countVideoLink)
recipe.sourceLink = sourceLink
recipe.nameRecipe = titleFacebookVideo
recipe.imageRecipe = NSKeyedArchiver.archivedData(withRootObject: CDataArray) as NSData
recipe.descriptionRecipe = descriptionFacebookVideo
print(pathToTheVideoFile)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.indicator.isHidden = true
self.viewIndicator.isHidden = true
self.indicator.stopAnimating()
self.goToListOfRecepies()
})
错误本身:
let recipe = Recipe(context: self.context)
viewIndicator.isHidden = false
indicator.isHidden = false
indicator.startAnimating()
getFacebookVideos(callback: { (descriptionFacebookVideo, titleFacebookVideo, CDataArray, countVideoLink, countImageLink, pathToTheVideoFile, sourceLink) in
recipe.pathToTheVideoFile = pathToTheVideoFile as NSObject
recipe.countImage = Int16(countImageLink - countVideoLink)
recipe.countVideo = Int16(countVideoLink)
recipe.sourceLink = sourceLink
recipe.nameRecipe = titleFacebookVideo
recipe.imageRecipe = NSKeyedArchiver.archivedData(withRootObject: CDataArray) as NSData
recipe.descriptionRecipe = descriptionFacebookVideo
print(pathToTheVideoFile)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.indicator.isHidden = true
self.viewIndicator.isHidden = true
self.indicator.stopAnimating()
self.goToListOfRecepies()
})
即:
self.goToListOfRecepies()
视频出现后,当你切换到另一个控制器时有一个错误,请告诉我如何修复它?尝试了所有选项,我找到了答案:DispatchQueue.main.async,从哪里获取它们?