DocumentReference+WriteEncodable.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. #if SWIFT_PACKAGE
  18. @_exported import FirebaseFirestoreInternalWrapper
  19. #else
  20. @_exported import FirebaseFirestoreInternal
  21. #endif // SWIFT_PACKAGE
  22. public extension DocumentReference {
  23. /// Encodes an instance of `Encodable` and overwrites the encoded data
  24. /// to the document referred by this `DocumentReference`. If no document exists,
  25. /// it is created. If a document already exists, it is overwritten.
  26. ///
  27. /// See `Firestore.Encoder` for more details about the encoding process.
  28. ///
  29. /// - Parameters:
  30. /// - value: An instance of `Encodable` to be encoded to a document.
  31. /// - encoder: An encoder instance to use to run the encoding.
  32. /// - completion: A closure to execute once the document has been successfully
  33. /// written to the server. This closure will not be called while
  34. /// the client is offline, though local changes will be visible
  35. /// immediately.
  36. func setData<T: Encodable>(from value: T,
  37. encoder: Firestore.Encoder = Firestore.Encoder(),
  38. completion: ((Error?) -> Void)? = nil) throws {
  39. let encoded = try encoder.encode(value)
  40. setData(encoded, completion: completion)
  41. }
  42. /// Encodes an instance of `Encodable` and overwrites the encoded data
  43. /// to the document referred by this `DocumentReference`. If no document exists,
  44. /// it is created. If a document already exists, it is overwritten. If you pass
  45. /// merge:true, the provided `Encodable` will be merged into any existing document.
  46. ///
  47. /// See `Firestore.Encoder` for more details about the encoding process.
  48. ///
  49. /// - Parameters:
  50. /// - value: An instance of `Encodable` to be encoded to a document.
  51. /// - merge: Whether to merge the provided `Encodable` into any existing
  52. /// document.
  53. /// - encoder: An encoder instance to use to run the encoding.
  54. /// - completion: A closure to execute once the document has been successfully
  55. /// written to the server. This closure will not be called while
  56. /// the client is offline, though local changes will be visible
  57. /// immediately.
  58. func setData<T: Encodable>(from value: T,
  59. merge: Bool,
  60. encoder: Firestore.Encoder = Firestore.Encoder(),
  61. completion: ((Error?) -> Void)? = nil) throws {
  62. let encoded = try encoder.encode(value)
  63. setData(encoded, merge: merge, completion: completion)
  64. }
  65. /// Encodes an instance of `Encodable` and writes the encoded data to the document referred
  66. /// by this `DocumentReference` by only replacing the fields specified under `mergeFields`.
  67. /// Any field that is not specified in mergeFields is ignored and remains untouched. If the
  68. /// document doesn’t yet exist, this method creates it and then sets the data.
  69. ///
  70. /// It is an error to include a field in `mergeFields` that does not have a corresponding
  71. /// field in the `Encodable`.
  72. ///
  73. /// See `Firestore.Encoder` for more details about the encoding process.
  74. ///
  75. /// - Parameters:
  76. /// - value: An instance of `Encodable` to be encoded to a document.
  77. /// - mergeFields: Array of `String` or `FieldPath` elements specifying which fields to
  78. /// merge. Fields can contain dots to reference nested fields within the
  79. /// document.
  80. /// - encoder: An encoder instance to use to run the encoding.
  81. /// - completion: A closure to execute once the document has been successfully
  82. /// written to the server. This closure will not be called while
  83. /// the client is offline, though local changes will be visible
  84. /// immediately.
  85. func setData<T: Encodable>(from value: T,
  86. mergeFields: [Any],
  87. encoder: Firestore.Encoder = Firestore.Encoder(),
  88. completion: ((Error?) -> Void)? = nil) throws {
  89. let encoded = try encoder.encode(value)
  90. setData(encoded, mergeFields: mergeFields, completion: completion)
  91. }
  92. }