user9039131
user9039131

Reputation:

Fetching core data from database fails due to FOR loop

I am trying to retrieve all values excluding null from one attribute from my core data during the start of the viewController. But during the for loop the no always fetches value 0 and doesn't increment ahead. So my results.count is 8, then it displays 0 for 8 times and fetching the same value for the attribute.

func searchMark() -> Int
{
    do
    {
        let mngdCntxt = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "AddedBookmark")
        let results = try mngdCntxt.fetch(fetchRequest)
        //fetchRequest.returnsObjectsAsFaults = false

        for no in 0..<results.count{

          if let match = results[no] as? AddedBookmark
             {
                  print(no)
                  let providerNo = match.value(forKey: "providerNo") as! Int
                  print("providerNo: \(providerNo)")
                  return providerNo
           } 
    }
    catch{}
    return 0
}

The value of providerNo is fetched same through the for loop.

Upvotes: 0

Views: 87

Answers (2)

vadian
vadian

Reputation: 285290

If you are

trying to retrieve all values excluding null from one attribute

that implies that you actually want to return an Int array and your force-unwrapped attribute implies that the attribute is declared as non-optional. According to these two assumptions null is meant to be 0

The logical conclusion is to specify a predicate to return all records whose providerNo != 0, map the items to the providerNo values and return that.

func searchMark() -> [Int]
{
    do {
        let mngdCntxt = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "AddedBookmark")
        fetchRequest.predicate = NSPredicate(format: "providerNo != 0")
        let results = try mngdCntxt.fetch(fetchRequest) as! [AddedBookmark]
        return results.map{ Int($0.providerNo) }
    }
    catch {
        print(error)
        return [Int]()
    }
}

Upvotes: 0

standousset
standousset

Reputation: 1161

You return too soon, so the loop does not even increment once (and that is also why the loop does not crash when no == results.count):

func searchMark() -> Int {
    var output = 0
    do {
        let mngdCntxt = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "AddedBookmark")
        let results = try mngdCntxt.fetch(fetchRequest)

        for no in 0...(results.count-1) {
           if let match = results[no] as? AddedBookmark {
                print(no)
                let providerNo = match.value(forKey: "providerNo") as! Int
                print("providerNo: \(providerNo)")
                output = providerNo
           }
        } 
        return output
    }
    catch{}
    return output
}

This function may not be exactly what you expect but it shows you how big the loop should be and when to return

Upvotes: 1

Related Questions