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?

4
  • [NSManagedObject] it's an Array which holds NSManagedObject's instances. You can't invoke deleteObject on array. Commented May 12, 2015 at 20:28
  • Ah I see. So what should it be invoked with? Commented May 12, 2015 at 20:46
  • please see my answer below Commented May 12, 2015 at 20:57
  • An NSArray has a property firstObject, which depending on your sort, may be helpful. Commented May 12, 2015 at 23:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

When saving, I am getting the error: "CoreData: error: Mutating a managed object 0x7fecdbcc0390 <x-coredata:///Article/tCEF47CDA-545F-484B-A2E5-72912A74D6CE2> (0x7fecdbcc00e0) after it has been removed from its context."
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...
1

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

2 Comments

Thanks Javier. For some reason I am still getting the same console error as written in Kubba's answer. Very strange.
Is there a way to have this done after having it sort by ascending order?
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

3 Comments

Yes, I have attempted using deleteObject and have updated my question above for clarity.
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...
It is being returned as a NSManagedObject because you are using the as! operator to convert it

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.