0

I am presently making my first steps using Core Data, with Swift. Even though I have quite a bit of experience with Core Data, I have close to none with Swift.

Here is what I want to do: make a function which is going to compute the number of records in an entity, from the name of the entity.

Something like this:

func countEntity (name:String) -> Int {
    var theNumberOfRecords:Int
    let fetchRequest = NSFetchRequest(entityName:name)
    // What I have tried at this point only gives error messages …..
    return theNumberOfRecords
}

It should not be rocket science. But I have not been able to make something work after various trial and errors. How should I write the necessary code? I presume it should be no more that a few lines. The one I have in Objective C is 6 lines.

Michel
  • 10,303
  • 17
  • 82
  • 179
  • If you want to write a lot fewer lines of code, try using https://github.com/Prosumma/CoreDataQueryInterface. Your query would be something like `managedObjectContext.from(Entity).filter({$0.name == name}).count()` and that's it. – Gregory Higley Jun 06 '15 at 01:57

1 Answers1

1

You get the number of objects a fetch request would have returned with countForFetchRequest():

func countEntity (name:String, context : NSManagedObjectContext) -> Int {
    let fetchRequest = NSFetchRequest(entityName:name)
    var error : NSError?
    let theNumberOfRecords = context.countForFetchRequest(fetchRequest, error: &error)
    if theNumberOfRecords == NSNotFound {
        println("Error: \(error?.localizedDescription)")
    }
    return theNumberOfRecords
}

In the case of an error, countForFetchRequest() returns NSNotFound and you have to decide how to handle that situation.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks. When writing what you suggest. I first get a complaint asking for a ! after `context` After fixing this, I have no error. But then, when I try to call this function (which I have placed in `AppDelegate`) from the `RootViewController` I get an error saying: `Cannot invoke 'countEntity' with an argument list of type '(String)'` I don’t quite understand why, but I suppose I must be making some beginner mistake. Am I not missing the equivalent of "#import AppDelegate.h" in objective C, if that ever exists? – Michel May 29 '15 at 06:10
  • @Michel: I changed the signature of the function so that it must be called as `countEntity("YourEntityName", context)`. Does the RootViewController have a pointer to the managed object context? How did you do it in Objective-C? Perhaps something like `let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!` is what you are looking for, compare http://stackoverflow.com/a/24046979/1187415. – Martin R May 29 '15 at 06:49
  • I saw that, but I do not think this is the issue, because I kept the old signature. The RootViewController not having a pointer to the managed object context, I use the local one already inside AppDelegate. This is what I always did in ObjC. But how can RootViewController know about the proper signature for a function somewhere else? Since there is no "#import AppDelegate.h" like thing? I am missing some detail. – Michel May 29 '15 at 07:04
  • Following what is in the link you just mentioned. I was able to make it work, with both your signature and mine. It seems like I need a bit of practice in Swift:) – Michel May 29 '15 at 07:16