DocumentReference+WriteEncodable.swift 4.5 KB

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