0

I am working on an app written in Swift that uses Core Data. For it to work correctly, I need to delete all the objects in Core Data for a specific entity and key. I have been able to find many different questions covering deleting from core data, but as far as I can tell none of them only delete objects for a specific key.

My current code follows a similar style to the "Fetch, Delete, Repeat" method in this article. It talks about an iOS 9 updated way to do this with NSBatchDeleteRequest, but I have not discovered any way to make either of these only delete the values for a specific key.

Current Delete Code (I have tried to add a key to the object, but it throws an error about not being able to cast NSCFNumber to NSManagedObject at runtime)

getContext().delete(object.value(forKey: key) as! NSManagedObject)

Full Code Pertaining to Core Data

func getContext () -> NSManagedObjectContext {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    return appDelegate.persistentContainer.viewContext
}

func coreDataReplaceValue(entity: String, key: String, value: Int) {
    let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: entity)

    do {
        let searchResults = try getContext().fetch(fetchRequest)

        for object in searchResults as! [NSManagedObject] {
            getContext().delete(object.value(forKey: key) as! NSManagedObject)
        }
    } catch {
        print("Error with request: \(error)")
    }


    let context = getContext()

    let entity =  NSEntityDescription.entity(forEntityName: entity, in: context)

    let accelerationLevel = NSManagedObject(entity: entity!, insertInto: context)

    accelerationLevel.setValue(value, forKey: key)

    do {
        try context.save()
        print("saved!")
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    } catch {

    }
}

Other questions I have looked at that pertain to my question, but don't include a way to add a key:

Swift 3 Core Data Delete Object

delete core data managed object with Swift 3

This last one looked promising at first, but the code changes it into an NSManagedObject, so I don't think it holds the solution.

Deleting first object in Core Data (Swift)

Thanks for any help!

Taylor

6
  • @Taylor may be you can get your solution from stackoverflow.com/questions/40756178/… Commented Nov 23, 2016 at 5:34
  • @RajJoshi It appears that only an entity is defined in the deleting data records section. I am looking for a way to delete only those with a certain key from the entity. Commented Nov 23, 2016 at 5:41
  • @KiritModi Are you suggesting inserting something instead of the index.row in order to achieve this? I don't understand what I would need to substitute it for to delete all entries of a certain key. If there is a way, I would love to know. Commented Nov 23, 2016 at 5:45
  • @Taylor we can not delete a key value from a object we can only assign nil value to a particular object's key like object.name = nil and save context value automatic delete from a object's member Commented Nov 23, 2016 at 5:59
  • If it is of type Int, would that work? Commented Nov 23, 2016 at 6:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.