質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

128閲覧

Swift製のEurekaを使ってFirebaseのuserNameを表示すると動きがカタつく

12e80e801281281

総合スコア3

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2024/02/19 09:27

実現したいこと

Swift製のEurekaを使ってFirebaseのuserNameを表示したい
・Eureka
https://github.com/xmartlabs/Eureka

発生している問題・分からないこと

動きがカクついてしまいます。
また正しい実装方法なのかもわからない状態です。

該当のソースコード

Swift

1import UIKit 2import Eureka 3import MessageUI 4 5class UserPageEditViewController: FormViewController, MFMailComposeViewControllerDelegate { 6 7 var userName: String = "" 8 9 // インスタンス 10 let viewModel = UserPageEditViewModel() 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 // ラベルに表示するためにここでデータを取得する 16 viewModel.fetchUserName() 17 // 監視 18 NotificationCenter.default.addObserver( 19 self, 20 selector: #selector(userNameFetched), 21 name: UserPageEditViewModel.userNameFetchedNotification, 22 object: nil 23 ) 24 25 // ----------- ナビゲーションバー ----------- // 26 // タイトル 27 self.navigationItem.title = "マイページ" 28 29 // ----------- Eureka ----------- // 30 form 31 // --------- プロフィール画像 --------- // 32 +++ Section("プロフィール画像") 33 <<< TextRow(){ row in 34 row.title = "プロフィール画像" 35 row.placeholder = "Enter text here" 36 } 37 // --------- 名前 --------- // 38 +++ Section("名前") 39 <<< TextRow(){ row in 40 row.title = "名前" 41 row.placeholder = self.viewModel.userName 42 } 43 44 // --------- 基本情報 --------- // 45 +++ Section("基本情報") 46 <<< LabelRow(){ row in 47 row.title = "アプリver" 48 row.value = "1.0" 49 } 50 51 // --------- 使い方 --------- // 52 <<< LabelRow(){ row in 53 row.title = "使い方" 54 row.value = "見る" 55 } 56 57 // --------- 問い合わせする --------- // 58 <<< LabelRow(){ row in 59 row.title = "問い合わせ" 60 row.value = "する" 61 }.onCellSelection { [unowned self] cell, row in 62 // メールを起動し、問い合わせする 63 self.sendEmail() 64 } 65 66 // --------- ログアウトする --------- // 67 <<< LabelRow(){ row in 68 row.title = "ログイン" 69 row.value = "ログアウトする" 70 }.onCellSelection { [unowned self] cell, row in 71 // ログアウトする 72 viewModel.firebaseLogOut() 73 } 74 } 75 76 77 // ------------ 問い合わせメール関連 ------------ // 78 func sendEmail() { 79 guard MFMailComposeViewController.canSendMail() else { 80 print("メールアカウントが設定されていません。") 81 return 82 } 83 84 let mailComposer = MFMailComposeViewController() 85 mailComposer.mailComposeDelegate = self 86 mailComposer.setToRecipients([""]) // 宛先のメールアドレス 87 mailComposer.setSubject("問い合わせ") 88 mailComposer.setMessageBody(getEmailBody(), isHTML: false) 89 90 present(mailComposer, animated: true, completion: nil) 91 } 92 93 func getEmailBody() -> String { 94 let deviceUUID = UIDevice.current.identifierForVendor?.uuidString ?? "Unknown UUID" 95 let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "Unknown Version" 96 97 let body = """ 98 ご連絡いただきありがとうございます。 99 100 以下、お問い合わせ内容です。 101 102 --- 103 104 デバイスのUUID: \(deviceUUID) 105 アプリのバージョン: \(appVersion) 106 107 お問い合わせ内容: ご入力いただいたお問い合わせ内容がここに入ります。 108 109 --- 110 111 よろしくお願いいたします。 112 """ 113 114 return body 115 } 116 117 // MARK: - MFMailComposeViewControllerDelegate 118 func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 119 if let error = error { 120 print("エラー: \(error.localizedDescription)") 121 } 122 controller.dismiss(animated: true, completion: nil) 123 124 // キャンセルボタンが押された場合、ContactViewControllerを閉じる 125 if result == .cancelled { 126 self.dismiss(animated: true, completion: nil) 127 } 128 } 129 130 // 通知受信時の処理 131 @objc func userNameFetched() { 132 // ----------- Eureka ----------- // 133 form 134 // --------- プロフィール画像 --------- // 135 +++ Section("プロフィール画像") 136 <<< TextRow(){ row in 137 row.title = "プロフィール画像" 138 row.placeholder = "Enter text here" 139 } 140 // --------- 名前 --------- // 141 +++ Section("名前") 142 <<< TextRow(){ row in 143 row.title = "名前" 144 row.placeholder = self.viewModel.userName 145 } 146 147 // --------- 基本情報 --------- // 148 +++ Section("基本情報") 149 <<< LabelRow(){ row in 150 row.title = "アプリver" 151 row.value = "1.0" 152 } 153 154 // --------- 使い方 --------- // 155 <<< LabelRow(){ row in 156 row.title = "使い方" 157 row.value = "見る" 158 } 159 160 // --------- 問い合わせする --------- // 161 <<< LabelRow(){ row in 162 row.title = "問い合わせ" 163 row.value = "する" 164 }.onCellSelection { [unowned self] cell, row in 165 // メールを起動し、問い合わせする 166 self.sendEmail() 167 } 168 169 // --------- ログアウトする --------- // 170 <<< LabelRow(){ row in 171 row.title = "ログイン" 172 row.value = "ログアウトする" 173 }.onCellSelection { [unowned self] cell, row in 174 // ログアウトする 175 viewModel.firebaseLogOut() 176 } 177 } 178} 179 180

Swift

1import PKHUD 2import Firebase 3import Foundation 4 5 6class UserPageEditViewModel { 7 8 // 変数 9 var userInfos: [UserInfo] = [] 10 // ユーザーネーム 11 var userName: String = "" 12 // 通知用のキー 13 static let userNameFetchedNotification = Notification.Name("userNameFetched") 14 15 // ------ Firebaseからユーザーデータを取得する ------ // 16 func fetchUserData() { 17 // Firestoreのデータベース参照を作成 18 let db = Firestore.firestore() 19 20 // 最近ログインしているユーザーのUIDを取得 21 guard let uid = Auth.auth().currentUser?.uid else { 22 // ユーザーがログインしていない場合の処理 23 return 24 } 25 26 // Firestoreのコレクション参照を作成 27 let collectionRef = db.collection("user") 28 29 collectionRef.document(uid).getDocument { (document, error) in 30 if let document = document, document.exists { 31 // ドキュメントが存在する場合、データを取得して表示する 32 let data = document.data() 33 34 let userInfo = UserInfo( 35 uuid: data?["uuid"] as? String ?? "", 36 userName: data?["userName"] as? String ?? "", 37 firebaseId: data?["firebaseId"] as? String ?? "", 38 paymentStatus: data?["paymentStatus"] != nil, 39 createTimeStamp: data?["createTimeStamp"] as? String ?? "", 40 updateTimeStamp: data?["updateTimeStamp"] as? String ?? "" 41 ) 42 // 追加 43 self.userInfos.append(userInfo) 44 45 } else { 46 // ドキュメントが存在しない場合の処理 47 print("Document does not exist") 48 } 49 } 50 } 51 52 // ------ ユーザーネームを取得する ------ // 53 func fetchUserName() { 54 // Firestoreのデータベース参照を作成 55 let db = Firestore.firestore() 56 // 最近ログインしているユーザーのUIDを取得 57 guard let uid = Auth.auth().currentUser?.uid else { 58 // ユーザーがログインしていない場合の処理 59 return 60 } 61 // Firestoreのコレクション参照を作成 62 let collectionRef = db.collection("user") 63 // コレクションを取得する 64 collectionRef.document(uid).getDocument { (document, error) in 65 if let document = document, document.exists { 66 let data = document.data() 67 68 guard let userName = data!["userName"] as? String else { 69 print("①UserName does not exist") 70 return 71 } 72 73 // 変数の保存 74 self.userName = userName 75 // 通知を送信 76 NotificationCenter.default.post(name: UserPageEditViewModel.userNameFetchedNotification, object: nil) 77 78 } else { 79 print("②UserName does not exist") 80 } 81 } 82 } 83 84 // ------ Firebaseからログアウトする ------ // 85 func firebaseLogOut() { 86 do { 87 try Auth.auth().signOut() 88 HUD.flash(.label("ログアウトしました。"), delay: 2.0) 89 } catch let signOutError as NSError { 90 print("ログアウトエラー: \(signOutError.localizedDescription)") 91 HUD.flash(.label("失敗。"), delay: 2.0) 92 } 93 } 94} 95

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

・表示することは出来ました。

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。

Swift

1// ViewModel.swift 2 3import UIKit 4import Eureka 5import MessageUI 6 7class UserPageEditViewController: FormViewController, MFMailComposeViewControllerDelegate { 8 9 var userName: String = "" 10 11 // インスタンス 12 let viewModel = UserPageEditViewModel() 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 // ラベルに表示するためにここでデータを取得する 18 viewModel.fetchUserName() 19 20 // ----------- ナビゲーションバー ----------- // 21 // タイトル 22 self.navigationItem.title = "マイページ" 23 24 self.setForm() 25 } 26 27 28 // ------------ 問い合わせメール関連 ------------ // 29 func sendEmail() { 30 guard MFMailComposeViewController.canSendMail() else { 31 print("メールアカウントが設定されていません。") 32 return 33 } 34 35 let mailComposer = MFMailComposeViewController() 36 mailComposer.mailComposeDelegate = self 37 mailComposer.setToRecipients([""]) // 宛先のメールアドレス 38 mailComposer.setSubject("問い合わせ") 39 mailComposer.setMessageBody(getEmailBody(), isHTML: false) 40 41 present(mailComposer, animated: true, completion: nil) 42 } 43 44 func getEmailBody() -> String { 45 let deviceUUID = UIDevice.current.identifierForVendor?.uuidString ?? "Unknown UUID" 46 let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "Unknown Version" 47 48 let body = """ 49 ご連絡いただきありがとうございます。 50 51 以下、お問い合わせ内容です。 52 53 --- 54 55 デバイスのUUID: \(deviceUUID) 56 アプリのバージョン: \(appVersion) 57 58 お問い合わせ内容: ご入力いただいたお問い合わせ内容がここに入ります。 59 60 --- 61 62 よろしくお願いいたします。 63 """ 64 65 return body 66 } 67 68 // MARK: - MFMailComposeViewControllerDelegate 69 func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 70 if let error = error { 71 print("エラー: \(error.localizedDescription)") 72 } 73 controller.dismiss(animated: true, completion: nil) 74 75 // キャンセルボタンが押された場合、ContactViewControllerを閉じる 76 if result == .cancelled { 77 self.dismiss(animated: true, completion: nil) 78 } 79 } 80 81 private func setForm() { 82 // ----------- Eureka ----------- // 83 form 84 // --------- プロフィール画像 --------- // 85 +++ Section("プロフィール画像") 86 <<< TextRow(){ row in 87 row.title = "プロフィール画像" 88 row.placeholder = "Enter text here" 89 } 90 // --------- 名前 --------- // 91 +++ Section("名前") 92 <<< TextRow(){ row in 93 row.title = "名前" 94 row.placeholder = self.viewModel.userName 95 } 96 97 // --------- 基本情報 --------- // 98 +++ Section("基本情報") 99 <<< LabelRow(){ row in 100 row.title = "アプリver" 101 row.value = "1.0" 102 } 103 104 // --------- 使い方 --------- // 105 <<< LabelRow(){ row in 106 row.title = "使い方" 107 row.value = "見る" 108 } 109 110 // --------- 問い合わせする --------- // 111 <<< LabelRow(){ row in 112 row.title = "問い合わせ" 113 row.value = "する" 114 }.onCellSelection { [unowned self] cell, row in 115 // メールを起動し、問い合わせする 116 self.sendEmail() 117 } 118 119 // --------- ログアウトする --------- // 120 <<< LabelRow(){ row in 121 row.title = "ログイン" 122 row.value = "ログアウトする" 123 }.onCellSelection { [unowned self] cell, row in 124 // ログアウトする 125 viewModel.firebaseLogOut() 126 } 127 } 128}

Swift

1//ViewModl.swift 2import PKHUD 3import Firebase 4import Foundation 5 6 7class UserPageEditViewModel { 8 9 // 変数 10 var userInfos: [UserInfo] = [] 11 // ユーザーネーム 12 var userName: String = "" 13 // 通知用のキー 14 static let userNameFetchedNotification = Notification.Name("userNameFetched") 15 16 // ------ Firebaseからユーザーデータを取得する ------ // 17 func fetchUserData() { 18 // Firestoreのデータベース参照を作成 19 let db = Firestore.firestore() 20 21 // 最近ログインしているユーザーのUIDを取得 22 guard let uid = Auth.auth().currentUser?.uid else { 23 // ユーザーがログインしていない場合の処理 24 return 25 } 26 27 // Firestoreのコレクション参照を作成 28 let collectionRef = db.collection("user") 29 30 collectionRef.document(uid).getDocument { (document, error) in 31 if let document = document, document.exists { 32 // ドキュメントが存在する場合、データを取得して表示する 33 let data = document.data() 34 35 let userInfo = UserInfo( 36 uuid: data?["uuid"] as? String ?? "", 37 userName: data?["userName"] as? String ?? "", 38 firebaseId: data?["firebaseId"] as? String ?? "", 39 paymentStatus: data?["paymentStatus"] != nil, 40 createTimeStamp: data?["createTimeStamp"] as? String ?? "", 41 updateTimeStamp: data?["updateTimeStamp"] as? String ?? "" 42 ) 43 // 追加 44 self.userInfos.append(userInfo) 45 46 } else { 47 // ドキュメントが存在しない場合の処理 48 print("Document does not exist") 49 } 50 } 51 } 52 53 // ------ ユーザーネームを取得する ------ // 54 func fetchUserName() { 55 // Firestoreのデータベース参照を作成 56 let db = Firestore.firestore() 57 // 最近ログインしているユーザーのUIDを取得 58 guard let uid = Auth.auth().currentUser?.uid else { 59 // ユーザーがログインしていない場合の処理 60 return 61 } 62 // Firestoreのコレクション参照を作成 63 let collectionRef = db.collection("user") 64 // コレクションを取得する 65 collectionRef.document(uid).getDocument { (document, error) in 66 if let document = document, document.exists { 67 let data = document.data() 68 69 guard let userName = data!["userName"] as? String else { 70 print("①UserName does not exist") 71 return 72 } 73 74 // 変数の保存 75 self.userName = userName 76 // 通知を送信 77 NotificationCenter.default.post(name: UserPageEditViewModel.userNameFetchedNotification, object: nil) 78 79 } else { 80 print("②UserName does not exist") 81 } 82 } 83 } 84 85 // ------ Firebaseからログアウトする ------ // 86 func firebaseLogOut() { 87 do { 88 try Auth.auth().signOut() 89 HUD.flash(.label("ログアウトしました。"), delay: 2.0) 90 } catch let signOutError as NSError { 91 print("ログアウトエラー: \(signOutError.localizedDescription)") 92 HUD.flash(.label("失敗。"), delay: 2.0) 93 } 94 } 95} 96 97}

投稿2024/02/22 10:38

12e80e801281281

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問