简体   繁体   中英

Foundation._GenericObjCError.NilError from core data batch delete

I am trying to use batch delete feature of core data. I have an entity named Car. That entity has a column name modelNumber as Int . I want to delete all cars which has modelNumber older than 2000. Here is my code:

func deleteCarsOlderThan(modelNumber: Int) {
    let predicate = NSPredicate(format: "modelNumber <= %@", NSNumber(int: modelNumber))

    let fetchRequest = NSFetchRequest(entityName: "Car")
    fetchRequest.predicate = predicate

    let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
    deleteRequest.resultType = .ResultTypeCount
    do {
        let result = try self.fhirManagedObjectContext.executeRequest(deleteRequest)
        try self.fhirManagedObjectContext.save()
    }
    catch {
        print(error)
    }
}

While executing this code, control goes to catch block and it gives an error says: Foundation._GenericObjCError.NilError . My fetch request is working well as if I use:

    let olderCars = self.executeFetchRequest(fetchRequest)

it returns me an array of older cars. I don't know where I am doing wrong. I am using iOS9 for this purpose.

TL;DR: While self.fhirManagedObjectContext is non-optional, it's probably returning nil from Objective-C.

The error you observed is generated by Swift's Foundation bridging runtime. (See the source code here .) This occurs when an Objective-C method with an error pointer returns a failure value ( NO or nil ), but no actual error was passed back via the NSError pointer. This could either be the result of a bug in Core Data or, more likely, a nil managed object context that when using Objective-C method dispatch causes the method to appear to return NO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM