-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathParseObject+combine.swift
152 lines (141 loc) · 7.91 KB
/
ParseObject+combine.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// ParseObject+combine.swift
// ParseSwift
//
// Created by Corey Baker on 1/29/21.
// Copyright © 2021 Parse Community. All rights reserved.
//
#if canImport(Combine)
import Foundation
import Combine
public extension ParseObject {
// MARK: Fetchable - Combine
/**
Fetches the `ParseObject` *aynchronously* with the current data from the server and sets an error if one occurs.
Publishes when complete.
- parameter includeKeys: The name(s) of the key(s) to include that are
`ParseObject`s. Use `["*"]` to include all keys. This is similar to `include` and
`includeAll` for `Query`.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
- note: The default cache policy for this method is `.reloadIgnoringLocalCacheData`. If a developer
desires a different policy, it should be inserted in `options`.
*/
func fetchPublisher(includeKeys: [String]? = nil,
options: API.Options = []) -> Future<Self, ParseError> {
Future { promise in
self.fetch(includeKeys: includeKeys,
options: options,
completion: promise)
}
}
/**
Saves the `ParseObject` *asynchronously* and publishes when complete.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
- important: If an object saved has the same objectId as current, it will automatically update the current.
*/
func savePublisher(isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = []) -> Future<Self, ParseError> {
Future { promise in
self.save(isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig,
options: options,
completion: promise)
}
}
/**
Deletes the `ParseObject` *asynchronously* and publishes when complete.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
- important: If an object deleted has the same objectId as current, it will automatically update the current.
*/
func deletePublisher(options: API.Options = []) -> Future<Void, ParseError> {
Future { promise in
self.delete(options: options, completion: promise)
}
}
}
public extension Sequence where Element: ParseObject {
// MARK: Batch Support - Combine
/**
Fetches a collection of objects *aynchronously* with the current data from the server and sets
an error if one occurs. Publishes when complete.
- parameter includeKeys: The name(s) of the key(s) to include that are
`ParseObject`s. Use `["*"]` to include all keys. This is similar to `include` and
`includeAll` for `Query`.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
- important: If an object fetched has the same objectId as current, it will automatically update the current.
*/
func fetchAllPublisher(includeKeys: [String]? = nil,
options: API.Options = []) -> Future<[(Result<Self.Element, ParseError>)], ParseError> {
Future { promise in
self.fetchAll(includeKeys: includeKeys,
options: options,
completion: promise)
}
}
/**
Saves a collection of objects *asynchronously* and publishes when complete.
- parameter batchLimit: The maximum number of objects to send in each batch. If the items to be batched.
is greater than the `batchLimit`, the objects will be sent to the server in waves up to the `batchLimit`.
Defaults to 50.
- parameter transaction: Treat as an all-or-nothing operation. If some operation failure occurs that
prevents the transaction from completing, then none of the objects are committed to the Parse Server database.
- parameter isIgnoreCustomObjectIdConfig: Ignore checking for `objectId`
when `ParseConfiguration.allowCustomObjectId = true` to allow for mixed
`objectId` environments. Defaults to false.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
- important: If an object saved has the same objectId as current, it will automatically update the current.
- warning: If `transaction = true`, then `batchLimit` will be automatically be set to the amount of the
objects in the transaction. The developer should ensure their respective Parse Servers can handle the limit or else
the transactions can fail.
- warning: If you are using `ParseConfiguration.allowCustomObjectId = true`
and plan to generate all of your `objectId`'s on the client-side then you should leave
`isIgnoreCustomObjectIdConfig = false`. Setting
`ParseConfiguration.allowCustomObjectId = true` and
`isIgnoreCustomObjectIdConfig = true` means the client will generate `objectId`'s
and the server will generate an `objectId` only when the client does not provide one. This can
increase the probability of colliiding `objectId`'s as the client and server `objectId`'s may be generated using
different algorithms. This can also lead to overwriting of `ParseObject`'s by accident as the
client-side checks are disabled. Developers are responsible for handling such cases.
*/
func saveAllPublisher(batchLimit limit: Int? = nil,
transaction: Bool = false,
isIgnoreCustomObjectIdConfig: Bool = false,
options: API.Options = []) -> Future<[(Result<Self.Element, ParseError>)], ParseError> {
Future { promise in
self.saveAll(batchLimit: limit,
transaction: transaction,
isIgnoreCustomObjectIdConfig: isIgnoreCustomObjectIdConfig,
options: options,
completion: promise)
}
}
/**
Deletes a collection of objects *asynchronously* and publishes when complete.
- parameter batchLimit: The maximum number of objects to send in each batch. If the items to be batched.
is greater than the `batchLimit`, the objects will be sent to the server in waves up to the `batchLimit`.
Defaults to 50.
- parameter transaction: Treat as an all-or-nothing operation. If some operation failure occurs that
prevents the transaction from completing, then none of the objects are committed to the Parse Server database.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
- important: If an object deleted has the same objectId as current, it will automatically update the current.
- warning: If `transaction = true`, then `batchLimit` will be automatically be set to the amount of the
objects in the transaction. The developer should ensure their respective Parse Servers can handle the limit or else
the transactions can fail.
*/
func deleteAllPublisher(batchLimit limit: Int? = nil,
transaction: Bool = false,
options: API.Options = []) -> Future<[(Result<Void, ParseError>)], ParseError> {
Future { promise in
self.deleteAll(batchLimit: limit,
transaction: transaction,
options: options,
completion: promise)
}
}
}
#endif