【问题标题】:Using a for-loop to download images and save to core data使用 for 循环下载图像并保存到核心数据
【发布时间】:2020-10-15 09:54:04
【问题描述】:

我目前有一个用于从存储在数组中的 url 下载图像的 for 循环。每获取 2 或 3 个新图像后,我总是遇到相同图像的问题。每次我获取新图像时,我都可以看到它使用不同的 url 来下载图像,但数组中的图像是相同的。

下面是我的代码:

 func downloadImageDetailsFromFlickr(completion: @escaping (Bool, Error?) -> Void) {
        self.imageLoading(is: true)
        flickrClient.getImageDetailsFromFlickr(lat: latitude, lon: longitude) { (photo, error) in
            if error == nil {
                self.photoStore = []
                for image in photo {
                    if image.url_m.count == 0 {
                        self.imageLabel.isHidden = false
                        self.imageLoading(is: false)
                        completion(false, nil)
                    } else {
                      self.photoStore.append(image.url_m)
                        print(self.photoStore)
                        completion(true, nil)
                    }
                }
            } else {
                print(error?.localizedDescription ?? "Error in the fetch image block")
                print("problem in downloading details.")
                completion(false, error)
            }
        }
    }
    
    func downloadImage(completion: @escaping(Bool, Error?) -> Void ) {
        let flickrImage = FlickrImage(context: self.dataController.viewContext)
        
        for flickr in photoStore {
            self.photoUrl = ""
            self.photoUrl = flickr
        }
            flickrClient.getImage(imageUrl: photoUrl ?? "") { (data, error) in
                if error == nil {
                self.imageLoading(is: false)
                    flickrImage.photo = data
                    flickrImage.url = self.photoUrl
                    flickrImage.locations = self.location
                    flickrImage.locations?.latitude = self.latitude
                    flickrImage.locations?.longitude = self.longitude
                }
                else {
                    print(error?.localizedDescription ?? "Something went wrong downloading an image")
                    }
            do {
                 try self.dataController.viewContext.save()
                 self.photoStore.removeAll()
                 completion(true, nil)
                } catch {
                 print("Error saving into Core Data")
                 completion(false, error)
                }
            }
}

请忽略红色框,白色框表示正在重新获取的图像。我的猜测是,它与核心数据有关。

【问题讨论】:

  • 此 for 循环 for flickr in photoStore { self.photoUrl = "" self.photoUrl = flickr } 始终将 photostore 的最后一项存储在 photoUrl 中。这个循环的目的是什么?
  • @luckystars 我希望它一次传入每个值。它奏效了,它消除了网格布局中重复的图像,但是在获取了几次新图像之后,你会得到相同的图像网格。

标签: ios swift core-data uicollectionview nsfetchedresultscontroller


【解决方案1】:

我不确定这是否能解决您的问题,但您可以尝试使用 dispatchgroup()

它会是这样的

 let group = DispatchGroup()
    for flickr in photoStore {
//fetch image from url here
       group.enter()
       flickrClient.getImage(imageUrl: flickr ?? "") { (data, error) in
             if error == nil {
                self.imageLoading(is: false)
                    flickrImage.photo = data
                    flickrImage.url = self.photoUrl
                    flickrImage.locations = self.location
                    flickrImage.locations?.latitude = self.latitude
                    flickrImage.locations?.longitude = self.longitude
                    group.leave()

                }
                else {
                    print(error?.localizedDescription ?? "Something went wrong downloading an image")
                    group.leave()
                 }
        }

             group.notify(queue: .main) { [weak self] in

              //save your dowloaded image here
                 try self?.dataController.viewContext.save()
                     self?.photoStore.removeAll()
                   
                 } catch {
                    print("Error saving into Core Data")
                }
}

所以基本上通过使用 dispatchgroup,您可以将任务分组并异步运行。它只是为了确保请求不会重复,并且您将图像保存到核心数据一次。

【讨论】:

  • 感谢您的回复,我试过了,结果是一样的。我试图弄清楚还有哪些其他方法可以存储传递的 url 并通过它们进行过滤。
猜你喜欢
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 2020-10-31
  • 1970-01-01
相关资源
最近更新 更多