前提・実現したいこと
IBActionでまずはFirebaseのStorageに画像と動画の値を保存し、保存が完了したあとにそれらのurlをCloudFirestoreに保存したい。
発生している問題・エラーメッセージ
Firebaseに値を保存する前にCloudFirestoreにurlへの保存を実行してしまいnilになってしまう。
2020-09-21 14:08:00.772274+0900 MotionCard[4166:184597] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed ================================================================= Main Thread Checker: UI API called on a background thread: -[UIImageView image] PID: 4166, TID: 184605, Thread name: (none), Queue name: queue, QoS: 0 Backtrace: 4 MotionCard 0x0000000101829381 $s10MotionCard22RegisterViewControllerC12uploadImages33_DADD4B9CF75A5DBBDE77CBF083E5AB1BLLyyF + 1697 5 MotionCard 0x0000000101827af7 $s10MotionCard22RegisterViewControllerC12uploadButtonyySo8UIButtonCFyycfU_ + 295 6 MotionCard 0x0000000101827c90 $sIeg_IeyB_TR + 48 7 libdispatch.dylib 0x00000001054ce8ac _dispatch_call_block_and_release + 12 8 libdispatch.dylib 0x00000001054cfa88 _dispatch_client_callout + 8 9 libdispatch.dylib 0x00000001054d6500 _dispatch_lane_serial_drain + 1222 10 libdispatch.dylib 0x00000001054d7027 _dispatch_lane_invoke + 439 11 libdispatch.dylib 0x00000001054e2e90 _dispatch_workloop_worker_thread + 882 12 libsystem_pthread.dylib 0x00007fff5dcd8a3d _pthread_wqthread + 290 13 libsystem_pthread.dylib 0x00007fff5dcd7b77 start_wqthread + 15 2020-09-21 14:08:16.117488+0900 MotionCard[4166:184605] [reports] Main Thread Checker: UI API called on a background thread: -[UIImageView image] PID: 4166, TID: 184605, Thread name: (none), Queue name: queue, QoS: 0 Backtrace: 4 MotionCard 0x0000000101829381 $s10MotionCard22RegisterViewControllerC12uploadImages33_DADD4B9CF75A5DBBDE77CBF083E5AB1BLLyyF + 1697 5 MotionCard 0x0000000101827af7 $s10MotionCard22RegisterViewControllerC12uploadButtonyySo8UIButtonCFyycfU_ + 295 6 MotionCard 0x0000000101827c90 $sIeg_IeyB_TR + 48 7 libdispatch.dylib 0x00000001054ce8ac _dispatch_call_block_and_release + 12 8 libdispatch.dylib 0x00000001054cfa88 _dispatch_client_callout + 8 9 libdispatch.dylib 0x00000001054d6500 _dispatch_lane_serial_drain + 1222 10 libdispatch.dylib 0x00000001054d7027 _dispatch_lane_invoke + 439 11 libdispatch.dylib 0x00000001054e2e90 _dispatch_workloop_worker_thread + 882 12 libsystem_pthread.dylib 0x00007fff5dcd8a3d _pthread_wqthread + 290 13 libsystem_pthread.dylib 0x00007fff5dcd7b77 start_wqthread + 15 success 2020-09-21 14:08:20.302050+0900 MotionCard[4166:185113] BackgroundSession <8E1858DB-84E5-42AF-9CF6-63115607005A> connection to background transfer daemon invalidated url: Optional("https://firebasestorage.googleapis.com/v0/b/motioncard-7e8c6.appspot.com/o/movies%2F1600664897534.mov?alt=media&token=d285db32-742e-4146-9db1-3ab3da6ed954") url: Optional("https://firebasestorage.googleapis.com/v0/b/motioncard-7e8c6.appspot.com/o/images%2F1600664895977.jpg?alt=media&token=823acfda-4bd7-4489-8dc1-0a916ee4b317")
該当のソースコード
swift
1@IBAction func uploadButton(_ sender: UIButton) { 2 3 let dispatchGroup = DispatchGroup() 4 // 直列キュー / attibutes指定なし 5 let dispatchQueue = DispatchQueue(label: "queue") 6 7 // 非同期処理を実行 8 dispatchGroup.enter() 9 dispatchQueue.async(group: dispatchGroup) { 10 [weak self] in 11 12 self!.uploadImages() 13 self!.uploadMovies() 14 dispatchGroup.leave() 15 } 16 17 // 全ての非同期処理完了後にメインスレッドで処理 18 dispatchGroup.notify(queue: .main) { [self] in 19 if nameTextField.text != nil,mailTextField.text != nil, imagePath != nil, moviePath != nil { 20 db.collection(CardData.collectionname).addDocument(data: [ 21 CardData.cardName : nameTextField.text!, 22 CardData.cardMail : mailTextField.text!, 23 CardData.cardImage : imagePath, 24 CardData.cardMovie : moviePath 25 ]) { (error) in 26 if let e = error { 27 print("error") 28 }else { 29 print("success") 30 } 31 } 32 } 33 } 34} 35 36fileprivate func uploadImages() { 37 let date = NSDate() 38 let currentTimeStampInSecond = UInt64(floor(date.timeIntervalSince1970 * 1000)) 39 let storageRef = Storage.storage().reference().child("images").child("(currentTimeStampInSecond).jpg") 40 let metaData = StorageMetadata() 41 metaData.contentType = "image/jpg" 42 if let uploadData = self.pickImage.image?.jpegData(compressionQuality: 0.9) { 43 storageRef.putData(uploadData, metadata: metaData) { (metadata , error) in 44 if error != nil { 45 print("error: (error?.localizedDescription)") 46 } 47 storageRef.downloadURL(completion: { (url, error) in 48 if error != nil { 49 print("error: (error?.localizedDescription)") 50 } 51 print("url: (url?.absoluteString)") 52 self.imagePath = "url: (String(describing: url?.absoluteString))" 53 }) 54 } 55 } 56} 57 58fileprivate func uploadMovies() { 59 let date = NSDate() 60 let currentTimeStampInSecond = UInt64(floor(date.timeIntervalSince1970 * 1000)) 61 let storageRef = Storage.storage().reference().child("movies").child("(currentTimeStampInSecond).mov") 62 let metaData = StorageMetadata() 63 metaData.contentType = "video/quicktime" 64 if let videoData = movie { 65 storageRef.putFile(from: videoData as URL, metadata: metaData) { (metadata , error) in 66 if error != nil { 67 print("error: (error?.localizedDescription)") 68 } 69 storageRef.downloadURL(completion: { (url, error) in 70 if error != nil { 71 print("error: (error?.localizedDescription)") 72 } 73 print("url: (url?.absoluteString)") 74 self.moviePath = "url: (String(describing: url?.absoluteString))" 75 }) 76 } 77 } 78}
試したこと
DispatchQueueを使用して直列処理を試してみましたがうまくいきませんでした。
補足情報
SWIFT初心者でいたらないこともあると思いますが何卒よろしくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/23 13:28