DocumentReference+WriteEncodable.swift 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import FirebaseFirestore
  18. public extension DocumentReference {
  19. /// Encodes an instance of `Encodable` and overwrites the encoded data
  20. /// to the document referred by this `DocumentReference`. If no document exists,
  21. /// it is created. If a document already exists, it is overwritten.
  22. ///
  23. /// See `Firestore.Encoder` for more details about the encoding process.
  24. ///
  25. /// - Parameters:
  26. /// - value: An instance of `Encodable` to be encoded to a document.
  27. /// - encoder: An encoder instance to use to run the encoding.
  28. /// - completion: A closure to execute once the document has been successfully
  29. /// written to the server. This closure will not be called while
  30. /// the client is offline, though local changes will be visible
  31. /// immediately.
  32. func setData<T: Encodable>(from value: T,
  33. encoder: Firestore.Encoder = Firestore.Encoder(),
  34. completion: ((Error?) -> Void)? = nil) throws {
  35. let encoded = try encoder.encode(value)
  36. setData(encoded, completion: completion)
  37. }
  38. /// Encodes an instance of `Encodable` and overwrites the encoded data
  39. /// to the document referred by this `DocumentReference`. If no document exists,
  40. /// it is created. If a document already exists, it is overwritten. If you pass
  41. /// merge:true, the provided `Encodable` will be merged into any existing document.
  42. ///
  43. /// See `Firestore.Encoder` for more details about the encoding process.
  44. ///
  45. /// - Parameters:
  46. /// - value: An instance of `Encodable` to be encoded to a document.
  47. /// - merge: Whether to merge the provided `Encodable` into any existing
  48. /// document.
  49. /// - encoder: An encoder instance to use to run the encoding.
  50. /// - completion: A closure to execute once the document has been successfully
  51. /// written to the server. This closure will not be called while
  52. /// the client is offline, though local changes will be visible
  53. /// immediately.
  54. func setData<T: Encodable>(from value: T,
  55. merge: Bool,
  56. encoder: Firestore.Encoder = Firestore.Encoder(),
  57. completion: ((Error?) -> Void)? = nil) throws {
  58. let encoded = try encoder.encode(value)
  59. setData(encoded, merge: merge, completion: completion)
  60. }
  61. /// Encodes an instance of `Encodable` and writes the encoded data to the document referred
  62. /// by this `DocumentReference` by only replacing the fields specified under `mergeFields`.
  63. /// Any field that is not specified in mergeFields is ignored and remains untouched. If the
  64. /// document doesn’t yet exist, this method creates it and then sets the data.
  65. ///
  66. /// It is an error to include a field in `mergeFields` that does not have a corresponding
  67. /// field in the `Encodable`.
  68. ///
  69. /// See `Firestore.Encoder` for more details about the encoding process.
  70. ///
  71. /// - Parameters:
  72. /// - value: An instance of `Encodable` to be encoded to a document.
  73. /// - mergeFields: Array of `String` or `FieldPath` elements specifying which fields to
  74. /// merge. Fields can contain dots to reference nested fields within the
  75. /// document.
  76. /// - encoder: An encoder instance to use to run the encoding.
  77. /// - completion: A closure to execute once the document has been successfully
  78. /// written to the server. This closure will not be called while
  79. /// the client is offline, though local changes will be visible
  80. /// immediately.
  81. func setData<T: Encodable>(from value: T,
  82. mergeFields: [Any],
  83. encoder: Firestore.Encoder = Firestore.Encoder(),
  84. completion: ((Error?) -> Void)? = nil) throws {
  85. let encoded = try encoder.encode(value)
  86. setData(encoded, mergeFields: mergeFields, completion: completion)
  87. }
  88. }