Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mode offline Cocoaheads

Mode offline Cocoaheads

Mathieu Hausherr

May 17, 2018
Tweet

More Decks by Mathieu Hausherr

Other Decks in Technology

Transcript

  1. 3

  2. 4

  3. 5

  4. 6

  5. 7

  6. 8

  7. 9

  8. 10

  9. X

  10. 12

  11. 13

  12. 14

  13. 16

  14. X Cache all requests if !isNetworkReachable && readFromCache(handler) { return

    } manager.request(baseURL + path, method: method, parameters: parameters, encoding: encoding) .validate() .responseJSON { response in switch response.result { case .failure(let error): self.readFromCache(handler) case .success: self.writeResponseToCache(response: response) } }
  15. X Cache all requests if !isNetworkReachable && readFromCache(handler) { return

    } manager.request(baseURL + path, method: method, parameters: parameters, encoding: encoding) .validate() .responseJSON { response in switch response.result { case .failure(let error): self.readFromCache(handler) case .success: self.writeResponseToCache(response: response) } }
  16. X Cache all requests if !isNetworkReachable && readFromCache(handler) { return

    } manager.request(baseURL + path, method: method, parameters: parameters, encoding: encoding) .validate() .responseJSON { response in switch response.result { case .failure(let error): self.readFromCache(handler) case .success: self.writeResponseToCache(response: response) } }
  17. X Cache all requests if !isNetworkReachable && readFromCache(handler) { return

    } manager.request(baseURL + path, method: method, parameters: parameters, encoding: encoding) .validate() .responseJSON { response in switch response.result { case .failure(let error): self.readFromCache(handler) case .success: self.writeResponseToCache(response: response) } }
  18. X Cache strategies switch cacheMode { case .standard: if !isNetworkReachable

    && readFromCache(handler) { return } case .forceCache: readFromCache(handler); return case .twoStepLoading: readFromCache(handler) }
  19. X Cache strategies switch cacheMode { case .standard: if !isNetworkReachable

    && readFromCache(handler) { return } case .forceCache: readFromCache(handler); return case .twoStepLoading: readFromCache(handler) }
  20. X Cache strategies switch cacheMode { case .standard: if !isNetworkReachable

    && readFromCache(handler) { return } case .forceCache: readFromCache(handler); return case .twoStepLoading: readFromCache(handler) }
  21. X Cache Swift func getData() -> Data? { try? Data(contentsOf:

    cacheURL) } func set(data: Data) { try? data.write(to: cacheURL, options: [.atomic]) } var isCacheStillValid: Bool { guard let attributes = try? FileManager.default.attributesOfItem(atPath: pathForCacheFile) else { return false } guard let modificationDate = attributes[FileAttributeKey.modificationDate] as? Date else { return false } return self.cacheDuration - modificationDate.timeIntervalSinceNow > 0 }
  22. X Cache Swift func getData() -> Data? { try? Data(contentsOf:

    cacheURL) } func set(data: Data) { try? data.write(to: cacheURL, options: [.atomic]) } var isCacheStillValid: Bool { guard let attributes = try? FileManager.default.attributesOfItem(atPath: pathForCacheFile) else { return false } guard let modificationDate = attributes[FileAttributeKey.modificationDate] as? Date else { return false } return self.cacheDuration - modificationDate.timeIntervalSinceNow > 0 }
  23. X

  24. 31

  25. X Déclaration la plus rapide possible, image avec sha256 POST

    /inspection [{
 "created_at": …,
 "zone_id": …,
 "image_hash": …
 }]
  26. 33

  27. 36

  28. X

  29. X

  30. X

  31. X Image cache Swift class ImageCache { var inMemoryCache: [URL:

    UIImage] = [:] func image(forURL url: URL) -> UIImage? { if let image = inMemoryCache[url] { return image } if let image = readImageFromCache(url) { inMemoryCache[urlString] = image; return image } else { return nil } } }
  32. 42

  33. X

  34. X

  35. X