Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions GoMoney/Service/DataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,14 @@ class DataService {
}

/// get Transaction by id.
func getTransaction(by id: String, completion: ((Expense?) -> Void)? = nil) {
let transaction = realm.objects(Expense.self)
.first(where: { $0._id.stringValue == id })
completion?(transaction)
func getTransaction(by id: String) -> Expense? {
guard let objectId = try? ObjectId(string: id) else {
return nil
}
return realm.object(
ofType: Expense.self,
forPrimaryKey: objectId
)
}

func dropAllTable() {
Expand Down
33 changes: 17 additions & 16 deletions GoMoney/Service/RemoteService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,23 @@ class RemoteService {
return
}

local.getTransaction(by: id) { transaction in
guard let transaction = transaction else {
print("Transaction \(id) not found at local.")
return
}
do {
try self.db.collection("transactions")
.document(userId)
.collection("transactions")
.document(id)
.setData(from: transaction)

completion(nil)
} catch {
completion(error)
}
let transaction = local.getTransaction(by: id)

guard let transaction = transaction else {
print("Transaction \(id) not found at local.")
return
}

do {
try db.collection("transactions")
.document(userId)
.collection("transactions")
.document(id)
.setData(from: transaction)

completion(nil)
} catch {
completion(error)
}
}

Expand Down