0

I have an entity in Core data with date attribute. I want to fetch all the missing rows in a date range from Core data.

Eg: I have 25th-Dec-2021 row in Coredata. Now, I want to get all the dates in Dec 2021 (Date range -> 1st-Dec-2021 to 31st-Dec-2021) except the 25th-Dec-2021

In SQLite I could achieve this by creating a temporary table with dates from 1st-Dec-2021 to 31st-Dec-2021 by using Recursive CTE and then subtracting my existing table. This will give me the missing rows in the date range.

I am not sure how I can achieve this in iOS Core Data.

Mohamed Wasiq
  • 490
  • 4
  • 17
  • Use a NSPredicate, and get the items with date > 2021-12-01 and < 2021-12-25 or date > 2021-12-25 and < 2021-12-31. – Larme Dec 16 '21 at 12:54
  • My Core data has only 1 row, which is 2021-12-25. So I want to get all the date rows between 2021-12-01 to 2021-12-31 which are not present in the Core data – Mohamed Wasiq Dec 16 '21 at 13:16
  • 1
    I didn't understand correctly? You want an array of dates starting from 2021-12-01 to 2021-12-31 but without 2021-12-25? Like "Get all day of the month except if it's today? Could you maybe give the desired output? – Larme Dec 16 '21 at 13:17
  • So you want: https://stackoverflow.com/questions/32536612/all-dates-between-two-date-objects-swift doing it it twice (since there are in fact 2 ranges, as I previsouly described)? – Larme Dec 16 '21 at 13:19
  • Suppose I have 3 rows in Core Data entity, 3rd Dec, 5th Dec, and 8th Dec. I want a query which returns 1st Dec, 2nd Dec, 4th Dec, 6th Dec, 7th Dec, 9th Dec and 10th Dec for date range from 1st Dec to 10th Dec – Mohamed Wasiq Dec 16 '21 at 13:23
  • 1
    You can't do a query, since there is no objects. I don't understand what you're supposed to get. You can create an array of dates, but that won't be items in CoreData. In the linked question, modify the code to not append the date if it's in the same of one you want to exclude. – Larme Dec 16 '21 at 13:25
  • In SQLite it is possible by creating a temporary table from 1st Dec to 10th Dec using Recursive CTE. And subtracting the existing table which has 3 dates. This will return me the missing dates. – Mohamed Wasiq Dec 16 '21 at 13:28
  • 2
    Stop thinking in sql and start thinking in swift, that is my advice. I am not sure exactly what you want to accomplish but start by creating the date range array in swift as suggested and then fetch any objects with dates in the range and use the result to subtract matching dates from your array. – Joakim Danielson Dec 16 '21 at 13:55
  • Thank you for your suggestion. You are right, I should stop thinking from SQL perspective. – Mohamed Wasiq Dec 16 '21 at 14:01

0 Answers0