質問編集履歴

3

savePhotoUrl変数の代入位置変更(できたとこまで。)

2018/10/11 05:32

投稿

kou009
kou009

スコア16

test CHANGED
File without changes
test CHANGED
@@ -246,17 +246,21 @@
246
246
 
247
247
  let photoUrl = URL(string: photoUrlString)
248
248
 
249
- savePhotoUrl = photoUrl
250
-
251
249
 
252
250
 
253
251
  // 読み込み(新規)
254
252
 
255
253
  if savePhotoUrl == nil {
256
254
 
257
- self.photo.sd_setImage(with: photoUrl)
255
+ self.photo.sd_setImage(with: photoUrl) { (image, error, SDImageCacheType, url) in
256
+
258
-
257
+ self.savePhotoUrl = url
258
+
259
- }
259
+ }
260
+
261
+ }
262
+
263
+
260
264
 
261
265
  // 何もしない(キャッシュ)
262
266
 
@@ -268,9 +272,11 @@
268
272
 
269
273
  // キャンセル処理後に読み込み
270
274
 
271
- else if savePhotoUrl != photoUrl /*&& 読み込み中*/ {
275
+ else if savePhotoUrl != photoUrl /*&& 読み込み中*/ {
272
-
276
+
273
- self.photo.sd_setImage(with: savePhotoUrl)
277
+ self.photo.sd_setImage(with: photoUrl) { (image, error, SDImageCacheType, url) in
278
+
279
+ self.savePhotoUrl = url
274
280
 
275
281
  }
276
282
 

2

Postクラス(自作クラス)の追記と、UICollectionViewDataSourceを継承したクラスのviewDidLoad()の部分 を追記。

2018/10/11 05:32

投稿

kou009
kou009

スコア16

test CHANGED
File without changes
test CHANGED
@@ -44,6 +44,56 @@
44
44
 
45
45
 
46
46
 
47
+ Postクラス
48
+
49
+ ```Swift
50
+
51
+ class Post {
52
+
53
+
54
+
55
+ var id: String?
56
+
57
+ var timestamp: Int?
58
+
59
+ var imageUrls = [String]()
60
+
61
+
62
+
63
+ }
64
+
65
+
66
+
67
+
68
+
69
+ extension Post {
70
+
71
+ static func transformPostPhoto(dict: [String: Any], key: String) -> Post {
72
+
73
+ let post = Post()
74
+
75
+ post.id = key
76
+
77
+ post.timestamp = dict["timestamp"] as? Int
78
+
79
+ let image0 = dict["image0"] as? String
80
+
81
+ let image1 = dict["image1"] as? String
82
+
83
+ post.imageUrls.append(image0!)
84
+
85
+ post.imageUrls.append(image1!)
86
+
87
+ return post
88
+
89
+ }
90
+
91
+ }
92
+
93
+ ```
94
+
95
+
96
+
47
97
  UICollectionViewDataSourceと、UICollectionViewDelegateFlowLayoutを継承したクラス
48
98
 
49
99
  ```Swift
@@ -198,19 +248,53 @@
198
248
 
199
249
  savePhotoUrl = photoUrl
200
250
 
251
+
252
+
253
+ // 読み込み(新規)
254
+
255
+ if savePhotoUrl == nil {
256
+
257
+ self.photo.sd_setImage(with: photoUrl)
258
+
259
+ }
260
+
261
+ // 何もしない(キャッシュ)
262
+
263
+ else if savePhotoUrl == photoUrl {
264
+
265
+ print("何もしない(キャッシュ)")
266
+
267
+ }
268
+
269
+ // キャンセル処理後に読み込み
270
+
271
+ else if savePhotoUrl != photoUrl /*&& 読み込み中*/ {
272
+
273
+ self.photo.sd_setImage(with: savePhotoUrl)
274
+
275
+ }
276
+
277
+ // (必要なら解放処理後に)読み込み
278
+
279
+ else if savePhotoUrl != photoUrl {
280
+
281
+
282
+
283
+ }
284
+
201
285
 
202
286
 
203
- if self.savePhotoUrl == photoUrl {
287
+ // if self.savePhotoUrl == photoUrl {
204
288
 
205
289
 
206
290
 
207
- self.photo.sd_setImage(with: photoUrl)
291
+ // self.photo.sd_setImage(with: photoUrl)
208
-
292
+
209
- } else {
293
+ // } else {
210
-
294
+
211
- print("一致しないので読み込みキャンセル")
295
+ // print("一致しないので読み込みキャンセル")
212
-
296
+
213
- }
297
+ // }
214
298
 
215
299
  }
216
300
 

1

PostCollectionViewCellクラスのfunc updateView()を修正、追記しました。

2018/10/11 02:55

投稿

kou009
kou009

スコア16

test CHANGED
File without changes
test CHANGED
@@ -54,6 +54,48 @@
54
54
 
55
55
 
56
56
 
57
+ //追記
58
+
59
+ override func viewDidLoad() {
60
+
61
+ super.viewDidLoad()
62
+
63
+
64
+
65
+ var REF_POSTS = Database.database().reference().child("posts")
66
+
67
+ REF_POSTS.queryOrdered(byChild: "timestamp").observeSingleEvent(of: .value) { (snapshot) in
68
+
69
+ if let arraySnapshot = (snapshot.children.allObjects as? [DataSnapshot])?.reversed() {
70
+
71
+
72
+
73
+ arraySnapshot.forEach({ (child) in
74
+
75
+ if let dict = child.value as? [String: Any] {
76
+
77
+ let post = Post.transformPostPhoto(dict: dict, key: child.key)
78
+
79
+
80
+
81
+ self.posts.append(post)
82
+
83
+ }
84
+
85
+ })
86
+
87
+
88
+
89
+ self.collectionView.reloadData()
90
+
91
+ }
92
+
93
+ }
94
+
95
+ }
96
+
97
+
98
+
57
99
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
58
100
 
59
101
  return posts.count
@@ -124,6 +166,8 @@
124
166
 
125
167
  @IBOutlet weak var photo: UIImageView!
126
168
 
169
+ var savePhotoUrl: URL?
170
+
127
171
 
128
172
 
129
173
  var post: Post? {
@@ -138,17 +182,39 @@
138
182
 
139
183
 
140
184
 
185
+ //修正&追記
186
+
141
187
  func updateView() {
142
188
 
189
+ print("Thread.isMainThread ", Thread.isMainThread) // true
190
+
191
+ print("Thread.current ", Thread.current) //<NSThread: 0x281e91d40>{number = 1, name = main}
192
+
143
193
  if let photoUrlString = post?.imageUrls[0] {
144
194
 
195
+
196
+
145
197
  let photoUrl = URL(string: photoUrlString)
146
198
 
199
+ savePhotoUrl = photoUrl
200
+
201
+
202
+
203
+ if self.savePhotoUrl == photoUrl {
204
+
205
+
206
+
147
- photo.sd_setImage(with: photoUrl)
207
+ self.photo.sd_setImage(with: photoUrl)
208
+
209
+ } else {
210
+
211
+ print("一致しないので読み込みキャンセル")
212
+
213
+ }
148
214
 
149
215
  }
150
216
 
151
- }
217
+ }
152
218
 
153
219
  }
154
220