Olivier
Olivier

Reputation: 25

CoreData test if entity is null

i need to find a record in my CoreData database, so i made this function. My problem is if i don't find any record, i want to return null value or anything i can catch ... how i can do it ?

for now, when the function didn't find, the return is an empty object (Vendeurs) but my application crash when i test an attribute of this empty entity

func recherche(code: String) -> Vendeurs {

   let v:Vendeurs = Vendeurs()

   let context=getContext(backGround: false)
   let fetchRequest:NSFetchRequest<Vendeurs> = Vendeurs.fetchRequest()
   let predicate:NSPredicate = NSPredicate(format: "code == %@", code)
   fetchRequest.predicate = predicate

   do {
      let result = try context.fetch(fetchRequest)
      if result.count > 0 {
         return result[0]
      }
   } catch {

   }
   return v
}

my call :

let v=Vendeur()
let ve=v.recherche(code: codeSecret)
if (Int(ve.id) > 0) && (Int(ve.id) != idVendeur) {
   // blah blah
}

Thanks for you help ...

Olivier

Upvotes: 0

Views: 1250

Answers (1)

vadian
vadian

Reputation: 285079

You cannot catch something which does not throw an error. You need to improve the workflow.

Return the array and check for empty array after the call:

func recherche(code: String) -> [Vendeurs] {

   let context=getContext(backGround: false)
   let fetchRequest:NSFetchRequest<Vendeurs> = Vendeurs.fetchRequest()
   let predicate:NSPredicate = NSPredicate(format: "code == %@", code)
   fetchRequest.predicate = predicate

   do {
      return try context.fetch(fetchRequest)

   } catch {
     print(error)
     return [Vendeurs]()
   }
}

let vendeurs = v.recherche(code: codeSecret)
if !vendeurs.isEmpty, let vid = Int(vendeurs[0].id) {
    if vid > 0 && vid != idVendeur {
      // blah blah
    }
}

Or make the recherche method can throw

func recherche(code: String) -> [Vendeurs] throws {

   let context=getContext(backGround: false)
   let fetchRequest:NSFetchRequest<Vendeurs> = Vendeurs.fetchRequest()
   let predicate:NSPredicate = NSPredicate(format: "code == %@", code)
   fetchRequest.predicate = predicate
   return try context.fetch(fetchRequest)
}

do { 
   let vendeurs = try v.recherche(code: codeSecret)
   if !vendeurs.isEmpty, let vid = Int(vendeurs[0].id) {
       if vid > 0 && vid != idVendeur {
         // blah blah
       }
   }
} catch {
  print(error)
}

Or – third option – if recherche is supposed only to check and return an id > 0

func recherche(code: String) -> Int {

    let context=getContext(backGround: false)
    let fetchRequest:NSFetchRequest<Vendeurs> = Vendeurs.fetchRequest()
    let predicate:NSPredicate = NSPredicate(format: "code == %@", code)
    fetchRequest.predicate = predicate
    do {
        let result = try context.fetch(fetchRequest)
        guard !result.isEmpty, let id = Int(result[0].id) else { return 0 }
        return id
    } catch {
        print(error)
        return 0
    }
}

let vid = v.recherche(code: codeSecret)
    if vid > 0 && vid != idVendeur {
      // blah blah
    }
}

Upvotes: 1

Related Questions