Hello,
I have two base classes
struct ParseCategory: ParseObject {
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var originalData: Data?
var name: String?
var index: Int?
var icon: ParseFile?
var articles: ParseRelation<Self>?
func merge(with object: Self) throws -> Self {
var updated = try mergeParse(with: object)
if updated.shouldRestoreKey(\.name, original: object) {
updated.name = object.name
}
if updated.shouldRestoreKey(\.index, original: object) {
updated.index = object.index
}
if updated.shouldRestoreKey(\.icon, original: object) {
updated.icon = object.icon
}
return updated
}
}
struct ParseArticle: ParseObject {
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
var originalData: Data?
// Custom properties
var headline: String?
var author: String?
var lead: String?
var body: String?
var tail: String?
var heroImage: ParseFile?
var bodyImage: ParseFile?
var sourceImage: ParseFile?
var sourceUrl: String?
var category: String?
var breakingNews: Bool?
var newsOfTheDay: Bool?
// Implement your own version of merge
func merge(with object: Self) throws -> Self {
var updated = try mergeParse(with: object)
if updated.shouldRestoreKey(\.headline, original: object) {
updated.headline = object.headline
}
if updated.shouldRestoreKey(\.author, original: object) {
updated.author = object.author
}
if updated.shouldRestoreKey(\.lead, original: object) {
updated.lead = object.lead
}
if updated.shouldRestoreKey(\.body, original: object) {
updated.body = object.body
}
if updated.shouldRestoreKey(\.tail, original: object) {
updated.tail = object.tail
}
if updated.shouldRestoreKey(\.sourceUrl, original: object) {
updated.sourceUrl = object.sourceUrl
}
if updated.shouldRestoreKey(\.category, original: object) {
updated.category = object.category
}
if updated.shouldRestoreKey(\.breakingNews, original: object) {
updated.breakingNews = object.breakingNews
}
if updated.shouldRestoreKey(\.newsOfTheDay, original: object) {
updated.newsOfTheDay = object.newsOfTheDay
}
return updated
}
}
I have newsgroups on my server. Logically, each group should contain a set of news related to this group.
When creating a news, I get the desired group by ID, create news and try to set a one-to-many relationship in the group.
First method:
func save(for article: Article, with categoryId: String) {
if let parseCategory = try? ParseCategory.query("objectId" == categoryId).first() {
let parseArticle = ParseArticle(article: article)
if let savedArticle = try? parseArticle.save() {
if let relationOperation = try? parseCategory.operation.addRelation("articles", objects: [savedArticle]) {
_ = try? relationOperation.save()
}
}
}
}
Other method:
func save(for article: Article, with categoryId: String) {
if let parseCategory = try? ParseCategory.query("objectId" == categoryId).first() {
let parseArticle = ParseArticle(article: article)
if let savedArticle = try? parseArticle.save() {
do {
guard let newRelations = try parseCategory.relation?.add("articles", objects: [savedArticle]) else {
print("Error: should have unwrapped relation")
return
}
newRelations.save { result in
switch result {
case .success(let saved):
print("The relation saved successfully: \(saved)")
case .failure(let error):
print("Error saving role: \(error)")
}
}
}
catch {
print(error)
}
}
}
}
None of the methods wants to save the relationship. I studied the documentation and tried different ways, but the relations on the server are empty and do not refer to the added instances.
What am I doing wrong?