From babef10504e3f070f280cd674128b2abce400264 Mon Sep 17 00:00:00 2001 From: Emin Date: Tue, 29 Oct 2024 16:49:27 +0500 Subject: [PATCH] ios: attempt to fix weird core data crashes, but related to hashes --- .../HashesPersistenceController.swift | 52 +++++++------------ .../PlacesPersistenceController.swift | 34 ------------ .../ReviewsPersistenceController.swift | 47 ----------------- .../Repositories/PlacesRepositoryImpl.swift | 10 ++-- 4 files changed, 25 insertions(+), 118 deletions(-) diff --git a/iphone/Maps/Tourism/Data/Db/PersistenceControllers/HashesPersistenceController.swift b/iphone/Maps/Tourism/Data/Db/PersistenceControllers/HashesPersistenceController.swift index f69b095c78..58a7490ecc 100644 --- a/iphone/Maps/Tourism/Data/Db/PersistenceControllers/HashesPersistenceController.swift +++ b/iphone/Maps/Tourism/Data/Db/PersistenceControllers/HashesPersistenceController.swift @@ -19,46 +19,18 @@ class HashesPersistenceController { } // MARK: - CRUD Operations - func putOneHash(_ hash: Hash) { - putHash(hash, shouldSave: true) - } - - func putHashes(hashes: [Hash]) { - hashes.forEach { hash in - putHash(hash, shouldSave: false) // Don't save in each iteration - } - - // Save the context once after all inserts/updates + func insertHashes(hashes: [Hash]) { let context = container.viewContext - do { - try context.save() - } catch { - print("Failed to save context: \(error)") - } - } - - func putHash(_ hash: Hash, shouldSave: Bool) { - let context = container.viewContext - let fetchRequest: NSFetchRequest = HashEntity.fetchRequest() - fetchRequest.predicate = NSPredicate(format: "categoryId == %lld", hash.categoryId) - fetchRequest.fetchLimit = 1 do { - let result = try context.fetch(fetchRequest).first - - if let existingHash = result { - existingHash.value = hash.value - } else { + for hash in hashes { let newHash = HashEntity(context: context) newHash.categoryId = hash.categoryId newHash.value = hash.value } - - if shouldSave { - try context.save() - } + try context.save() } catch { - print("Failed to insert or update hash: \(error)") + print("Failed to save context: \(error)") } } @@ -96,4 +68,20 @@ class HashesPersistenceController { return [] } } + + func deleteHash(hash: Hash) { + let context = container.viewContext + let fetchRequest: NSFetchRequest = HashEntity.fetchRequest() + fetchRequest.predicate = NSPredicate(format: "categoryId == %lld", hash.categoryId) + + do { + if let hash = try context.fetch(fetchRequest).first { + context.delete(hash) + try context.save() + } + } catch { + print(error) + print("Failed to delete review: \(error)") + } + } } diff --git a/iphone/Maps/Tourism/Data/Db/PersistenceControllers/PlacesPersistenceController.swift b/iphone/Maps/Tourism/Data/Db/PersistenceControllers/PlacesPersistenceController.swift index 6e478dcfc6..a7a066812c 100644 --- a/iphone/Maps/Tourism/Data/Db/PersistenceControllers/PlacesPersistenceController.swift +++ b/iphone/Maps/Tourism/Data/Db/PersistenceControllers/PlacesPersistenceController.swift @@ -35,40 +35,6 @@ class PlacesPersistenceController: NSObject, NSFetchedResultsControllerDelegate } // MARK: Places - func putPlaces(_ places: [PlaceFull], categoryId: Int64) { - let context = container.viewContext - - places.forEach { place in - putPlace(place, categoryId: categoryId, context: context) - } - - // Save the context once after all inserts/updates - do { - try context.save() - } catch { - print("Failed to save context: \(error)") - } - } - - private func putPlace(_ place: PlaceFull, categoryId: Int64, context: NSManagedObjectContext) { - let fetchRequest: NSFetchRequest = PlaceEntity.fetchRequest() - fetchRequest.predicate = NSPredicate(format: "id == %lld", place.id) - fetchRequest.fetchLimit = 1 - - do { - if let existingPlace = try context.fetch(fetchRequest).first { - updatePlace(existingPlace, with: place, categoryId: categoryId) - } else { - // Insert a new place - let newPlace = PlaceEntity(context: context) - newPlace.id = place.id - updatePlace(newPlace, with: place, categoryId: categoryId) - } - } catch { - print("Failed to insert or update place: \(error)") - } - } - func insertPlaces(_ places: [PlaceFull], categoryId: Int64) { let context = container.viewContext diff --git a/iphone/Maps/Tourism/Data/Db/PersistenceControllers/ReviewsPersistenceController.swift b/iphone/Maps/Tourism/Data/Db/PersistenceControllers/ReviewsPersistenceController.swift index 7391710ae3..01abc28f65 100644 --- a/iphone/Maps/Tourism/Data/Db/PersistenceControllers/ReviewsPersistenceController.swift +++ b/iphone/Maps/Tourism/Data/Db/PersistenceControllers/ReviewsPersistenceController.swift @@ -24,54 +24,7 @@ class ReviewsPersistenceController: NSObject, NSFetchedResultsControllerDelegate } // MARK: - Review Operations - func putReview(_ review: Review) { - let context = container.viewContext - let fetchRequest: NSFetchRequest = ReviewEntity.fetchRequest() - fetchRequest.predicate = NSPredicate(format: "id == %lld", review.id) - - do { - let results = try context.fetch(fetchRequest) - if let existingReview = results.first { - // Update existing review - updateReviewEntity(existingReview, with: review) - } else { - let newReview = ReviewEntity(context: context) - newReview.id = review.id - updateReviewEntity(newReview, with: review) - } - try context.save() - } catch { - print(error) - print("Failed to insert/update review: \(error)") - } - } - - func putReviews(_ reviews: [Review]) { - let context = container.viewContext - let fetchRequest: NSFetchRequest = ReviewEntity.fetchRequest() - - do { - for review in reviews { - fetchRequest.predicate = NSPredicate(format: "id == %lld", review.id) - let results = try context.fetch(fetchRequest) - if let existingReview = results.first { - // Update existing review - updateReviewEntity(existingReview, with: review) - } else { - let newReview = ReviewEntity(context: context) - newReview.id = review.id - updateReviewEntity(newReview, with: review) - } - } - try context.save() - } catch { - print(error) - print("Failed to insert/update reviews: \(error)") - } - } - func insertReviews(_ reviews: [Review]) { - print("inserting") let context = container.viewContext do { diff --git a/iphone/Maps/Tourism/Data/Repositories/PlacesRepositoryImpl.swift b/iphone/Maps/Tourism/Data/Repositories/PlacesRepositoryImpl.swift index 38873806db..02564e0af7 100644 --- a/iphone/Maps/Tourism/Data/Repositories/PlacesRepositoryImpl.swift +++ b/iphone/Maps/Tourism/Data/Repositories/PlacesRepositoryImpl.swift @@ -91,7 +91,7 @@ class PlacesRepositoryImpl: PlacesRepository { } // update hashes - hashesPersistenceController.putHashes(hashes: [ + hashesPersistenceController.insertHashes(hashes: [ Hash(categoryId: PlaceCategory.sights.id, value: allData.attractionsHash), Hash(categoryId: PlaceCategory.restaurants.id, value: allData.restaurantsHash), Hash(categoryId: PlaceCategory.hotels.id, value: allData.accommodationsHash) @@ -162,10 +162,10 @@ class PlacesRepositoryImpl: PlacesRepository { } // update hash - hashesPersistenceController.putHash( - Hash(categoryId: hash!.categoryId, value: resource.hash), - shouldSave: true - ) + hashesPersistenceController.deleteHash(hash: hash!) + hashesPersistenceController.insertHashes(hashes: [ + Hash(categoryId: hash!.categoryId, value: resource.hash) + ]) } }