Sfoglia il codice sorgente

[FirebaseCore] Use optional instead of error (#10906)

* [FirebaseCore] Use optional instead of error

* Revert debugging change

* Fix typo
Nick Cooke 3 anni fa
parent
commit
1f3befad87

+ 9 - 4
FirebaseCore/Internal/Sources/HeartbeatLogging/HeartbeatStorage.swift

@@ -134,11 +134,16 @@ final class HeartbeatStorage: HeartbeatStorageProtocol {
 
   /// Loads and decodes the stored heartbeats bundle from a given storage object.
   /// - Parameter storage: The storage container to read from.
-  /// - Returns: The decoded `HeartbeatsBundle` that is loaded from storage.
-  private func load(from storage: Storage) throws -> HeartbeatsBundle {
+  /// - Returns: The decoded `HeartbeatsBundle` loaded from storage; `nil` if storage is empty.
+  /// - Throws: An error if storage could not be read or the data could not be decoded.
+  private func load(from storage: Storage) throws -> HeartbeatsBundle? {
     let data = try storage.read()
-    let heartbeatData = try data.decoded(using: decoder) as HeartbeatsBundle
-    return heartbeatData
+    if data.isEmpty {
+      return nil
+    } else {
+      let heartbeatData = try data.decoded(using: decoder) as HeartbeatsBundle
+      return heartbeatData
+    }
   }
 
   /// Saves the encoding of the given value to the given storage container.