Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.55 KB | None | 0 0
  1. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  2.         let cell = tableView.dequeueReusableCell(withIdentifier: "BookCell", for: indexPath) as! BookTableViewCell
  3.        
  4.         // Configure the cell...
  5.         let book = books[indexPath.row]
  6.         cell.nameLabel.text = book.object(forKey: "name") as? String
  7.         cell.typeLabel.text = book.object(forKey: "genre") as? String
  8.         cell.isbnLabel.text = book.object(forKey: "isbn") as? String
  9.  
  10.         // Set the default book image
  11.         cell.thumbnailImageView.image = UIImage(named: "photo_album")
  12.        
  13.         // Check if the book image is stored in cache
  14.         if let imageFileURL = imageCache.object(forKey: book.recordID) {
  15.             // Fetch image from cache
  16.             if let imageData = try? Data.init(contentsOf: imageFileURL as URL) {
  17.                 cell.thumbnailImageView.image = UIImage(data: imageData)
  18.             }
  19.            
  20.         } else {
  21.             // Fetch Image from Cloud in background
  22.             let publicDatabase = CKContainer.default().publicCloudDatabase
  23.             let fetchBookImageRecordsOperation = CKFetchRecordsOperation(recordIDs: [book.recordID])
  24.             fetchBookImageRecordsOperation.desiredKeys = ["image"]
  25.             fetchBookImageRecordsOperation.queuePriority = .veryHigh
  26.            
  27.             fetchBookImageRecordsOperation.perRecordCompletionBlock = { (record, recordID, error) -> Void in
  28.                 if let error = error {
  29.                     print("Failed to get book image: \(error.localizedDescription)")
  30.                     return
  31.                 }
  32.                
  33.                 if let bookRecord = record {
  34.                     OperationQueue.main.addOperation() {
  35.                         if let image = bookRecord.object(forKey: "image") {
  36.                             let bookImageAsset = image as! CKAsset
  37.                            
  38.                             if let imageData = try? Data.init(contentsOf: bookImageAsset.fileURL) {
  39.                                 cell.thumbnailImageView.image = UIImage(data: imageData)
  40.                             }
  41.                            
  42.                             // Add the book image URL to cache
  43.                             self.imageCache.setObject(bookImageAsset.fileURL as NSURL, forKey: book.recordID)
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.            
  49.             publicDatabase.add(fetchBookImageRecordsOperation)
  50.         }
  51.        
  52.         return cell
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement