Fix Realm crashes when trying to delete certain objects

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-02-02 20:12:03 +01:00
parent 6a258c0c37
commit 98afd95e59
No known key found for this signature in database
GPG key ID: C839200C384636B0

View file

@ -109,7 +109,7 @@ class NextcloudFilesDatabaseManager : NSObject {
} }
private func processItemMetadatasToDelete(databaseToWriteTo: Realm, private func processItemMetadatasToDelete(databaseToWriteTo: Realm,
existingMetadatas: [NextcloudItemMetadataTable], existingMetadatas: Results<NextcloudItemMetadataTable>,
updatedMetadatas: [NextcloudItemMetadataTable]) { updatedMetadatas: [NextcloudItemMetadataTable]) {
assert(databaseToWriteTo.isInWriteTransaction) assert(databaseToWriteTo.isInWriteTransaction)
@ -125,12 +125,14 @@ class NextcloudFilesDatabaseManager : NSObject {
etag: %@ etag: %@
""" """
, metadataToDelete.ocId, metadataToDelete.fileName, metadataToDelete.etag) , metadataToDelete.ocId, metadataToDelete.fileName, metadataToDelete.etag)
databaseToWriteTo.delete(metadataToDelete)
// Can't pass copies, we need the originals from the database
databaseToWriteTo.delete(ncDatabase().objects(NextcloudItemMetadataTable.self).filter("ocId == %@", metadataToDelete.ocId))
} }
} }
private func processItemMetadatasToUpdate(databaseToWriteTo: Realm, private func processItemMetadatasToUpdate(databaseToWriteTo: Realm,
existingMetadatas: [NextcloudItemMetadataTable], existingMetadatas: Results<NextcloudItemMetadataTable>,
updatedMetadatas: [NextcloudItemMetadataTable]) { updatedMetadatas: [NextcloudItemMetadataTable]) {
assert(databaseToWriteTo.isInWriteTransaction) assert(databaseToWriteTo.isInWriteTransaction)
@ -170,12 +172,12 @@ class NextcloudFilesDatabaseManager : NSObject {
do { do {
try database.write { try database.write {
let existingMetadatas = itemMetadatas(account: account, serverUrl: serverUrl, status: .normal) let existingMetadatas = ncDatabase().objects(NextcloudItemMetadataTable.self).filter("account == %@ AND serverUrl == %@ AND status == %@", account, serverUrl, NextcloudItemMetadataTable.Status.normal.rawValue)
processItemMetadatasToDelete(databaseToWriteTo: database, processItemMetadatasToDelete(databaseToWriteTo: database,
existingMetadatas: existingMetadatas, existingMetadatas: existingMetadatas,
updatedMetadatas: updatedMetadatas) updatedMetadatas: updatedMetadatas)
processItemMetadatasToUpdate(databaseToWriteTo: database, processItemMetadatasToUpdate(databaseToWriteTo: database,
existingMetadatas: existingMetadatas, existingMetadatas: existingMetadatas,
updatedMetadatas: updatedMetadatas) updatedMetadatas: updatedMetadatas)
@ -212,7 +214,7 @@ class NextcloudFilesDatabaseManager : NSObject {
} }
private func processDirectoryMetadatasToDelete(databaseToWriteTo: Realm, private func processDirectoryMetadatasToDelete(databaseToWriteTo: Realm,
existingDirectoryMetadatas: [NextcloudDirectoryMetadataTable], existingDirectoryMetadatas: Results<NextcloudDirectoryMetadataTable>,
updatedDirectoryMetadatas: [NextcloudDirectoryMetadataTable]) { updatedDirectoryMetadatas: [NextcloudDirectoryMetadataTable]) {
assert(databaseToWriteTo.isInWriteTransaction) assert(databaseToWriteTo.isInWriteTransaction)
@ -228,12 +230,14 @@ class NextcloudFilesDatabaseManager : NSObject {
etag: %@ etag: %@
""" """
, metadataToDelete.ocId, metadataToDelete.serverUrl, metadataToDelete.etag) , metadataToDelete.ocId, metadataToDelete.serverUrl, metadataToDelete.etag)
databaseToWriteTo.delete(metadataToDelete)
// Can't pass copies, we need the originals from the database
databaseToWriteTo.delete(databaseToWriteTo.objects(NextcloudDirectoryMetadataTable.self).filter("ocId == %@", metadataToDelete.ocId))
} }
} }
private func processDirectoryMetadatasToUpdate(databaseToWriteTo: Realm, private func processDirectoryMetadatasToUpdate(databaseToWriteTo: Realm,
existingDirectoryMetadatas: [NextcloudDirectoryMetadataTable], existingDirectoryMetadatas: Results<NextcloudDirectoryMetadataTable>,
updatedDirectoryMetadatas: [NextcloudDirectoryMetadataTable]) { updatedDirectoryMetadatas: [NextcloudDirectoryMetadataTable]) {
assert(databaseToWriteTo.isInWriteTransaction) assert(databaseToWriteTo.isInWriteTransaction)
@ -272,7 +276,8 @@ class NextcloudFilesDatabaseManager : NSObject {
do { do {
try database.write { try database.write {
let existingDirectoryMetadatas = directoryMetadatas(account: account, parentDirectoryServerUrl: parentDirectoryServerUrl) let existingDirectoryMetadatas = ncDatabase().objects(NextcloudDirectoryMetadataTable.self).filter("account == %@ AND parentDirectoryServerUrl == %@", account, parentDirectoryServerUrl)
processDirectoryMetadatasToDelete(databaseToWriteTo: database, processDirectoryMetadatasToDelete(databaseToWriteTo: database,
existingDirectoryMetadatas: existingDirectoryMetadatas, existingDirectoryMetadatas: existingDirectoryMetadatas,
updatedDirectoryMetadatas: updatedDirectoryMetadatas) updatedDirectoryMetadatas: updatedDirectoryMetadatas)