0

I am requesting Core Data to return me its Entity entries in ascending order based on contentid:

let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!

let sort = NSSortDescriptor(key:"contentid", ascending:true)
fetchRequest.sortDescriptors = [sort]

let fetchedResults = managedObjectContent.executeFetchRequest(fetchRequest, error: &error) as! [NSManagedObject]

How do I delete the first object in Core Data's returned, ascending entries?

I have also tried getting a single object by specifying:

fetchRequest.fetchLimit = 1

Thinking that this would return an [NSManagedObject], I attempted to then perform:

managedObjectContext.deleteObject(fetchedResults)

But it threw the error: cannot invoke method deleteObject with type '([NSManagedObject])'. What exactly am I doing incorrectly? Do I need to convert [NSManagedObject] to NSManagedObject, perhaps?

simplexity
  • 1,527
  • 1
  • 13
  • 31

3 Answers3

2

Try this:

let fetchedResults = context.executeFetchRequest(fetchRequest, error: nil)
if let result = fetchedResults?.first as? NSManagedObject {
    context.deleteObject(result)
}

executeFetchRequest returns [AnyObject]? so you must unwrap it, take first object and cast it to NSManagedObject. After that you will be able to perform delete with this object.

Kubba
  • 3,390
  • 19
  • 34
  • When saving, I am getting the error: "CoreData: error: Mutating a managed object 0x7fecdbcc0390 (0x7fecdbcc00e0) after it has been removed from its context." – simplexity May 12 '15 at 21:46
  • It appears that the above error is happening only after I am asking Core Data to return the data sorted. Otherwise it is taking the first item and removing it successfully, but what I really need it to do is remove the entry with the lowest contentid. So I am using the sort method but throws the error... – simplexity May 12 '15 at 23:57
1

Try with managedObjectContext.deleteObject(fetchedResults[0]) , checking first that fetchedResults[0] exists. Also you can use: managedObjectContext.deleteObject(fetchedResults.first)

Javier Flores Font
  • 2,075
  • 15
  • 13
0

Retrieve the first object from the returned array, then use deleteObject:

You can find a lot of useful information on Apple Documentation

--EDIT--

From Apple Doc

func executeFetchRequest(_ request: NSFetchRequest, error error: NSErrorPointer) -> [AnyObject]?

Return Value

An array of objects that meet the criteria specified by request fetched from the receiver and from the persistent stores associated with the receiver’s persistent store coordinator. If an error occurs, returns nil. If no objects match the criteria specified by request, returns an empty array.

Notice the return value of executeFetchRequest is an array, not a NSManagedObject

Leonardo
  • 1,740
  • 18
  • 27
  • Yes, I have attempted using deleteObject and have updated my question above for clarity. – simplexity May 12 '15 at 20:11
  • The part that I'm confused about is that fetchedResults is being returned as an [NSManagedObject]. When being passed to deleteObject, it says that it cannot be invoked with an NSManagedObject, but I thought that is what it is expecting. I went through the docs but still feel I'm totally missing something here... – simplexity May 12 '15 at 20:36
  • It is being returned as a NSManagedObject because you are using the as! operator to convert it – Leonardo May 12 '15 at 20:40