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

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

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

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

1042閲覧

Swift・iOSアプリで、Firebaseにアクセスできない?

masamasamasa

総合スコア95

Firebase

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

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2018/11/30 06:46

編集2018/11/30 07:36

##やりたいこと
Apiを作って、コードを簡潔に書きたい
イメージ説明
イメージ説明
イメージ説明

##問題
書き直す前: Firebaseにアクセス成功
書き直した後: Firebaseにアクセスできない?から、indicatorがずっと回ったままの状態になる
書き直し後のconsole: エラーなし ↓

console

12018-11-30 14:53:34.637882+0900 aaabbbccc[34977:1806460] - <AppMeasurement>[I-ACS036002] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist 2objc[34977]: Class AXAccessQueue is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AccessibilityUtilities.framework/AccessibilityUtilities (0x13287f780) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/Accessibility.framework/Frameworks/AXHearingSupport.framework/AXHearingSupport (0x133b26888). One of the two will be used. Which one is undefined. 3objc[34977]: Class AXAccessQueueTimer is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AccessibilityUtilities.framework/AccessibilityUtilities (0x13287cd50) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/Accessibility.framework/Frameworks/AXHearingSupport.framework/AXHearingSupport (0x133b268d8). One of the two will be used. Which one is undefined. 42018-11-30 14:53:35.041256+0900 aaabbbccc[34977:1806372] [AXMediaCommon] Unable to look up screen scale 52018-11-30 14:53:35.041439+0900 aaabbbccc[34977:1806372] [AXMediaCommon] Unexpected physical screen orientation 62018-11-30 14:53:35.117602+0900 aaabbbccc[34977:1806372] [AXMediaCommon] Unable to look up screen scale 72018-11-30 14:53:35.126056+0900 aaabbbccc[34977:1806372] [AXMediaCommon] Unable to look up screen scale 82018-11-30 14:53:35.126208+0900 aaabbbccc[34977:1806372] [AXMediaCommon] Unexpected physical screen orientation 91----------

コンソールを見ると、書き直した後の

REF_POSTS.child("posts").observe(.childAdded, with: { (DataSnapshot) in

で、Firebaseにアクセスできていないことが原因ではないかと考えています。
書き直した前後で書き方を変えていないが、APIのファイルではうまくいきません。
しかし、解決策がわかりません。

###Runした際の画面表示
(indicatorが回ったまま止まっています。)
イメージ説明

###コード
書き直す前

controller

1func loadPost() { 2 activityIndicatorView.startAnimating() 3 Database.database().reference().child("posts").observe(.childAdded, with: { (DataSnapshot) in 4 if let dict = DataSnapshot.value as? [String: Any] { 5 let newPost = Post.transformPostPhoto(dict: dict, key: DataSnapshot.key) 6 self.fetchUser(uid: newPost.uid!, completed: { 7 self.posts.append(newPost) 8 self.activityIndicatorView.stopAnimating() 9 self.activityIndicatorView.isHidden = true 10 self.tableView.reloadData() 11 }) 12 } 13 }) 14 }

書き直した後

controller

1func loadPost() { 2 PostApi().observePosts { (Post) in 3 self.fetchUser(uid: Post.uid!, completed: { 4 self.posts.append(Post) 5 self.activityIndicatorView.stopAnimating() 6 self.activityIndicatorView.isHidden = true 7 self.tableView.reloadData() 8 }) 9 } 10 }

Api

1class PostApi { 2 var REF_POSTS = Database.database().reference().child("posts") 3 4 func observePosts(complition: @escaping (Post) -> Void) { 5 print("1----------") 6 REF_POSTS.child("posts").observe(.childAdded, with: { (DataSnapshot) in 7 print("2--------") 8 print(DataSnapshot.value) 9 if let dict = DataSnapshot.value as? [String: Any] { 10 let newPost = Post.transformPostPhoto(dict: dict, key: DataSnapshot.key) 11 complition(newPost) 12 } 13 }) 14 }

すみませんが、よろしくお願いします。

##開発環境
MacOS Mojave 10.14.1
Xcode 10.0
Swift 3.0

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

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

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

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

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

guest

回答1

0

ベストアンサー

Swift

1 var REF_POSTS = Database.database().reference().child("posts") 2 3 func observePosts(complition: @escaping (Post) -> Void) { 4 print("1----------") 5 REF_POSTS.child("posts").observe(.childAdded, with: { (DataSnapshot) in 6 7

child("posts").child("posts") になっていませんか?

投稿2018/11/30 07:42

daisuke7

総合スコア1563

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

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

masamasamasa

2018/11/30 07:46

解決しました!! ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問