1

I have an app where I need to store a list of products to core data. I call the following method:

var productsList: [ProducttCD] = []  // ProductCd is the codable class used to decode products

private func getSavedProducts() {
    let context = appDelegate.persistentContainer.viewContext
    let request: NSFetchRequest<ProductCD> = ProductCD.fetchRequest()
    do {
        productsList = try context.fetch(request)
    } catch {
        print("Error fetching data from context \(error)")
    }
            
    if produtsList.count == 0 {
        self.saveProductsToCD(taskType: .saveAll)
    } else if let lastSaved = productsLastSaved, lastSaved < (Date().timeIntervalSince1970 - 86400) {
        self.saveProductsToCD(taskType: .update)
    }
}

Now, what I want to do is to essentially update this DB if it is more that 24 hours old. This means if one attribute for a specific entity has changed, for example, it should update the core data database.

What I had planned to do was to delete then entire database which seems extreme.

I did try to loop through the ids linked to the object, filter those which matched the core data entries and find a match. Bit this tool takes huge amount of time to use. What I would like to do is to delete the entire database of these entries completely. Then I could re save the airports to the blank DB. However, I am not sure how to delete all instances of a spcific property...

DevB1
  • 1,235
  • 3
  • 17
  • 43
  • Why not call instance method delete(_ object: NSManagedObject) on all managed object instances? You can call it like: coreDataStack.managedContext.delete(managedObject). https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506847-delete – Luke Dec 18 '20 at 22:54
  • https://stackoverflow.com/questions/1383598/core-data-quickest-way-to-delete-all-instances-of-an-entity – Joakim Danielson Dec 19 '20 at 08:07

0 Answers0