Larme

Untitled

Sep 22nd, 2021
822
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Let's debug...
  2.  
  3. Let's break your predicate first:
  4.  
  5.     let afterPredicate = NSPredicate(format: "timestamp >= %f, startTimeStamp)
  6.    let beforePredicate = NSPredicate(format: "timestamp < %f", endTimeStamp)
  7.    let combinedPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [afterPredicate, beforePredicate])
  8.  
  9.  
  10. With:
  11.  
  12.    func executeRequest(predicate: NSPredicate) -> [Item] {
  13.        let request = NSFetchRequest<Item>(entityName: "Item")
  14.        request.predicate = predicate
  15.        var items: [Item] = []
  16.        context.performAndWait {
  17.            do {
  18.                let fetched = try context.fetch(request)
  19.                items.append(contentsOf: fetched)
  20.            } catch {
  21.                print(error)
  22.            }
  23.        }
  24.        return items
  25.    }
  26.  
  27.  
  28. Now, let's test it:
  29.  
  30.    let after = executeRequest(predicate: afterPredicate)
  31.    print("After: \(after)")
  32.    let before = executeRequest(predicate: beforePredicate)
  33.    print("Before: \(before)")
  34.    
  35.    let combined = executeRequest(predicate: combinedPredicate)
  36.    print("Combined: \(combined)")
  37.    
  38.    let all = executeRequest(predicate: NSPredicate(value: true))
  39.    
  40.    let filterAfter = all.filter { afterPredicate.evaluate(with: $0) }
  41.    print("FilterAfter: \(filterAfter)")
  42.    let filterBefore = all.filter { beforePredicate.evaluate(with: $0) }
  43.    print("FilterBefore: \(filterBefore)")
  44.    let filterCombined = all.filter { predicate.evaluate(with: $0) }
  45.    print("FilterCombined: \(filterCombinedAfter)")
RAW Paste Data