-
I'm currently implementing a caching system with GRDB, and that involves syncing new data with the cached one. I have a singleton class, Private method private func modify<Object>(
object: Object.Type,
at id: Int,
from db: Database,
transform: (inout Object) -> Void) throws
where Object: FetchableRecord, Object: PersistableRecord {
if var value = try object.filter(Column("id") == id).fetchOne(db) {
try value.updateChanges(db) {
transform(&$0)
}
}
} Public method func modify<Object>(object: Object.Type, at id: Int, transform: (inout Object) -> Void) throws
where Object: FetchableRecord, Object: PersistableRecord {
try dbQueue.write { db in
try modify(object: object, at: id, from: db, transform: transform)
}
} And then it's usage: let objects = try cache.getObjects(as: Caching.ChatFilter.self)
TdApi.logger.debug("Going over received chat filters")
for chatFilter in update.chatFilters {
if objects.contains(where: { $0.id == chatFilter.id }) {
TdApi.logger.debug("Updating filter with id \(chatFilter.id) in database")
try cache.modify(object: Caching.ChatFilter.self, at: chatFilter.id) {
$0.title = chatFilter.title
$0.id = chatFilter.id
$0.iconName = chatFilter.iconName
}
} else {
TdApi.logger.debug("Creating a new one with id \(chatFilter.id)")
try cache.save(object: Caching.ChatFilter(
title: chatFilter.title,
id: chatFilter.id,
iconName: chatFilter.iconName
))
}
}
TdApi.logger.debug("Going over filters in database")
for object in objects {
if !update.chatFilters.contains(where: { $0.id == object.id }) {
TdApi.logger.debug("Update does not contain filter with id \(object.id), removing from database")
try cache.delete(object: object)
}
} I placed some debug logs in between the code so I can see what it does so I can share it. And below is exactly what it does:
For some reason, it can't find the record to update. What am I doing wrong? :) Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hello @ggoraa,
The "Key not found" error message comes from PersistenceError, which is thrown by the It says: "well, you asked me to update the chatFilter whose rowid is NULL, and I could not find any, so I prefer telling you that the update has failed with an error, so that you don't make wrong assumptions". Indeed, no row in the database can have the value Now, you may wonder why the This is a mix between the database schema, and how your record type implements the
|
Beta Was this translation helpful? Give feedback.
Hello @ggoraa,
The "Key not found" error message comes from PersistenceError, which is thrown by the
update
method (and related ones such asupdateChanges
), when it asked to update a row that does not exist in the database.It says: "well, you asked me to update the chatFilter whose rowid is NULL, and I could not find any, so I prefer telling you that the update has failed with an error, so that you don't make wrong assumptions".
Indeed, no row in the database can have the value
NULL
in therowid
column.Now, you may wonder why the
update
method attempts at identifying a row with a NULLrowid
…