質問編集履歴

4

誤字修正

2019/12/20 09:20

投稿

ruuuu
ruuuu

スコア174

test CHANGED
File without changes
test CHANGED
@@ -396,6 +396,6 @@
396
396
 
397
397
 
398
398
 
399
- データの取得処理は```func fetchdata()```に記載していますが、イベントリスナというのはデータの更新があった場合は、どこかの関数内であっても(どこに記載されていようとも)そこの処理部分だけ(今回は```fetchDataRef.observe```)呼び出されそこの処理を行う、のでしょうか?
399
+ データの取得処理は```fetchChatData```に記載していますが、イベントリスナというのはデータの更新があった場合は、どこかの関数内であっても(どこに記載されていようとも)そこの処理部分だけ(今回は```fetchDataRef.observe```)呼び出されそこの処理を行う、のでしょうか?
400
400
 
401
401
  [Firebase公式ドキュメント](https://firebase.google.com/docs/database/ios/retrieve-data?hl=ja)には```データが変更されると、そのたびに再トリガーされます。```とあったのですが、関数の中にある関数(```fetchDataRef.observe```)だけ呼び出された場合、呼び出された後の処理はどうなるのかなど、少々疑問でした為、質問させて貰いました。

3

修正

2019/12/20 09:20

投稿

ruuuu
ruuuu

スコア174

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,7 @@
1
1
  **イベントリスナ**について、お聞きしたいことがあります。
2
2
 
3
+ ```
4
+
3
5
  import UIKit
4
6
 
5
7
  import ChameleonFramework

2

修正

2019/12/20 08:52

投稿

ruuuu
ruuuu

スコア174

test CHANGED
File without changes
test CHANGED
@@ -1,399 +1,397 @@
1
- **イベントリスナ**について、お聞きしたかった為、質問させて貰いました
1
+ **イベントリスナ**について、お聞きしたいことがあり
2
+
3
+ import UIKit
4
+
5
+ import ChameleonFramework
6
+
7
+ import Firebase
8
+
9
+
10
+
11
+ class ChatViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate {
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+ @IBOutlet weak var tableView: UITableView!
20
+
21
+ @IBOutlet weak var messageTextField: UITextField!
22
+
23
+
24
+
25
+ @IBOutlet weak var sendButton: UIButton!
26
+
27
+
28
+
29
+ //スクリーンサイズ
30
+
31
+ let screenSize = UIScreen.main.bounds.size
32
+
33
+
34
+
35
+ var chatArray = [Message]()
36
+
37
+
38
+
39
+ override func viewDidLoad() {
40
+
41
+ super.viewDidLoad()
42
+
43
+
44
+
45
+ tableView.delegate = self
46
+
47
+ tableView.dataSource = self
48
+
49
+ messageTextField.delegate = self
50
+
51
+
52
+
53
+ //カスタムセルを作成する前に、テーブルビューに伝える必要がある
54
+
55
+ tableView.register(UINib(nibName:"CustomCell",bundle:nil),forCellReuseIdentifier:"Cell")
56
+
57
+
58
+
59
+ //可変
60
+
61
+ tableView.rowHeight = UITableView.automaticDimension
62
+
63
+ tableView.estimatedRowHeight = 75
64
+
65
+
66
+
67
+ //キーボード
68
+
69
+ NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillShow(_ :)), name: UIResponder.keyboardWillShowNotification, object: nil)
70
+
71
+
72
+
73
+ NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
74
+
75
+
76
+
77
+ //Firebaseからデータをfetch(取得)
78
+
79
+ fetchChatData()
80
+
81
+ tableView.separatorStyle = .none
82
+
83
+
84
+
85
+ }
86
+
87
+
88
+
89
+ @objc func keyboardWillShow(_ notification:NSNotification){
90
+
91
+
92
+
93
+ let keyboardHeight = ((notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as Any) as AnyObject).cgRectValue.height
94
+
95
+
96
+
97
+ messageTextField.frame.origin.y = screenSize.height - keyboardHeight - messageTextField.frame.height
98
+
99
+
100
+
101
+
102
+
103
+ }
104
+
105
+
106
+
107
+
108
+
109
+ @objc func keyboardWillHide(_ notification:NSNotification){
110
+
111
+
112
+
113
+ messageTextField.frame.origin.y = screenSize.height - messageTextField.frame.height
114
+
115
+
116
+
117
+ guard let rect = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue,
118
+
119
+ let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else{return}
120
+
121
+ UIView.animate(withDuration: duration) {
122
+
123
+
124
+
125
+ //移動アニメーション? ↓view自体が上がってしまっている為、元の位置に戻す為の指定
126
+
127
+ let transform = CGAffineTransform(translationX: 0, y: 0)
128
+
129
+
130
+
131
+ self.view.transform = transform
132
+
133
+
134
+
135
+ }
136
+
137
+ }
138
+
139
+
140
+
141
+ //タッチされた場合
142
+
143
+ override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
144
+
145
+
146
+
147
+ messageTextField.resignFirstResponder()
148
+
149
+
150
+
151
+ }
152
+
153
+
154
+
155
+ //Returnキーが押された場合
156
+
157
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
158
+
159
+
160
+
161
+ textField.resignFirstResponder()
162
+
163
+ return true
164
+
165
+
166
+
167
+ }
168
+
169
+
170
+
171
+
172
+
173
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
174
+
175
+
176
+
177
+ //メッセージの数
178
+
179
+ return chatArray.count
180
+
181
+
182
+
183
+ }
184
+
185
+
186
+
187
+ func numberOfSections(in tableView: UITableView) -> Int {
188
+
189
+ return 1
190
+
191
+ }
192
+
193
+
194
+
195
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
196
+
197
+
198
+
199
+ //このメソッドを呼び出す前に、registerとして登録する必要がある
200
+
201
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
202
+
203
+
204
+
205
+ cell.messageLabel.text = chatArray[indexPath.row].message
206
+
207
+
208
+
209
+ cell.userNameLabel.text = chatArray[indexPath.row].sender
210
+
211
+ cell.iconImageView.image = UIImage(named:"dogAvatarImage")
212
+
213
+
214
+
215
+
216
+
217
+ if cell.userNameLabel.text == Auth.auth().currentUser?.email as! String{
218
+
219
+
220
+
221
+ cell.messageLabel.backgroundColor = UIColor.flatGreen()
222
+
223
+ cell.messageLabel.layer.cornerRadius = 20
224
+
225
+ cell.messageLabel.layer.masksToBounds = true
226
+
227
+
228
+
229
+ }else{
230
+
231
+
232
+
233
+ cell.messageLabel.backgroundColor = UIColor.flatBlue()
234
+
235
+ cell.messageLabel.layer.cornerRadius = 20
236
+
237
+ cell.messageLabel.layer.masksToBounds = true
238
+
239
+ }
240
+
241
+
242
+
243
+ return cell
244
+
245
+
246
+
247
+ }
248
+
249
+
250
+
251
+ func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
252
+
253
+
254
+
255
+ return 100
256
+
257
+ }
258
+
259
+
260
+
261
+ @IBAction func sendAction(_ sender: Any) {
262
+
263
+
264
+
265
+ messageTextField.endEditing(true)
266
+
267
+ messageTextField.isEnabled = false
268
+
269
+ //ボタン無効
270
+
271
+ sendButton.isEnabled = false
272
+
273
+
274
+
275
+ if messageTextField.text!.count > 15{
276
+
277
+
278
+
279
+ print("15文字以上です!!。")
280
+
281
+
282
+
283
+ return
284
+
285
+
286
+
287
+ }
288
+
289
+ let chatDB = Database.database().reference().child("chats")
290
+
291
+
292
+
293
+ //キーバリュー型で内容を送信(Dictionary型)
294
+
295
+
296
+
297
+ let messageInfo = ["sender":Auth.auth().currentUser?.email,"message":messageTextField.text!]
298
+
299
+
300
+
301
+ //chatDBに入れる
302
+
303
+ chatDB.childByAutoId().setValue(messageInfo) { (error, result) in
304
+
305
+
306
+
307
+ if error != nil{
308
+
309
+
310
+
311
+ print(error as Any)
312
+
313
+
314
+
315
+ }else{
316
+
317
+
318
+
319
+ print("送信完了")
320
+
321
+ // self.sendButton.isEnabled = true
322
+
323
+ // self.messageTextField.isEnabled = true
324
+
325
+ // self.messageTextField.text = ""
326
+
327
+
328
+
329
+ }
330
+
331
+
332
+
333
+ }
334
+
335
+
336
+
337
+ }
338
+
339
+
340
+
341
+ //データを引っ張ってくる
342
+
343
+ func fetchChatData(){
344
+
345
+
346
+
347
+ //どこからデータを引っ張ってくるのか
348
+
349
+ let fetchDataRef = Database.database().reference().child("chats")
350
+
351
+
352
+
353
+ //新しく更新があった時だけ取得
354
+
355
+ fetchDataRef.observe(.childAdded) { (snapShot) in
356
+
357
+ let snapShotData = snapShot.value as AnyObject
358
+
359
+ let text = snapShotData.value(forKey: "message")
360
+
361
+ let sender = snapShotData.value(forKey: "sender")
362
+
363
+
364
+
365
+ let message = Message()
366
+
367
+ message.message = text as! String
368
+
369
+ message.sender = sender as! String
370
+
371
+ self.chatArray.append(message)
372
+
373
+ self.tableView.reloadData()
374
+
375
+
376
+
377
+ }
378
+
379
+
380
+
381
+ }
382
+
383
+
384
+
385
+
386
+
387
+
388
+
389
+ }
390
+
391
+
2
392
 
3
393
  ```
4
394
 
5
- import UIKit
6
-
7
- import ChameleonFramework
8
-
9
- import Firebase
10
-
11
-
12
-
13
- class ChatViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate {
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
- @IBOutlet weak var tableView: UITableView!
22
-
23
- @IBOutlet weak var messageTextField: UITextField!
24
-
25
-
26
-
27
- @IBOutlet weak var sendButton: UIButton!
28
-
29
-
30
-
31
- //スクリーンサイズ
32
-
33
- let screenSize = UIScreen.main.bounds.size
34
-
35
-
36
-
37
- var chatArray = [Message]()
38
-
39
-
40
-
41
- override func viewDidLoad() {
42
-
43
- super.viewDidLoad()
44
-
45
-
46
-
47
- tableView.delegate = self
48
-
49
- tableView.dataSource = self
50
-
51
- messageTextField.delegate = self
52
-
53
-
54
-
55
- //カスタムセルを作成する前に、テーブルビューに伝える必要がある
56
-
57
- tableView.register(UINib(nibName:"CustomCell",bundle:nil),forCellReuseIdentifier:"Cell")
58
-
59
-
60
-
61
- //可変
62
-
63
- tableView.rowHeight = UITableView.automaticDimension
64
-
65
- tableView.estimatedRowHeight = 75
66
-
67
-
68
-
69
- //キーボード
70
-
71
- NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillShow(_ :)), name: UIResponder.keyboardWillShowNotification, object: nil)
72
-
73
-
74
-
75
- NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
76
-
77
-
78
-
79
- //Firebaseからデータをfetch(取得)
80
-
81
- fetchChatData()
82
-
83
- tableView.separatorStyle = .none
84
-
85
-
86
-
87
- }
88
-
89
-
90
-
91
- @objc func keyboardWillShow(_ notification:NSNotification){
92
-
93
-
94
-
95
- let keyboardHeight = ((notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as Any) as AnyObject).cgRectValue.height
96
-
97
-
98
-
99
- messageTextField.frame.origin.y = screenSize.height - keyboardHeight - messageTextField.frame.height
100
-
101
-
102
-
103
-
104
-
105
- }
106
-
107
-
108
-
109
-
110
-
111
- @objc func keyboardWillHide(_ notification:NSNotification){
112
-
113
-
114
-
115
- messageTextField.frame.origin.y = screenSize.height - messageTextField.frame.height
116
-
117
-
118
-
119
- guard let rect = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue,
120
-
121
- let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else{return}
122
-
123
- UIView.animate(withDuration: duration) {
124
-
125
-
126
-
127
- //移動アニメーション? ↓view自体が上がってしまっている為、元の位置に戻す為の指定
128
-
129
- let transform = CGAffineTransform(translationX: 0, y: 0)
130
-
131
-
132
-
133
- self.view.transform = transform
134
-
135
-
136
-
137
- }
138
-
139
- }
140
-
141
-
142
-
143
- //タッチされた場合
144
-
145
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
146
-
147
-
148
-
149
- messageTextField.resignFirstResponder()
150
-
151
-
152
-
153
- }
154
-
155
-
156
-
157
- //Returnキーが押された場合
158
-
159
- func textFieldShouldReturn(_ textField: UITextField) -> Bool {
160
-
161
-
162
-
163
- textField.resignFirstResponder()
164
-
165
- return true
166
-
167
-
168
-
169
- }
170
-
171
-
172
-
173
-
174
-
175
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
176
-
177
-
178
-
179
- //メッセージの数
180
-
181
- return chatArray.count
182
-
183
-
184
-
185
- }
186
-
187
-
188
-
189
- func numberOfSections(in tableView: UITableView) -> Int {
190
-
191
- return 1
192
-
193
- }
194
-
195
-
196
-
197
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
198
-
199
-
200
-
201
- //このメソッドを呼び出す前に、registerとして登録する必要がある
202
-
203
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
204
-
205
-
206
-
207
- cell.messageLabel.text = chatArray[indexPath.row].message
208
-
209
-
210
-
211
- cell.userNameLabel.text = chatArray[indexPath.row].sender
212
-
213
- cell.iconImageView.image = UIImage(named:"dogAvatarImage")
214
-
215
-
216
-
217
-
218
-
219
- if cell.userNameLabel.text == Auth.auth().currentUser?.email as! String{
220
-
221
-
222
-
223
- cell.messageLabel.backgroundColor = UIColor.flatGreen()
224
-
225
- cell.messageLabel.layer.cornerRadius = 20
226
-
227
- cell.messageLabel.layer.masksToBounds = true
228
-
229
-
230
-
231
- }else{
232
-
233
-
234
-
235
- cell.messageLabel.backgroundColor = UIColor.flatBlue()
236
-
237
- cell.messageLabel.layer.cornerRadius = 20
238
-
239
- cell.messageLabel.layer.masksToBounds = true
240
-
241
- }
242
-
243
-
244
-
245
- return cell
246
-
247
-
248
-
249
- }
250
-
251
-
252
-
253
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
254
-
255
-
256
-
257
- return 100
258
-
259
- }
260
-
261
-
262
-
263
- @IBAction func sendAction(_ sender: Any) {
264
-
265
-
266
-
267
- messageTextField.endEditing(true)
268
-
269
- messageTextField.isEnabled = false
270
-
271
- //ボタン無効
272
-
273
- sendButton.isEnabled = false
274
-
275
-
276
-
277
- if messageTextField.text!.count > 15{
278
-
279
-
280
-
281
- print("15文字以上です!!。")
282
-
283
-
284
-
285
- return
286
-
287
-
288
-
289
- }
290
-
291
- let chatDB = Database.database().reference().child("chats")
292
-
293
-
294
-
295
- //キーバリュー型で内容を送信(Dictionary型)
296
-
297
-
298
-
299
- let messageInfo = ["sender":Auth.auth().currentUser?.email,"message":messageTextField.text!]
300
-
301
-
302
-
303
- //chatDBに入れる
304
-
305
- chatDB.childByAutoId().setValue(messageInfo) { (error, result) in
306
-
307
-
308
-
309
- if error != nil{
310
-
311
-
312
-
313
- print(error as Any)
314
-
315
-
316
-
317
- }else{
318
-
319
-
320
-
321
- print("送信完了")
322
-
323
- // self.sendButton.isEnabled = true
324
-
325
- // self.messageTextField.isEnabled = true
326
-
327
- // self.messageTextField.text = ""
328
-
329
-
330
-
331
- }
332
-
333
-
334
-
335
- }
336
-
337
-
338
-
339
- }
340
-
341
-
342
-
343
- //データを引っ張ってくる
344
-
345
- func fetchChatData(){
346
-
347
-
348
-
349
- //どこからデータを引っ張ってくるのか
350
-
351
- let fetchDataRef = Database.database().reference().child("chats")
352
-
353
-
354
-
355
- //新しく更新があった時だけ取得
356
-
357
- fetchDataRef.observe(.childAdded) { (snapShot) in
358
-
359
- let snapShotData = snapShot.value as AnyObject
360
-
361
- let text = snapShotData.value(forKey: "message")
362
-
363
- let sender = snapShotData.value(forKey: "sender")
364
-
365
-
366
-
367
- let message = Message()
368
-
369
- message.message = text as! String
370
-
371
- message.sender = sender as! String
372
-
373
- self.chatArray.append(message)
374
-
375
- self.tableView.reloadData()
376
-
377
-
378
-
379
- }
380
-
381
-
382
-
383
- }
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
- }
392
-
393
-
394
-
395
- ```
396
-
397
395
 
398
396
 
399
397
  データの取得処理は```func fetchdata()```に記載していますが、イベントリスナというのはデータの更新があった場合は、どこかの関数内であっても(どこに記載されていようとも)そこの処理部分だけ(今回は```fetchDataRef.observe```)呼び出されそこの処理を行う、のでしょうか?

1

追記、修正

2019/12/20 08:51

投稿

ruuuu
ruuuu

スコア174

test CHANGED
File without changes
test CHANGED
@@ -398,4 +398,4 @@
398
398
 
399
399
  データの取得処理は```func fetchdata()```に記載していますが、イベントリスナというのはデータの更新があった場合は、どこかの関数内であっても(どこに記載されていようとも)そこの処理部分だけ(今回は```fetchDataRef.observe```)呼び出されそこの処理を行う、のでしょうか?
400
400
 
401
- [Firebase公式ドキュメント](https://firebase.google.com/docs/database/ios/retrieve-data?hl=ja)には```データが変更されると、そのたびに再トリガーされます。```とあったのですが、関数の中にある関数だけ呼び出された場合、呼び出された後の処理はどうなるのかなど、少々疑問でした為、質問させて貰いました。
401
+ [Firebase公式ドキュメント](https://firebase.google.com/docs/database/ios/retrieve-data?hl=ja)には```データが変更されると、そのたびに再トリガーされます。```とあったのですが、関数の中にある関数(```fetchDataRef.observe```)だけ呼び出された場合、呼び出された後の処理はどうなるのかなど、少々疑問でした為、質問させて貰いました。